From 632686cfa5f5590642016bbc0f976a037ed3d361 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Wed, 20 Mar 2024 13:18:21 +0100 Subject: fix(agent): Replace `PromptToolkit` with `click.prompt` - Replace `session.prompt_async(..)` with `click.prompt(..)` in `clean_input` (autogpt/app/utils.py) - Convert `clean_input` back to a synchronous function (and amend its usages accordingly) - Remove `prompt-toolkit` dependency This mitigates issues crashes in some shell environments on Windows. --- autogpts/autogpt/autogpt/app/main.py | 14 +++++++------- autogpts/autogpt/autogpt/app/setup.py | 20 +++++++++----------- autogpts/autogpt/autogpt/app/utils.py | 14 +++++--------- .../autogpt/autogpt/commands/user_interaction.py | 2 +- autogpts/autogpt/poetry.lock | 4 ++-- autogpts/autogpt/pyproject.toml | 1 - 6 files changed, 24 insertions(+), 31 deletions(-) diff --git a/autogpts/autogpt/autogpt/app/main.py b/autogpts/autogpt/autogpt/app/main.py index 39524ee70..976ecfabf 100644 --- a/autogpts/autogpt/autogpt/app/main.py +++ b/autogpts/autogpt/autogpt/app/main.py @@ -180,7 +180,7 @@ async def run_auto_gpt( "Existing agents\n---------------\n" + "\n".join(f"{i} - {id}" for i, id in enumerate(existing_agents, 1)) ) - load_existing_agent = await clean_input( + load_existing_agent = clean_input( config, "Enter the number or name of the agent to run," " or hit enter to create a new one:", @@ -205,7 +205,7 @@ async def run_auto_gpt( if load_existing_agent: agent_state = None while True: - answer = await clean_input(config, "Resume? [Y/n]") + answer = clean_input(config, "Resume? [Y/n]") if answer == "" or answer.lower() == "y": agent_state = agent_manager.load_agent_state(load_existing_agent) break @@ -238,7 +238,7 @@ async def run_auto_gpt( # Agent was resumed after `finish` -> rewrite result of `finish` action finish_reason = agent.event_history.current_episode.action.args["reason"] print(f"Agent previously self-terminated; reason: '{finish_reason}'") - new_assignment = await clean_input( + new_assignment = clean_input( config, "Please give a follow-up question or assignment:" ) agent.event_history.register_result( @@ -270,7 +270,7 @@ async def run_auto_gpt( if not agent: task = "" while task.strip() == "": - task = await clean_input( + task = clean_input( config, "Enter the task that you want AutoGPT to execute," " with as much detail as possible:", @@ -343,7 +343,7 @@ async def run_auto_gpt( # Allow user to Save As other ID save_as_id = ( - await clean_input( + clean_input( config, f"Press enter to save as '{agent_id}'," " or enter a different ID to save to:", @@ -725,9 +725,9 @@ async def get_user_feedback( while user_feedback is None: # Get input from user if config.chat_messages_enabled: - console_input = await clean_input(config, "Waiting for your response...") + console_input = clean_input(config, "Waiting for your response...") else: - console_input = await clean_input( + console_input = clean_input( config, Fore.MAGENTA + "Input:" + Style.RESET_ALL ) diff --git a/autogpts/autogpt/autogpt/app/setup.py b/autogpts/autogpt/autogpt/app/setup.py index df4e9436c..94460e62f 100644 --- a/autogpts/autogpt/autogpt/app/setup.py +++ b/autogpts/autogpt/autogpt/app/setup.py @@ -69,20 +69,18 @@ async def interactively_revise_ai_settings( ) if ( - await clean_input(app_config, "Continue with these settings? [Y/n]") + clean_input(app_config, "Continue with these settings? [Y/n]").lower() or app_config.authorise_key ) == app_config.authorise_key: break # Ask for revised ai_profile ai_profile.ai_name = ( - await clean_input( - app_config, "Enter AI name (or press enter to keep current):" - ) + clean_input(app_config, "Enter AI name (or press enter to keep current):") or ai_profile.ai_name ) ai_profile.ai_role = ( - await clean_input( + clean_input( app_config, "Enter new AI role (or press enter to keep current):" ) or ai_profile.ai_role @@ -94,7 +92,7 @@ async def interactively_revise_ai_settings( constraint = directives.constraints[i] print_attribute(f"Constraint {i+1}:", f'"{constraint}"') new_constraint = ( - await clean_input( + clean_input( app_config, f"Enter new constraint {i+1}" " (press enter to keep current, or '-' to remove):", @@ -112,7 +110,7 @@ async def interactively_revise_ai_settings( # Add new constraints while True: - new_constraint = await clean_input( + new_constraint = clean_input( app_config, "Press enter to finish, or enter a constraint to add:", ) @@ -126,7 +124,7 @@ async def interactively_revise_ai_settings( resource = directives.resources[i] print_attribute(f"Resource {i+1}:", f'"{resource}"') new_resource = ( - await clean_input( + clean_input( app_config, f"Enter new resource {i+1}" " (press enter to keep current, or '-' to remove):", @@ -143,7 +141,7 @@ async def interactively_revise_ai_settings( # Add new resources while True: - new_resource = await clean_input( + new_resource = clean_input( app_config, "Press enter to finish, or enter a resource to add:", ) @@ -157,7 +155,7 @@ async def interactively_revise_ai_settings( best_practice = directives.best_practices[i] print_attribute(f"Best Practice {i+1}:", f'"{best_practice}"') new_best_practice = ( - await clean_input( + clean_input( app_config, f"Enter new best practice {i+1}" " (press enter to keep current, or '-' to remove):", @@ -174,7 +172,7 @@ async def interactively_revise_ai_settings( # Add new best practices while True: - new_best_practice = await clean_input( + new_best_practice = clean_input( app_config, "Press enter to finish, or add a best practice to add:", ) diff --git a/autogpts/autogpt/autogpt/app/utils.py b/autogpts/autogpt/autogpt/app/utils.py index 3e986852b..49a1e2a44 100644 --- a/autogpts/autogpt/autogpt/app/utils.py +++ b/autogpts/autogpt/autogpt/app/utils.py @@ -7,20 +7,18 @@ import sys from pathlib import Path from typing import TYPE_CHECKING +import click import requests from colorama import Fore, Style from git import InvalidGitRepositoryError, Repo -from prompt_toolkit import ANSI, PromptSession -from prompt_toolkit.history import InMemoryHistory if TYPE_CHECKING: from autogpt.config import Config logger = logging.getLogger(__name__) -session = PromptSession(history=InMemoryHistory()) -async def clean_input(config: "Config", prompt: str = ""): +def clean_input(config: "Config", prompt: str = ""): try: if config.chat_messages_enabled: for plugin in config.plugins: @@ -53,11 +51,9 @@ async def clean_input(config: "Config", prompt: str = ""): # ask for input, default when just pressing Enter is y logger.debug("Asking user via keyboard...") - # handle_sigint must be set to False, so the signal handler in the - # autogpt/main.py could be employed properly. This referes to - # https://github.com/Significant-Gravitas/AutoGPT/pull/4799/files/3966cdfd694c2a80c0333823c3bc3da090f85ed3#r1264278776 - answer = await session.prompt_async(ANSI(prompt + " "), handle_sigint=False) - return answer + return click.prompt( + text=prompt, prompt_suffix=" ", default="", show_default=False + ) except KeyboardInterrupt: logger.info("You interrupted AutoGPT") logger.info("Quitting...") diff --git a/autogpts/autogpt/autogpt/commands/user_interaction.py b/autogpts/autogpt/autogpt/commands/user_interaction.py index c53213c7f..bd4dd639c 100644 --- a/autogpts/autogpt/autogpt/commands/user_interaction.py +++ b/autogpts/autogpt/autogpt/commands/user_interaction.py @@ -28,5 +28,5 @@ COMMAND_CATEGORY_TITLE = "User Interaction" ) async def ask_user(question: str, agent: Agent) -> str: print(f"\nQ: {question}") - resp = await clean_input(agent.legacy_config, "A:") + resp = clean_input(agent.legacy_config, "A:") return f"The user's answer: '{resp}'" diff --git a/autogpts/autogpt/poetry.lock b/autogpts/autogpt/poetry.lock index 176be5e63..6ee147a45 100644 --- a/autogpts/autogpt/poetry.lock +++ b/autogpts/autogpt/poetry.lock @@ -4589,7 +4589,7 @@ files = [ name = "prompt-toolkit" version = "3.0.43" description = "Library for building powerful interactive command lines in Python" -optional = false +optional = true python-versions = ">=3.7.0" files = [ {file = "prompt_toolkit-3.0.43-py3-none-any.whl", hash = "sha256:a11a29cb3bf0a28a387fe5122cdb649816a957cd9261dcedf8c9f1fef33eacf6"}, @@ -7248,4 +7248,4 @@ benchmark = ["agbenchmark"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "f693821e204aaf64d946ac45d7c0e447f02c2a3f173f939dac2fcc8e797a142f" +content-hash = "a09e20daaf94e05457ded6a9989585cd37edf96a036c5f9e505f0b2456403a25" diff --git a/autogpts/autogpt/pyproject.toml b/autogpts/autogpt/pyproject.toml index 919e11018..ad138ee24 100644 --- a/autogpts/autogpt/pyproject.toml +++ b/autogpts/autogpt/pyproject.toml @@ -48,7 +48,6 @@ orjson = "^3.8.10" Pillow = "*" pinecone-client = "^2.2.1" playsound = "~1.2.2" -prompt_toolkit = "^3.0.38" pydantic = "*" pylatexenc = "*" pypdf = "^3.1.0" -- cgit v1.2.3