put loop in in if main

This commit is contained in:
Merwane Hamadi
2023-04-13 05:57:21 -07:00
parent c8b8673286
commit b112f5ebfa

View File

@@ -316,118 +316,123 @@ def parse_arguments():
cfg.memory_backend = chosen cfg.memory_backend = chosen
# TODO: fill in llm values here def main():
check_openai_api_key() global ai_name, memory
parse_arguments() # TODO: fill in llm values here
logger.set_level(logging.DEBUG if cfg.debug_mode else logging.INFO) check_openai_api_key()
ai_name = "" parse_arguments()
prompt = construct_prompt() logger.set_level(logging.DEBUG if cfg.debug_mode else logging.INFO)
# print(prompt) ai_name = ""
# Initialize variables prompt = construct_prompt()
full_message_history = [] # print(prompt)
result = None # Initialize variables
next_action_count = 0 full_message_history = []
# Make a constant: result = None
user_input = "Determine which next command to use, and respond using the format specified above:" next_action_count = 0
# Make a constant:
user_input = "Determine which next command to use, and respond using the format specified above:"
# Initialize memory and make sure it is empty.
# this is particularly important for indexing and referencing pinecone memory
memory = get_memory(cfg, init=True)
print('Using memory of type: ' + memory.__class__.__name__)
# Interaction Loop
while True:
# Send message to AI, get response
with Spinner("Thinking... "):
assistant_reply = chat.chat_with_ai(
prompt,
user_input,
full_message_history,
memory,
cfg.fast_token_limit) # TODO: This hardcodes the model to use GPT3.5. Make this an argument
# Initialize memory and make sure it is empty. # Print Assistant thoughts
# this is particularly important for indexing and referencing pinecone memory print_assistant_thoughts(assistant_reply)
memory = get_memory(cfg, init=True)
print('Using memory of type: ' + memory.__class__.__name__)
# Interaction Loop # Get command name and arguments
while True: try:
# Send message to AI, get response command_name, arguments = cmd.get_command(
with Spinner("Thinking... "): attempt_to_fix_json_by_finding_outermost_brackets(assistant_reply))
assistant_reply = chat.chat_with_ai( if cfg.speak_mode:
prompt, speak.say_text(f"I want to execute {command_name}")
user_input, except Exception as e:
full_message_history, logger.error("Error: \n", str(e))
memory,
cfg.fast_token_limit) # TODO: This hardcodes the model to use GPT3.5. Make this an argument
# Print Assistant thoughts if not cfg.continuous_mode and next_action_count == 0:
print_assistant_thoughts(assistant_reply) ### GET USER AUTHORIZATION TO EXECUTE COMMAND ###
# Get key press: Prompt the user to press enter to continue or escape
# Get command name and arguments # to exit
try: user_input = ""
command_name, arguments = cmd.get_command(attempt_to_fix_json_by_finding_outermost_brackets(assistant_reply))
if cfg.speak_mode:
speak.say_text(f"I want to execute {command_name}")
except Exception as e:
logger.error("Error: \n", str(e))
if not cfg.continuous_mode and next_action_count == 0:
### GET USER AUTHORIZATION TO EXECUTE COMMAND ###
# Get key press: Prompt the user to press enter to continue or escape
# to exit
user_input = ""
logger.typewriter_log(
"NEXT ACTION: ",
Fore.CYAN,
f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}")
print(
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)
while True:
console_input = utils.clean_input(Fore.MAGENTA + "Input:" + Style.RESET_ALL)
if console_input.lower().rstrip() == "y":
user_input = "GENERATE NEXT COMMAND JSON"
break
elif console_input.lower().startswith("y -"):
try:
next_action_count = abs(int(console_input.split(" ")[1]))
user_input = "GENERATE NEXT COMMAND JSON"
except ValueError:
print("Invalid input format. Please enter 'y -n' where n is the number of continuous tasks.")
continue
break
elif console_input.lower() == "n":
user_input = "EXIT"
break
else:
user_input = console_input
command_name = "human_feedback"
break
if user_input == "GENERATE NEXT COMMAND JSON":
logger.typewriter_log( logger.typewriter_log(
"-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=", "NEXT ACTION: ",
Fore.MAGENTA, Fore.CYAN,
"") f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}")
elif user_input == "EXIT": print(
print("Exiting...", flush=True) f"Enter 'y' to authorise command, 'y -N' to run N continuous commands, 'n' to exit program, or enter feedback for {ai_name}...",
break flush=True)
else: while True:
# Print command console_input = utils.clean_input(Fore.MAGENTA + "Input:" + Style.RESET_ALL)
logger.typewriter_log( if console_input.lower().rstrip() == "y":
"NEXT ACTION: ", user_input = "GENERATE NEXT COMMAND JSON"
Fore.CYAN, break
f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}") elif console_input.lower().startswith("y -"):
try:
next_action_count = abs(int(console_input.split(" ")[1]))
user_input = "GENERATE NEXT COMMAND JSON"
except ValueError:
print("Invalid input format. Please enter 'y -n' where n is the number of continuous tasks.")
continue
break
elif console_input.lower() == "n":
user_input = "EXIT"
break
else:
user_input = console_input
command_name = "human_feedback"
break
# Execute command if user_input == "GENERATE NEXT COMMAND JSON":
if command_name is not None and command_name.lower().startswith( "error" ): logger.typewriter_log(
result = f"Command {command_name} threw the following error: " + arguments "-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=",
elif command_name == "human_feedback": Fore.MAGENTA,
result = f"Human feedback: {user_input}" "")
else: elif user_input == "EXIT":
result = f"Command {command_name} returned: {cmd.execute_command(command_name, arguments)}" print("Exiting...", flush=True)
if next_action_count > 0: break
next_action_count -= 1 else:
# Print command
logger.typewriter_log(
"NEXT ACTION: ",
Fore.CYAN,
f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}")
memory_to_add = f"Assistant Reply: {assistant_reply} " \ # Execute command
f"\nResult: {result} " \ if command_name is not None and command_name.lower().startswith("error"):
f"\nHuman Feedback: {user_input} " result = f"Command {command_name} threw the following error: " + arguments
elif command_name == "human_feedback":
result = f"Human feedback: {user_input}"
else:
result = f"Command {command_name} returned: {cmd.execute_command(command_name, arguments)}"
if next_action_count > 0:
next_action_count -= 1
memory.add(memory_to_add) memory_to_add = f"Assistant Reply: {assistant_reply} " \
f"\nResult: {result} " \
f"\nHuman Feedback: {user_input} "
# Check if there's a result from the command append it to the message memory.add(memory_to_add)
# history
if result is not None: # Check if there's a result from the command append it to the message
full_message_history.append(chat.create_chat_message("system", result)) # history
logger.typewriter_log("SYSTEM: ", Fore.YELLOW, result) if result is not None:
else: full_message_history.append(chat.create_chat_message("system", result))
full_message_history.append( logger.typewriter_log("SYSTEM: ", Fore.YELLOW, result)
chat.create_chat_message( else:
"system", "Unable to execute command")) full_message_history.append(
logger.typewriter_log("SYSTEM: ", Fore.YELLOW, "Unable to execute command") chat.create_chat_message(
"system", "Unable to execute command"))
logger.typewriter_log("SYSTEM: ", Fore.YELLOW, "Unable to execute command")
if __name__ == "__main__":
main()