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.

Refactor agent.py to improve JSON handling and validation

Merwane Hamadibf24cd950831
diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.py
index 301d3f023e..32d982e52a 100644
--- a/autogpt/agent/agent.py
+++ b/autogpt/agent/agent.py
@@ -3,9 +3,8 @@ from autogpt.app import execute_command, get_command
from autogpt.chat import chat_with_ai, create_chat_message
from autogpt.config import Config
-from autogpt.json_fixes.bracket_termination import (
- attempt_to_fix_json_by_finding_outermost_brackets,
-)
+from autogpt.json_fixes.master_json_fix_method import fix_json_using_multiple_techniques
+from autogpt.json_validation.validate_json import validate_json
from autogpt.logs import logger, print_assistant_thoughts
from autogpt.speech import say_text
from autogpt.spinner import Spinner
@@ -70,18 +69,20 @@ class Agent:
cfg.fast_token_limit,
) # TODO: This hardcodes the model to use GPT3.5. Make this an argument
- # Print Assistant thoughts
- print_assistant_thoughts(self.ai_name, assistant_reply)
+ assistant_reply_json = fix_json_using_multiple_techniques(assistant_reply)
- # Get command name and arguments
- try:
- command_name, arguments = get_command(
- attempt_to_fix_json_by_finding_outermost_brackets(assistant_reply)
- )
- if cfg.speak_mode:
- say_text(f"I want to execute {command_name}")
- except Exception as e:
- logger.error("Error: \n", str(e))
+ # Print Assistant thoughts
+ if assistant_reply_json != {}:
+ validate_json(assistant_reply_json, 'llm_response_format_1')
+ # Get command name and arguments
+ try:
+ print_assistant_thoughts(self.ai_name, assistant_reply_json)
+ command_name, arguments = get_command(assistant_reply_json)
+ # command_name, arguments = assistant_reply_json_valid["command"]["name"], assistant_reply_json_valid["command"]["args"]
+ if cfg.speak_mode:
+ say_text(f"I want to execute {command_name}")
+ except Exception as e:
+ logger.error("Error: \n", str(e))
if not cfg.continuous_mode and self.next_action_count == 0:
### GET USER AUTHORIZATION TO EXECUTE COMMAND ###