From bafcdcea7c89ffa1cdac037673b9110a6313e086 Mon Sep 17 00:00:00 2001 From: lukas-eu <62448426+lukas-eu@users.noreply.github.com> Date: Tue, 27 Jun 2023 18:16:08 +0200 Subject: [PATCH] Filtering out ANSI escape codes in printed assistant thoughts (#4810) Co-authored-by: merwanehamadi Co-authored-by: Luke K (pr-0f3t) <2609441+lc0rp@users.noreply.github.com> --- autogpt/agent/agent.py | 4 ++-- autogpt/logs.py | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.py index fca03a5f..c578152a 100644 --- a/autogpt/agent/agent.py +++ b/autogpt/agent/agent.py @@ -17,7 +17,7 @@ from autogpt.log_cycle.log_cycle import ( USER_INPUT_FILE_NAME, LogCycleHandler, ) -from autogpt.logs import logger, print_assistant_thoughts +from autogpt.logs import logger, print_assistant_thoughts, remove_ansi_escape from autogpt.memory.message_history import MessageHistory from autogpt.memory.vector import VectorMemory from autogpt.models.command_registry import CommandRegistry @@ -185,7 +185,7 @@ class Agent: logger.typewriter_log( "NEXT ACTION: ", Fore.CYAN, - f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} " + f"COMMAND = {Fore.CYAN}{remove_ansi_escape(command_name)}{Style.RESET_ALL} " f"ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}", ) diff --git a/autogpt/logs.py b/autogpt/logs.py index 90d006bc..8eb4c94a 100644 --- a/autogpt/logs.py +++ b/autogpt/logs.py @@ -249,6 +249,10 @@ def remove_color_codes(s: str) -> str: return ansi_escape.sub("", s) +def remove_ansi_escape(s: str) -> str: + return s.replace("\x1B", "") + + logger = Logger() @@ -263,12 +267,16 @@ def print_assistant_thoughts( assistant_thoughts_criticism = None assistant_thoughts = assistant_reply_json_valid.get("thoughts", {}) - assistant_thoughts_text = assistant_thoughts.get("text") + assistant_thoughts_text = remove_ansi_escape(assistant_thoughts.get("text")) if assistant_thoughts: - 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") + assistant_thoughts_reasoning = remove_ansi_escape( + assistant_thoughts.get("reasoning") + ) + assistant_thoughts_plan = remove_ansi_escape(assistant_thoughts.get("plan")) + assistant_thoughts_criticism = remove_ansi_escape( + assistant_thoughts.get("criticism") + ) + assistant_thoughts_speak = remove_ansi_escape(assistant_thoughts.get("speak")) logger.typewriter_log( f"{ai_name.upper()} THOUGHTS:", Fore.YELLOW, f"{assistant_thoughts_text}" )