Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Merge branch 'master' into master

Toran Bruce Richards93c25827051b
diff --cc scripts/chat.py
index 505c061e5a,f4cf22991b..4a7056d2b1
--- a/scripts/chat.py
+++ b/scripts/chat.py
@@@ -43,7 -44,9 +44,8 @@@ def chat_with_ai
user_input,
full_message_history,
permanent_memory,
- token_limit,
- debug=False):
+ token_limit):
+ """Interact with the OpenAI API, sending the prompt, user input, message history, and permanent memory."""
while True:
try:
"""
@@@ -61,8 -64,8 +63,10 @@@
"""
model = cfg.fast_llm_model # TODO: Change model from hardcode to argument
# Reserve 1000 tokens for the response
- if debug:
- print(f"Token limit: {token_limit}")
++
+ if cfg.debug_mode:
+ print(f"Token limit: {token_limit}")
++
send_token_limit = token_limit - 1000
relevant_memory = permanent_memory.get_relevant(str(full_message_history[-5:]), 10)
diff --cc scripts/config.py
index a7c980e1a4,03f1d5df27..9b6f95b793
--- a/scripts/config.py
+++ b/scripts/config.py
@@@ -26,9 -32,10 +32,11 @@@ class Config(metaclass=Singleton)
"""
def __init__(self):
+ """Initialize the Config class"""
+ self.debug = False
self.continuous_mode = False
self.speak_mode = False
+ self.debug_mode = False
# TODO - make these models be self-contained, using langchain, so we can configure them once and call it good
self.fast_llm_model = os.getenv("FAST_LLM_MODEL", "gpt-3.5-turbo")
self.smart_llm_model = os.getenv("SMART_LLM_MODEL", "gpt-4")
@@@ -65,12 -83,11 +84,14 @@@
self.continuous_mode = value
def set_speak_mode(self, value: bool):
+ """Set the speak mode value."""
self.speak_mode = value
+ def set_debug_mode(self, value: bool):
+ self.debug_mode = value
+
def set_fast_llm_model(self, value: str):
+ """Set the fast LLM model value."""
self.fast_llm_model = value
def set_smart_llm_model(self, value: str):
diff --cc scripts/json_parser.py
index 4a6f284591,e83cb15445..4165a1d23b
--- a/scripts/json_parser.py
+++ b/scripts/json_parser.py
@@@ -26,30 -34,46 +34,48 @@@ def fix_and_parse_json
try:
json_str = json_str.replace('\t', '')
return json.loads(json_str)
- except Exception as e:
- # Let's do something manually - sometimes GPT responds with something BEFORE the braces:
- # "I'm sorry, I don't understand. Please try again."{"text": "I'm sorry, I don't understand. Please try again.", "confidence": 0.0}
- # So let's try to find the first brace and then parse the rest of the string
+ except json.JSONDecodeError as _: # noqa: F841
+ json_str = correct_json(json_str)
try:
- brace_index = json_str.index("{")
- json_str = json_str[brace_index:]
- last_brace_index = json_str.rindex("}")
- json_str = json_str[:last_brace_index+1]
- return json.loads(json_str)
- except Exception as e:
- if try_to_fix_with_gpt:
- print(f"Warning: Failed to parse AI output, attempting to fix.\n If you see this warning frequently, it's likely that your prompt is confusing the AI. Try changing it up slightly.")
+ return json.loads(json_str)
+ except json.JSONDecodeError as _: # noqa: F841
+ pass
+ # Let's do something manually:
+ # sometimes GPT responds with something BEFORE the braces:
+ # "I'm sorry, I don't understand. Please try again."
+ # {"text": "I'm sorry, I don't understand. Please try again.",
+ # "confidence": 0.0}
+ # So let's try to find the first brace and then parse the rest
+ # of the string
+ try:
+ brace_index = json_str.index("{")
+ json_str = json_str[brace_index:]
+ last_brace_index = json_str.rindex("}")
+ json_str = json_str[:last_brace_index+1]
+ return json.loads(json_str)
+ except json.JSONDecodeError as e: # noqa: F841
+ if try_to_fix_with_gpt:
+ print("Warning: Failed to parse AI output, attempting to fix."
+ "\n If you see this warning frequently, it's likely that"
+ " your prompt is confusing the AI. Try changing it up"
+ " slightly.")
# Now try to fix this up using the ai_functions
- ai_fixed_json = fix_json(json_str, json_schema)
- ai_fixed_json = fix_json(json_str, JSON_SCHEMA, cfg.debug)
++ ai_fixed_json = fix_json(json_str, JSON_SCHEMA)
++
if ai_fixed_json != "failed":
- return json.loads(ai_fixed_json)
+ return json.loads(ai_fixed_json)
else:
- print(f"Failed to fix ai output, telling the AI.") # This allows the AI to react to the error message, which usually results in it correcting its ways.
- return json_str
- else:
+ # This allows the AI to react to the error message,
+ # which usually results in it correcting its ways.
+ print("Failed to fix ai output, telling the AI.")
+ return json_str
+ else:
raise e
-
-
-def fix_json(json_str: str, schema: str, debug=False) -> str:
++
+
+def fix_json(json_str: str, schema: str) -> str:
+ """Fix the given JSON string to make it parseable and fully complient with the provided schema."""
++
# Try to fix the JSON using gpt:
function_string = "def fix_json(json_str: str, schema:str=None) -> str:"
args = [f"'''{json_str}'''", f"'''{schema}'''"]