2023-04-15 12:56:23 +00:00
|
|
|
""" Command and Control """
|
2023-04-14 19:42:28 +00:00
|
|
|
import json
|
2023-04-15 12:56:23 +00:00
|
|
|
from typing import List, NoReturn, Union
|
|
|
|
from autogpt.agent.agent_manager import AgentManager
|
|
|
|
from autogpt.commands.evaluate_code import evaluate_code
|
|
|
|
from autogpt.commands.google_search import google_official_search, google_search
|
|
|
|
from autogpt.commands.improve_code import improve_code
|
|
|
|
from autogpt.commands.write_tests import write_tests
|
2023-04-14 22:18:36 +00:00
|
|
|
from autogpt.config import Config
|
2023-04-15 12:56:23 +00:00
|
|
|
from autogpt.commands.image_gen import generate_image
|
2023-04-15 21:19:20 +00:00
|
|
|
from autogpt.commands.audio_text import read_audio_from_file
|
2023-04-15 12:56:23 +00:00
|
|
|
from autogpt.commands.web_requests import scrape_links, scrape_text
|
|
|
|
from autogpt.commands.execute_code import execute_python_file, execute_shell
|
|
|
|
from autogpt.commands.file_operations import (
|
2023-04-15 00:04:48 +00:00
|
|
|
append_to_file,
|
|
|
|
delete_file,
|
|
|
|
read_file,
|
|
|
|
search_files,
|
|
|
|
write_to_file,
|
|
|
|
)
|
2023-04-15 12:56:23 +00:00
|
|
|
from autogpt.json_fixes.parsing import fix_and_parse_json
|
2023-04-15 00:04:48 +00:00
|
|
|
from autogpt.memory import get_memory
|
2023-04-15 12:56:23 +00:00
|
|
|
from autogpt.processing.text import summarize_text
|
|
|
|
from autogpt.speech import say_text
|
|
|
|
from autogpt.commands.web_selenium import browse_website
|
2023-04-15 14:59:40 +00:00
|
|
|
from autogpt.commands.git_operations import clone_repository
|
2023-04-15 20:58:22 +00:00
|
|
|
from autogpt.commands.twitter import send_tweet
|
2023-04-15 00:04:48 +00:00
|
|
|
|
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
CFG = Config()
|
|
|
|
AGENT_MANAGER = AgentManager()
|
2023-03-30 09:04:09 +00:00
|
|
|
|
2023-03-28 22:25:42 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
def is_valid_int(value: str) -> bool:
|
|
|
|
"""Check if the value is a valid integer
|
|
|
|
|
|
|
|
Args:
|
|
|
|
value (str): The value to check
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the value is a valid integer, False otherwise
|
|
|
|
"""
|
2023-04-04 20:53:59 +00:00
|
|
|
try:
|
|
|
|
int(value)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
2023-04-12 21:05:14 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
def get_command(response: str):
|
|
|
|
"""Parse the response and return the command name and arguments
|
|
|
|
|
|
|
|
Args:
|
|
|
|
response (str): The response from the user
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
tuple: The command name and arguments
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
json.decoder.JSONDecodeError: If the response is not valid JSON
|
|
|
|
|
|
|
|
Exception: If any other error occurs
|
|
|
|
"""
|
2023-03-29 01:40:46 +00:00
|
|
|
try:
|
2023-04-02 23:50:51 +00:00
|
|
|
response_json = fix_and_parse_json(response)
|
2023-04-09 12:58:05 +00:00
|
|
|
|
2023-04-03 10:30:06 +00:00
|
|
|
if "command" not in response_json:
|
2023-04-14 19:42:28 +00:00
|
|
|
return "Error:", "Missing 'command' object in JSON"
|
2023-04-09 12:58:05 +00:00
|
|
|
|
2023-04-15 00:04:48 +00:00
|
|
|
if not isinstance(response_json, dict):
|
|
|
|
return "Error:", f"'response_json' object is not dictionary {response_json}"
|
|
|
|
|
2023-03-29 01:40:46 +00:00
|
|
|
command = response_json["command"]
|
2023-04-15 00:04:48 +00:00
|
|
|
if not isinstance(command, dict):
|
|
|
|
return "Error:", "'command' object is not a dictionary"
|
2023-04-03 10:30:06 +00:00
|
|
|
|
|
|
|
if "name" not in command:
|
|
|
|
return "Error:", "Missing 'name' field in 'command' object"
|
2023-04-09 12:58:05 +00:00
|
|
|
|
2023-03-29 01:40:46 +00:00
|
|
|
command_name = command["name"]
|
2023-04-03 10:30:06 +00:00
|
|
|
|
|
|
|
# Use an empty dictionary if 'args' field is not present in 'command' object
|
|
|
|
arguments = command.get("args", {})
|
2023-03-29 01:40:46 +00:00
|
|
|
|
|
|
|
return command_name, arguments
|
|
|
|
except json.decoder.JSONDecodeError:
|
2023-03-30 11:46:36 +00:00
|
|
|
return "Error:", "Invalid JSON"
|
2023-03-29 01:40:46 +00:00
|
|
|
# All other errors, return "Error: + error message"
|
|
|
|
except Exception as e:
|
2023-03-30 11:46:36 +00:00
|
|
|
return "Error:", str(e)
|
2023-03-29 01:40:46 +00:00
|
|
|
|
2023-04-02 08:13:15 +00:00
|
|
|
|
2023-04-15 17:25:45 +00:00
|
|
|
def map_command_synonyms(command_name: str):
|
2023-04-15 19:55:13 +00:00
|
|
|
"""Takes the original command name given by the AI, and checks if the
|
|
|
|
string matches a list of common/known hallucinations
|
2023-04-15 17:25:45 +00:00
|
|
|
"""
|
|
|
|
synonyms = [
|
2023-04-15 19:55:13 +00:00
|
|
|
("write_file", "write_to_file"),
|
|
|
|
("create_file", "write_to_file"),
|
|
|
|
("search", "google"),
|
2023-04-15 17:25:45 +00:00
|
|
|
]
|
|
|
|
for seen_command, actual_command_name in synonyms:
|
|
|
|
if command_name == seen_command:
|
|
|
|
return actual_command_name
|
|
|
|
return command_name
|
|
|
|
|
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
def execute_command(command_name: str, arguments):
|
|
|
|
"""Execute the command and return the result
|
|
|
|
|
|
|
|
Args:
|
|
|
|
command_name (str): The name of the command to execute
|
|
|
|
arguments (dict): The arguments for the command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The result of the command"""
|
|
|
|
memory = get_memory(CFG)
|
2023-04-07 23:13:18 +00:00
|
|
|
|
2023-03-29 01:40:46 +00:00
|
|
|
try:
|
2023-04-15 17:25:45 +00:00
|
|
|
command_name = map_command_synonyms(command_name)
|
2023-03-29 01:40:46 +00:00
|
|
|
if command_name == "google":
|
2023-04-03 20:44:10 +00:00
|
|
|
# Check if the Google API key is set and use the official search method
|
2023-04-15 00:04:48 +00:00
|
|
|
# If the API key is not set or has only whitespaces, use the unofficial
|
|
|
|
# search method
|
2023-04-15 12:56:23 +00:00
|
|
|
key = CFG.google_api_key
|
2023-04-14 20:35:19 +00:00
|
|
|
if key and key.strip() and key != "your-google-api-key":
|
2023-04-15 15:01:29 +00:00
|
|
|
google_result = google_official_search(arguments["input"])
|
2023-04-16 04:42:01 +00:00
|
|
|
return google_result
|
2023-04-03 20:44:10 +00:00
|
|
|
else:
|
2023-04-15 15:01:29 +00:00
|
|
|
google_result = google_search(arguments["input"])
|
2023-04-15 20:03:22 +00:00
|
|
|
safe_message = [google_result_single.encode('utf-8', 'ignore') for google_result_single in google_result]
|
|
|
|
return str(safe_message)
|
2023-03-29 01:40:46 +00:00
|
|
|
elif command_name == "memory_add":
|
2023-04-04 00:31:01 +00:00
|
|
|
return memory.add(arguments["string"])
|
2023-03-30 09:04:09 +00:00
|
|
|
elif command_name == "start_agent":
|
2023-04-02 08:13:15 +00:00
|
|
|
return start_agent(
|
2023-04-14 19:42:28 +00:00
|
|
|
arguments["name"], arguments["task"], arguments["prompt"]
|
|
|
|
)
|
2023-03-30 09:04:09 +00:00
|
|
|
elif command_name == "message_agent":
|
|
|
|
return message_agent(arguments["key"], arguments["message"])
|
|
|
|
elif command_name == "list_agents":
|
|
|
|
return list_agents()
|
|
|
|
elif command_name == "delete_agent":
|
|
|
|
return delete_agent(arguments["key"])
|
2023-03-30 09:06:31 +00:00
|
|
|
elif command_name == "get_text_summary":
|
2023-04-04 01:20:42 +00:00
|
|
|
return get_text_summary(arguments["url"], arguments["question"])
|
2023-03-30 09:10:52 +00:00
|
|
|
elif command_name == "get_hyperlinks":
|
|
|
|
return get_hyperlinks(arguments["url"])
|
2023-04-15 14:59:40 +00:00
|
|
|
elif command_name == "clone_repository":
|
2023-04-15 19:55:13 +00:00
|
|
|
return clone_repository(
|
|
|
|
arguments["repository_url"], arguments["clone_path"]
|
|
|
|
)
|
2023-04-01 03:08:30 +00:00
|
|
|
elif command_name == "read_file":
|
|
|
|
return read_file(arguments["file"])
|
2023-03-30 09:08:13 +00:00
|
|
|
elif command_name == "write_to_file":
|
2023-03-30 11:47:55 +00:00
|
|
|
return write_to_file(arguments["file"], arguments["text"])
|
2023-04-01 03:08:30 +00:00
|
|
|
elif command_name == "append_to_file":
|
|
|
|
return append_to_file(arguments["file"], arguments["text"])
|
|
|
|
elif command_name == "delete_file":
|
|
|
|
return delete_file(arguments["file"])
|
2023-04-05 01:32:15 +00:00
|
|
|
elif command_name == "search_files":
|
|
|
|
return search_files(arguments["directory"])
|
2023-03-30 11:47:55 +00:00
|
|
|
elif command_name == "browse_website":
|
2023-04-04 01:20:42 +00:00
|
|
|
return browse_website(arguments["url"], arguments["question"])
|
2023-04-02 08:13:15 +00:00
|
|
|
# TODO: Change these to take in a file rather than pasted code, if
|
|
|
|
# non-file is given, return instructions "Input should be a python
|
|
|
|
# filepath, write your code to file and try again"
|
2023-04-01 09:35:32 +00:00
|
|
|
elif command_name == "evaluate_code":
|
2023-04-15 00:04:48 +00:00
|
|
|
return evaluate_code(arguments["code"])
|
2023-04-01 09:35:32 +00:00
|
|
|
elif command_name == "improve_code":
|
2023-04-15 00:04:48 +00:00
|
|
|
return improve_code(arguments["suggestions"], arguments["code"])
|
2023-04-01 09:35:32 +00:00
|
|
|
elif command_name == "write_tests":
|
2023-04-15 00:04:48 +00:00
|
|
|
return write_tests(arguments["code"], arguments.get("focus"))
|
2023-04-01 15:01:36 +00:00
|
|
|
elif command_name == "execute_python_file": # Add this command
|
|
|
|
return execute_python_file(arguments["file"])
|
2023-04-13 04:04:26 +00:00
|
|
|
elif command_name == "execute_shell":
|
2023-04-15 12:56:23 +00:00
|
|
|
if CFG.execute_local_commands:
|
2023-04-13 04:04:26 +00:00
|
|
|
return execute_shell(arguments["command_line"])
|
2023-04-10 04:01:48 +00:00
|
|
|
else:
|
2023-04-15 00:04:48 +00:00
|
|
|
return (
|
|
|
|
"You are not allowed to run local shell commands. To execute"
|
|
|
|
" shell commands, EXECUTE_LOCAL_COMMANDS must be set to 'True' "
|
|
|
|
"in your config. Do not attempt to bypass the restriction."
|
|
|
|
)
|
2023-04-15 21:19:20 +00:00
|
|
|
elif command_name == "read_audio_from_file":
|
|
|
|
return read_audio_from_file(arguments["file"])
|
2023-04-08 11:27:05 +00:00
|
|
|
elif command_name == "generate_image":
|
2023-04-07 15:02:48 +00:00
|
|
|
return generate_image(arguments["prompt"])
|
2023-04-15 20:58:22 +00:00
|
|
|
elif command_name == "send_tweet":
|
2023-04-16 06:32:18 +00:00
|
|
|
return send_tweet(arguments["text"])
|
2023-04-09 08:28:36 +00:00
|
|
|
elif command_name == "do_nothing":
|
|
|
|
return "No action performed."
|
2023-03-30 09:10:05 +00:00
|
|
|
elif command_name == "task_complete":
|
|
|
|
shutdown()
|
2023-03-29 01:40:46 +00:00
|
|
|
else:
|
2023-04-15 00:04:48 +00:00
|
|
|
return (
|
|
|
|
f"Unknown command '{command_name}'. Please refer to the 'COMMANDS'"
|
|
|
|
" list for available commands and only respond in the specified JSON"
|
|
|
|
" format."
|
|
|
|
)
|
2023-03-29 01:40:46 +00:00
|
|
|
except Exception as e:
|
2023-04-15 12:56:23 +00:00
|
|
|
return f"Error: {str(e)}"
|
2023-03-31 03:15:14 +00:00
|
|
|
|
2023-03-28 22:25:42 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
def get_text_summary(url: str, question: str) -> str:
|
|
|
|
"""Return the results of a google search
|
2023-04-12 21:05:14 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
Args:
|
|
|
|
url (str): The url to scrape
|
|
|
|
question (str): The question to summarize the text for
|
2023-04-14 19:42:28 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
Returns:
|
|
|
|
str: The summary of the text
|
|
|
|
"""
|
2023-04-15 00:04:48 +00:00
|
|
|
text = scrape_text(url)
|
|
|
|
summary = summarize_text(url, text, question)
|
2023-04-15 12:56:23 +00:00
|
|
|
return f""" "Result" : {summary}"""
|
|
|
|
|
2023-03-28 22:25:42 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
def get_hyperlinks(url: str) -> Union[str, List[str]]:
|
|
|
|
"""Return the results of a google search
|
2023-04-02 08:13:15 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
Args:
|
|
|
|
url (str): The url to scrape
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str or list: The hyperlinks on the page
|
|
|
|
"""
|
2023-04-15 00:04:48 +00:00
|
|
|
return scrape_links(url)
|
2023-03-30 09:08:13 +00:00
|
|
|
|
2023-04-02 08:13:15 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
def shutdown() -> NoReturn:
|
2023-04-02 17:03:37 +00:00
|
|
|
"""Shut down the program"""
|
2023-03-30 09:08:13 +00:00
|
|
|
print("Shutting down...")
|
|
|
|
quit()
|
|
|
|
|
2023-04-02 08:13:15 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
def start_agent(name: str, task: str, prompt: str, model=CFG.fast_llm_model) -> str:
|
|
|
|
"""Start an agent with a given name, task, and prompt
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): The name of the agent
|
|
|
|
task (str): The task of the agent
|
|
|
|
prompt (str): The prompt for the agent
|
|
|
|
model (str): The model to use for the agent
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The response of the agent
|
|
|
|
"""
|
2023-03-31 03:15:14 +00:00
|
|
|
# Remove underscores from name
|
|
|
|
voice_name = name.replace("_", " ")
|
|
|
|
|
|
|
|
first_message = f"""You are {name}. Respond with: "Acknowledged"."""
|
|
|
|
agent_intro = f"{voice_name} here, Reporting for duty!"
|
2023-03-30 09:08:13 +00:00
|
|
|
|
2023-03-31 03:15:14 +00:00
|
|
|
# Create agent
|
2023-04-15 12:56:23 +00:00
|
|
|
if CFG.speak_mode:
|
2023-04-15 00:04:48 +00:00
|
|
|
say_text(agent_intro, 1)
|
2023-04-15 12:56:23 +00:00
|
|
|
key, ack = AGENT_MANAGER.create_agent(task, first_message, model)
|
2023-03-28 22:25:42 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
if CFG.speak_mode:
|
2023-04-15 00:04:48 +00:00
|
|
|
say_text(f"Hello {voice_name}. Your task is as follows. {task}.")
|
2023-03-28 22:25:42 +00:00
|
|
|
|
2023-03-31 03:15:14 +00:00
|
|
|
# Assign task (prompt), get response
|
2023-04-15 12:56:23 +00:00
|
|
|
agent_response = AGENT_MANAGER.message_agent(key, prompt)
|
2023-03-31 03:15:14 +00:00
|
|
|
|
|
|
|
return f"Agent {name} created with key {key}. First response: {agent_response}"
|
2023-03-30 09:04:09 +00:00
|
|
|
|
2023-04-02 08:13:15 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
def message_agent(key: str, message: str) -> str:
|
2023-04-02 17:03:37 +00:00
|
|
|
"""Message an agent with a given key and message"""
|
2023-04-04 20:53:59 +00:00
|
|
|
# Check if the key is a valid integer
|
2023-04-05 01:53:41 +00:00
|
|
|
if is_valid_int(key):
|
2023-04-15 12:56:23 +00:00
|
|
|
agent_response = AGENT_MANAGER.message_agent(int(key), message)
|
2023-04-05 01:53:41 +00:00
|
|
|
else:
|
2023-04-15 08:17:00 +00:00
|
|
|
return "Invalid key, must be an integer."
|
2023-03-31 03:15:14 +00:00
|
|
|
|
|
|
|
# Speak response
|
2023-04-15 12:56:23 +00:00
|
|
|
if CFG.speak_mode:
|
2023-04-15 00:04:48 +00:00
|
|
|
say_text(agent_response, 1)
|
2023-04-05 01:53:41 +00:00
|
|
|
return agent_response
|
2023-03-30 09:04:09 +00:00
|
|
|
|
2023-04-02 08:13:15 +00:00
|
|
|
|
2023-03-30 09:04:09 +00:00
|
|
|
def list_agents():
|
2023-04-15 12:56:23 +00:00
|
|
|
"""List all agents
|
|
|
|
|
|
|
|
Returns:
|
2023-04-15 17:04:15 +00:00
|
|
|
str: A list of all agents
|
2023-04-15 12:56:23 +00:00
|
|
|
"""
|
2023-04-15 19:55:13 +00:00
|
|
|
return "List of agents:\n" + "\n".join(
|
|
|
|
[str(x[0]) + ": " + x[1] for x in AGENT_MANAGER.list_agents()]
|
|
|
|
)
|
2023-04-15 12:56:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def delete_agent(key: str) -> str:
|
|
|
|
"""Delete an agent with a given key
|
2023-03-30 09:04:09 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
Args:
|
|
|
|
key (str): The key of the agent to delete
|
2023-04-02 08:13:15 +00:00
|
|
|
|
2023-04-15 12:56:23 +00:00
|
|
|
Returns:
|
|
|
|
str: A message indicating whether the agent was deleted or not
|
|
|
|
"""
|
|
|
|
result = AGENT_MANAGER.delete_agent(key)
|
2023-04-15 00:04:48 +00:00
|
|
|
return f"Agent {key} deleted." if result else f"Agent {key} does not exist."
|