Refactor agent.py to improve JSON handling and validation

pull/1866/head
Merwane Hamadi 2023-04-16 09:41:52 -07:00
parent d934d226ce
commit bf24cd9508
1 changed files with 15 additions and 14 deletions

View File

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