From cd8fdb31efef2d6b755011f491863a17eead1bdd Mon Sep 17 00:00:00 2001 From: WladBlank Date: Wed, 26 Apr 2023 22:08:39 +0200 Subject: [PATCH] Chat plugin capability (#2929) Co-authored-by: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> --- .env.template | 6 +++++ autogpt/agent/agent.py | 30 ++++++++++++++++++++---- autogpt/config/config.py | 2 ++ autogpt/logs.py | 3 +++ autogpt/utils.py | 50 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 84 insertions(+), 7 deletions(-) diff --git a/.env.template b/.env.template index 1d9eefb3..3d2746c4 100644 --- a/.env.template +++ b/.env.template @@ -201,3 +201,9 @@ OPENAI_API_KEY=your-openai-api-key #ALLOWLISTED_PLUGINS - Sets the listed plugins that are allowed (Example: plugin1,plugin2,plugin3) ALLOWLISTED_PLUGINS= + +################################################################################ +### CHAT PLUGIN SETTINGS +################################################################################ +# CHAT_MESSAGES_ENABLED - Enable chat messages (Default: False) +# CHAT_MESSAGES_ENABLED=False diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.py index 395d00ae..6dd5918b 100644 --- a/autogpt/agent/agent.py +++ b/autogpt/agent/agent.py @@ -9,7 +9,7 @@ from autogpt.llm_utils import create_chat_completion from autogpt.logs import logger, print_assistant_thoughts from autogpt.speech import say_text from autogpt.spinner import Spinner -from autogpt.utils import clean_input +from autogpt.utils import clean_input, send_chat_message_to_user from autogpt.workspace import Workspace @@ -84,8 +84,11 @@ class Agent: logger.typewriter_log( "Continuous Limit Reached: ", Fore.YELLOW, f"{cfg.continuous_limit}" ) + send_chat_message_to_user( + f"Continuous Limit Reached: \n {cfg.continuous_limit}" + ) break - + send_chat_message_to_user("Thinking... \n") # Send message to AI, get response with Spinner("Thinking... "): assistant_reply = chat_with_ai( @@ -114,6 +117,8 @@ class Agent: command_name, arguments = get_command(assistant_reply_json) if cfg.speak_mode: say_text(f"I want to execute {command_name}") + + send_chat_message_to_user("Thinking... \n") arguments = self._resolve_pathlike_command_args(arguments) except Exception as e: @@ -123,6 +128,11 @@ class Agent: # ### GET USER AUTHORIZATION TO EXECUTE COMMAND ### # Get key press: Prompt the user to press enter to continue or escape # to exit + self.user_input = "" + send_chat_message_to_user( + "NEXT ACTION: \n " + f"COMMAND = {command_name} \n " + f"ARGUMENTS = {arguments}" + ) logger.typewriter_log( "NEXT ACTION: ", Fore.CYAN, @@ -136,9 +146,13 @@ class Agent: flush=True, ) while True: - console_input = clean_input( - Fore.MAGENTA + "Input:" + Style.RESET_ALL - ) + console_input = "" + if cfg.chat_messages_enabled: + console_input = clean_input("Waiting for your response...") + else: + console_input = clean_input( + Fore.MAGENTA + "Input:" + Style.RESET_ALL + ) if console_input.lower().strip() == "y": user_input = "GENERATE NEXT COMMAND JSON" break @@ -193,10 +207,16 @@ class Agent: "", ) elif user_input == "EXIT": + send_chat_message_to_user("Exiting...") print("Exiting...", flush=True) break else: # Print command + send_chat_message_to_user( + "NEXT ACTION: \n " + f"COMMAND = {command_name} \n " + f"ARGUMENTS = {arguments}" + ) + logger.typewriter_log( "NEXT ACTION: ", Fore.CYAN, diff --git a/autogpt/config/config.py b/autogpt/config/config.py index 97ab2d58..c0afaeb1 100644 --- a/autogpt/config/config.py +++ b/autogpt/config/config.py @@ -61,6 +61,8 @@ class Config(metaclass=Singleton): self.use_mac_os_tts = False self.use_mac_os_tts = os.getenv("USE_MAC_OS_TTS") + self.chat_messages_enabled = os.getenv("CHAT_MESSAGES_ENABLED") == "True" + self.use_brian_tts = False self.use_brian_tts = os.getenv("USE_BRIAN_TTS") diff --git a/autogpt/logs.py b/autogpt/logs.py index 3e231d8d..fc529f25 100644 --- a/autogpt/logs.py +++ b/autogpt/logs.py @@ -12,6 +12,7 @@ from colorama import Fore, Style from autogpt.singleton import Singleton from autogpt.speech import say_text +from autogpt.utils import send_chat_message_to_user class Logger(metaclass=Singleton): @@ -84,6 +85,8 @@ class Logger(metaclass=Singleton): if speak_text and self.speak_mode: say_text(f"{title}. {content}") + send_chat_message_to_user(f"{title}. {content}") + if content: if isinstance(content, list): content = " ".join(content) diff --git a/autogpt/utils.py b/autogpt/utils.py index c8553eaf..17be611a 100644 --- a/autogpt/utils.py +++ b/autogpt/utils.py @@ -11,10 +11,56 @@ try: except: pass +from autogpt.config import Config -def clean_input(prompt: str = ""): + +def send_chat_message_to_user(report: str): + cfg = Config() + if not cfg.chat_messages_enabled: + return + for plugin in cfg.plugins: + if not hasattr(plugin, "can_handle_report"): + continue + if not plugin.can_handle_report(): + continue + plugin.report(report) + + +def clean_input(prompt: str = "", talk=False): try: - return input(prompt) + cfg = Config() + if cfg.chat_messages_enabled: + for plugin in cfg.plugins: + if not hasattr(plugin, "can_handle_user_input"): + continue + if not plugin.can_handle_user_input(user_input=prompt): + continue + plugin_response = plugin.user_input(user_input=prompt) + if not plugin_response: + continue + if plugin_response.lower() in [ + "yes", + "yeah", + "y", + "ok", + "okay", + "sure", + "alright", + ]: + return "y" + elif plugin_response.lower() in [ + "no", + "nope", + "n", + "negative", + ]: + return "n" + return plugin_response + + # ask for input, default when just pressing Enter is y + print("Asking user via keyboard...") + answer = input(prompt) + return answer except KeyboardInterrupt: print("You interrupted Auto-GPT") print("Quitting...")