Skip to content

Commit

Permalink
fix(agent): Replace PromptToolkit with click.prompt
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Pwuts committed Mar 20, 2024
1 parent 1262b72 commit 632686c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 31 deletions.
14 changes: 7 additions & 7 deletions autogpts/autogpt/autogpt/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:",
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:",
Expand Down Expand Up @@ -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:",
Expand Down Expand Up @@ -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
)

Expand Down
20 changes: 9 additions & 11 deletions autogpts/autogpt/autogpt/app/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):",
Expand All @@ -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:",
)
Expand All @@ -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):",
Expand All @@ -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:",
)
Expand All @@ -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):",
Expand All @@ -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:",
)
Expand Down
14 changes: 5 additions & 9 deletions autogpts/autogpt/autogpt/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
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:
Expand Down Expand Up @@ -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...")
Expand Down
2 changes: 1 addition & 1 deletion autogpts/autogpt/autogpt/commands/user_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
)
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}'"
4 changes: 2 additions & 2 deletions autogpts/autogpt/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion autogpts/autogpt/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 632686c

Please sign in to comment.