wrap infinite loop in class agent

This commit is contained in:
Merwane Hamadi
2023-04-13 15:19:41 -07:00
parent a3024ca80d
commit c59b6b5543

View File

@@ -351,6 +351,44 @@ def main():
# this is particularly important for indexing and referencing pinecone memory # this is particularly important for indexing and referencing pinecone memory
memory = get_memory(cfg, init=True) memory = get_memory(cfg, init=True)
print('Using memory of type: ' + memory.__class__.__name__) print('Using memory of type: ' + memory.__class__.__name__)
agent = Agent(
ai_name=ai_name,
memory=memory,
full_message_history=full_message_history,
next_action_count=next_action_count,
prompt=prompt,
user_input=user_input
)
agent.start_interaction_loop()
class Agent:
"""Agent class for interacting with Auto-GPT.
Attributes:
ai_name: The name of the agent.
memory: The memory object to use.
full_message_history: The full message history.
next_action_count: The number of actions to execute.
prompt: The prompt to use.
user_input: The user input.
"""
def __init__(self,
ai_name,
memory,
full_message_history,
next_action_count,
prompt,
user_input):
self.ai_name = ai_name
self.memory = memory
self.full_message_history = full_message_history
self.next_action_count = next_action_count
self.prompt = prompt
self.user_input = user_input
def start_interaction_loop(self):
# Interaction Loop # Interaction Loop
loop_count = 0 loop_count = 0
while True: while True:
@@ -363,10 +401,10 @@ def main():
# Send message to AI, get response # Send message to AI, get response
with Spinner("Thinking... "): with Spinner("Thinking... "):
assistant_reply = chat.chat_with_ai( assistant_reply = chat.chat_with_ai(
prompt, self.prompt,
user_input, self.user_input,
full_message_history, self.full_message_history,
memory, self.memory,
cfg.fast_token_limit) # TODO: This hardcodes the model to use GPT3.5. Make this an argument cfg.fast_token_limit) # TODO: This hardcodes the model to use GPT3.5. Make this an argument
# Print Assistant thoughts # Print Assistant thoughts
@@ -381,45 +419,45 @@ def main():
except Exception as e: except Exception as e:
logger.error("Error: \n", str(e)) logger.error("Error: \n", str(e))
if not cfg.continuous_mode and next_action_count == 0: if not cfg.continuous_mode and self.next_action_count == 0:
### GET USER AUTHORIZATION TO EXECUTE COMMAND ### ### GET USER AUTHORIZATION TO EXECUTE COMMAND ###
# Get key press: Prompt the user to press enter to continue or escape # Get key press: Prompt the user to press enter to continue or escape
# to exit # to exit
user_input = "" self.user_input = ""
logger.typewriter_log( logger.typewriter_log(
"NEXT ACTION: ", "NEXT ACTION: ",
Fore.CYAN, Fore.CYAN,
f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}") f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}")
print( print(
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 {self.ai_name}...",
flush=True) flush=True)
while True: while True:
console_input = utils.clean_input(Fore.MAGENTA + "Input:" + Style.RESET_ALL) console_input = utils.clean_input(Fore.MAGENTA + "Input:" + Style.RESET_ALL)
if console_input.lower().rstrip() == "y": if console_input.lower().rstrip() == "y":
user_input = "GENERATE NEXT COMMAND JSON" self.user_input = "GENERATE NEXT COMMAND JSON"
break break
elif console_input.lower().startswith("y -"): elif console_input.lower().startswith("y -"):
try: try:
next_action_count = abs(int(console_input.split(" ")[1])) self.next_action_count = abs(int(console_input.split(" ")[1]))
user_input = "GENERATE NEXT COMMAND JSON" self.user_input = "GENERATE NEXT COMMAND JSON"
except ValueError: except ValueError:
print("Invalid input format. Please enter 'y -n' where n is the number of continuous tasks.") print("Invalid input format. Please enter 'y -n' where n is the number of continuous tasks.")
continue continue
break break
elif console_input.lower() == "n": elif console_input.lower() == "n":
user_input = "EXIT" self.user_input = "EXIT"
break break
else: else:
user_input = console_input self.user_input = console_input
command_name = "human_feedback" command_name = "human_feedback"
break break
if user_input == "GENERATE NEXT COMMAND JSON": if self.user_input == "GENERATE NEXT COMMAND JSON":
logger.typewriter_log( logger.typewriter_log(
"-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=", "-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=",
Fore.MAGENTA, Fore.MAGENTA,
"") "")
elif user_input == "EXIT": elif self.user_input == "EXIT":
print("Exiting...", flush=True) print("Exiting...", flush=True)
break break
else: else:
@@ -433,25 +471,25 @@ def main():
if command_name is not None and command_name.lower().startswith("error"): if command_name is not None and command_name.lower().startswith("error"):
result = f"Command {command_name} threw the following error: " + arguments result = f"Command {command_name} threw the following error: " + arguments
elif command_name == "human_feedback": elif command_name == "human_feedback":
result = f"Human feedback: {user_input}" result = f"Human feedback: {self.user_input}"
else: else:
result = f"Command {command_name} returned: {cmd.execute_command(command_name, arguments)}" result = f"Command {command_name} returned: {cmd.execute_command(command_name, arguments)}"
if next_action_count > 0: if self.next_action_count > 0:
next_action_count -= 1 self.next_action_count -= 1
memory_to_add = f"Assistant Reply: {assistant_reply} " \ memory_to_add = f"Assistant Reply: {assistant_reply} " \
f"\nResult: {result} " \ f"\nResult: {result} " \
f"\nHuman Feedback: {user_input} " f"\nHuman Feedback: {self.user_input} "
memory.add(memory_to_add) self.memory.add(memory_to_add)
# Check if there's a result from the command append it to the message # Check if there's a result from the command append it to the message
# history # history
if result is not None: if result is not None:
full_message_history.append(chat.create_chat_message("system", result)) self.full_message_history.append(chat.create_chat_message("system", result))
logger.typewriter_log("SYSTEM: ", Fore.YELLOW, result) logger.typewriter_log("SYSTEM: ", Fore.YELLOW, result)
else: else:
full_message_history.append( self.full_message_history.append(
chat.create_chat_message( chat.create_chat_message(
"system", "Unable to execute command")) "system", "Unable to execute command"))
logger.typewriter_log("SYSTEM: ", Fore.YELLOW, "Unable to execute command") logger.typewriter_log("SYSTEM: ", Fore.YELLOW, "Unable to execute command")