-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Added the ability for users to use config files with V3 #1077
Conversation
src/revChatGPT/V3.py
Outdated
@@ -22,7 +22,7 @@ class Chatbot: | |||
|
|||
def __init__( | |||
self, | |||
api_key: str, | |||
api_key: str = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Api key can be included in config, so we don't require it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need checks to see if it's included in at least 1 option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some formatting things to consider in comments
Wouldn't a more dry solution not require a def save(self, file: str, *keys: str) -> None:
"""
Save the Chatbot configuration to a JSON file
"""
with open(file, "w", encoding="utf-8") as f:
json.dump(
{
key: self.__dict__[key]
for key in self.__get_filtered_class_keys(*keys)
},
f,
indent=2,
)
def load(self, file: str, *keys: str) -> None:
"""
Load the Chatbot configuration from a JSON file
"""
with open(file, encoding="utf-8") as f:
loaded_config = json.load(f)
self.__dict__.update(
{
key: loaded_config[key]
for key in self.__get_filtered_class_keys(*keys)
}
)
def __get_filtered_class_keys(self, *keys: str) -> list:
"""
Get filtered class keys from the given keys.
"""
class_keys = self.__dict__.keys() - {"session"}
if not keys:
return class_keys
# Check if all passed keys are valid
if invalid_keys := set(keys) - {"not"} - class_keys:
raise ValueError(
f"Invalid keys: {invalid_keys}",
)
# Remove the passed keys from the class keys.
if keys[0] is "not":
given_keys = keys[1:]
return [key for key in class_keys if key not in given_keys]
else:
# Only return specified keys that are in class_keys
return [key for key in keys if key in class_keys] This would allow for commands like:
if config:
chatbot = ChatbotCLI(args.api_key)
try:
chatbot.load(config)
except:
print(f"Error: {args.config} could not be loaded")
sys.exit()
else:
chatbot = ChatbotCLI(
api_key=args.api_key,
system_prompt=args.base_prompt,
proxy=args.proxy,
temperature=args.temperature,
top_p=args.top_p,
reply_count=args.reply_count,
) |
Co-authored-by: Daniel Parizher <105245560+Arborym@users.noreply.github.com>
Co-authored-by: Daniel Parizher <105245560+Arborym@users.noreply.github.com>
I'll refactor my code to use this approach |
Does anyone know why the PR isn't updating? I added a commit to the branch, and it appears there, but they're not appearing here. |
It seems I needed to add another commit, and now it updated the pr. |
src/revChatGPT/V3.py
Outdated
if config.get("reply_count") is not None: | ||
reply_count = config.get("reply_count") | ||
if config.get("system_prompt") is not None: | ||
system_prompt = config.get("system_prompt")``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accidentally left a Markdown artifact in here 😅
system_prompt = config.get("system_prompt")``` | |
system_prompt = config.get("system_prompt") |
Conflicts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
@acheong08 The custom submit key breaks things if a submit key isn't given because it then tries to reference the key_binding var when it's never defined. Should I fix that in this pr? |
Would be appreciated |
Co-authored-by: Daniel Parizher <105245560+Arborym@users.noreply.github.com>
I added the ability for people to add a config file using --config. There are some conflicts with a few new commits, so I still need to resolve them. Link this pr to issue #981.