Merge pull request #633 from Sma-Das/clean_input

[Utility] Created clean_input to cleanly exit KeyboardInterrupts
pull/688/head^2
Toran Bruce Richards 2023-04-10 22:39:45 +01:00 committed by GitHub
commit 0eab8d1087
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 9 deletions

View File

@ -1,6 +1,7 @@
import json import json
import random import random
import commands as cmd import commands as cmd
import utils
from memory import get_memory from memory import get_memory
import data import data
import chat import chat
@ -143,12 +144,12 @@ def load_variables(config_file="config.yaml"):
# Prompt the user for input if config file is missing or empty values # Prompt the user for input if config file is missing or empty values
if not ai_name: if not ai_name:
ai_name = input("Name your AI: ") ai_name = utils.clean_input("Name your AI: ")
if ai_name == "": if ai_name == "":
ai_name = "Entrepreneur-GPT" ai_name = "Entrepreneur-GPT"
if not ai_role: if not ai_role:
ai_role = input(f"{ai_name} is: ") ai_role = utils.clean_input(f"{ai_name} is: ")
if ai_role == "": if ai_role == "":
ai_role = "an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth." ai_role = "an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth."
@ -158,7 +159,7 @@ def load_variables(config_file="config.yaml"):
print("Enter nothing to load defaults, enter nothing when finished.") print("Enter nothing to load defaults, enter nothing when finished.")
ai_goals = [] ai_goals = []
for i in range(5): for i in range(5):
ai_goal = input(f"Goal {i+1}: ") ai_goal = utils.clean_input(f"Goal {i+1}: ")
if ai_goal == "": if ai_goal == "":
break break
ai_goals.append(ai_goal) ai_goals.append(ai_goal)
@ -191,7 +192,7 @@ def construct_prompt():
Fore.GREEN, Fore.GREEN,
f"Would you like me to return to being {config.ai_name}?", f"Would you like me to return to being {config.ai_name}?",
speak_text=True) speak_text=True)
should_continue = input(f"""Continue with the last settings? should_continue = utils.clean_input(f"""Continue with the last settings?
Name: {config.ai_name} Name: {config.ai_name}
Role: {config.ai_role} Role: {config.ai_role}
Goals: {config.ai_goals} Goals: {config.ai_goals}
@ -226,7 +227,7 @@ def prompt_user():
"Name your AI: ", "Name your AI: ",
Fore.GREEN, Fore.GREEN,
"For example, 'Entrepreneur-GPT'") "For example, 'Entrepreneur-GPT'")
ai_name = input("AI Name: ") ai_name = utils.clean_input("AI Name: ")
if ai_name == "": if ai_name == "":
ai_name = "Entrepreneur-GPT" ai_name = "Entrepreneur-GPT"
@ -241,7 +242,7 @@ def prompt_user():
"Describe your AI's role: ", "Describe your AI's role: ",
Fore.GREEN, Fore.GREEN,
"For example, 'an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth.'") "For example, 'an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth.'")
ai_role = input(f"{ai_name} is: ") ai_role = utils.clean_input(f"{ai_name} is: ")
if ai_role == "": if ai_role == "":
ai_role = "an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth." ai_role = "an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth."
@ -253,7 +254,7 @@ def prompt_user():
print("Enter nothing to load defaults, enter nothing when finished.", flush=True) print("Enter nothing to load defaults, enter nothing when finished.", flush=True)
ai_goals = [] ai_goals = []
for i in range(5): for i in range(5):
ai_goal = input(f"{Fore.LIGHTBLUE_EX}Goal{Style.RESET_ALL} {i+1}: ") ai_goal = utils.clean_input(f"{Fore.LIGHTBLUE_EX}Goal{Style.RESET_ALL} {i+1}: ")
if ai_goal == "": if ai_goal == "":
break break
ai_goals.append(ai_goal) ai_goals.append(ai_goal)
@ -355,7 +356,7 @@ while True:
f"Enter 'y' to authorise command, 'y -N' to run N continuous commands, 'n' to exit program, or enter feedback for {ai_name}...", f"Enter 'y' to authorise command, 'y -N' to run N continuous commands, 'n' to exit program, or enter feedback for {ai_name}...",
flush=True) flush=True)
while True: while True:
console_input = input(Fore.MAGENTA + "Input:" + Style.RESET_ALL) console_input = utils.clean_input(Fore.MAGENTA + "Input:" + Style.RESET_ALL)
if console_input.lower() == "y": if console_input.lower() == "y":
user_input = "GENERATE NEXT COMMAND JSON" user_input = "GENERATE NEXT COMMAND JSON"
break break

8
scripts/utils.py Normal file
View File

@ -0,0 +1,8 @@
def clean_input(prompt: str=''):
try:
return input(prompt)
except KeyboardInterrupt:
print("You interrupted Auto-GPT")
print("Quitting...")
exit(0)