From 099a5e1090bb93d2aa33e44718e5faa83d4ecce9 Mon Sep 17 00:00:00 2001 From: Toran Bruce Richards Date: Mon, 3 Apr 2023 11:30:06 +0100 Subject: [PATCH] Handles incorrect AI formatting in a more forgiving way. --- scripts/commands.py | 12 +++++++++++- scripts/main.py | 29 ++++++++++++++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/scripts/commands.py b/scripts/commands.py index ff65f1a169..f8290471b7 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -15,9 +15,19 @@ cfg = Config() def get_command(response): try: response_json = fix_and_parse_json(response) + + if "command" not in response_json: + return "Error:" , "Missing 'command' object in JSON" + command = response_json["command"] + + if "name" not in command: + return "Error:", "Missing 'name' field in 'command' object" + command_name = command["name"] - arguments = command["args"] + + # Use an empty dictionary if 'args' field is not present in 'command' object + arguments = command.get("args", {}) if not arguments: arguments = {} diff --git a/scripts/main.py b/scripts/main.py index ef4ba58768..2f01ccd26d 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -54,19 +54,22 @@ def print_assistant_thoughts(assistant_reply): # Parse and print Assistant response assistant_reply_json = fix_and_parse_json(assistant_reply) - assistant_thoughts = assistant_reply_json.get("thoughts") - if assistant_thoughts: - assistant_thoughts_text = assistant_thoughts.get("text") - assistant_thoughts_reasoning = assistant_thoughts.get("reasoning") - assistant_thoughts_plan = assistant_thoughts.get("plan") - assistant_thoughts_criticism = assistant_thoughts.get("criticism") - assistant_thoughts_speak = assistant_thoughts.get("speak") - else: - assistant_thoughts_text = None - assistant_thoughts_reasoning = None - assistant_thoughts_plan = None - assistant_thoughts_criticism = None - assistant_thoughts_speak = None + try: + assistant_thoughts = assistant_reply_json.get("thoughts") + if assistant_thoughts: + assistant_thoughts_text = assistant_thoughts.get("text") + assistant_thoughts_reasoning = assistant_thoughts.get("reasoning") + assistant_thoughts_plan = assistant_thoughts.get("plan") + assistant_thoughts_criticism = assistant_thoughts.get("criticism") + assistant_thoughts_speak = assistant_thoughts.get("speak") + else: + assistant_thoughts_text = None + assistant_thoughts_reasoning = None + assistant_thoughts_plan = None + assistant_thoughts_criticism = None + assistant_thoughts_speak = None + except Exception as e: + assistant_thoughts_text = "The AI's response was unreadable." print_to_console( f"{ai_name.upper()} THOUGHTS:",