2023-03-28 22:25:42 +00:00
|
|
|
import browse
|
|
|
|
import json
|
|
|
|
import memory as mem
|
2023-03-29 01:40:46 +00:00
|
|
|
import datetime
|
2023-03-30 09:04:09 +00:00
|
|
|
import agent_manager as agents
|
2023-03-31 03:15:14 +00:00
|
|
|
import speak
|
|
|
|
from config import Config
|
2023-04-01 03:08:30 +00:00
|
|
|
from file_operations import read_file, write_to_file, append_to_file, delete_file
|
2023-03-31 03:15:14 +00:00
|
|
|
cfg = Config()
|
2023-03-30 09:04:09 +00:00
|
|
|
|
2023-03-28 22:25:42 +00:00
|
|
|
|
2023-03-29 01:40:46 +00:00
|
|
|
def get_command(response):
|
|
|
|
try:
|
|
|
|
response_json = json.loads(response)
|
|
|
|
command = response_json["command"]
|
|
|
|
command_name = command["name"]
|
|
|
|
arguments = command["args"]
|
|
|
|
|
2023-03-30 09:05:03 +00:00
|
|
|
if not arguments:
|
|
|
|
arguments = {}
|
|
|
|
|
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
|
|
|
|
|
|
|
def execute_command(command_name, arguments):
|
|
|
|
try:
|
|
|
|
if command_name == "google":
|
|
|
|
return google_search(arguments["input"])
|
|
|
|
elif command_name == "check_news":
|
|
|
|
return check_news(arguments["source"])
|
|
|
|
elif command_name == "check_notifications":
|
|
|
|
return check_notifications(arguments["website"])
|
|
|
|
elif command_name == "memory_add":
|
|
|
|
return commit_memory(arguments["string"])
|
|
|
|
elif command_name == "memory_del":
|
|
|
|
return delete_memory(arguments["key"])
|
|
|
|
elif command_name == "memory_ovr":
|
|
|
|
return overwrite_memory(arguments["key"], arguments["string"])
|
2023-03-30 09:04:09 +00:00
|
|
|
elif command_name == "start_agent":
|
2023-03-31 03:15:14 +00:00
|
|
|
return start_agent(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-29 01:40:46 +00:00
|
|
|
elif command_name == "navigate_website":
|
|
|
|
return navigate_website(arguments["action"], arguments["username"])
|
|
|
|
elif command_name == "register_account":
|
|
|
|
return register_account(arguments["username"], arguments["website"])
|
2023-03-30 09:06:31 +00:00
|
|
|
elif command_name == "get_text_summary":
|
|
|
|
return get_text_summary(arguments["url"])
|
2023-03-30 09:10:52 +00:00
|
|
|
elif command_name == "get_hyperlinks":
|
|
|
|
return get_hyperlinks(arguments["url"])
|
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-03-30 11:47:55 +00:00
|
|
|
elif command_name == "browse_website":
|
2023-03-30 09:23:52 +00:00
|
|
|
return browse_website(arguments["url"])
|
2023-03-30 09:10:05 +00:00
|
|
|
elif command_name == "task_complete":
|
|
|
|
shutdown()
|
2023-03-29 01:40:46 +00:00
|
|
|
else:
|
|
|
|
return f"unknown command {command_name}"
|
|
|
|
# All other errors, return "Error: + error message"
|
|
|
|
except Exception as e:
|
|
|
|
return "Error: " + str(e)
|
2023-03-28 22:25:42 +00:00
|
|
|
|
2023-03-29 01:40:46 +00:00
|
|
|
def get_datetime():
|
|
|
|
return "Current date and time: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
|
|
|
|
|
|
|
### Implemented Commands: ###
|
2023-03-31 03:15:14 +00:00
|
|
|
def google_search(query, num_results=8):
|
2023-03-28 22:25:42 +00:00
|
|
|
search_results = []
|
|
|
|
for j in browse.search(query, num_results=num_results):
|
|
|
|
search_results.append(j)
|
2023-03-31 03:15:14 +00:00
|
|
|
|
2023-03-28 22:25:42 +00:00
|
|
|
return json.dumps(search_results, ensure_ascii=False, indent=4)
|
|
|
|
|
2023-03-30 09:23:52 +00:00
|
|
|
def browse_website(url):
|
|
|
|
summary = get_text_summary(url)
|
|
|
|
links = get_hyperlinks(url)
|
|
|
|
|
2023-03-30 11:45:15 +00:00
|
|
|
# Limit links to 5
|
|
|
|
if len(links) > 5:
|
|
|
|
links = links[:5]
|
|
|
|
|
2023-03-30 09:23:52 +00:00
|
|
|
result = f"""Website Content Summary: {summary}\n\nLinks: {links}"""
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2023-03-30 09:07:18 +00:00
|
|
|
def get_text_summary(url):
|
2023-03-29 08:43:32 +00:00
|
|
|
text = browse.scrape_text(url)
|
2023-03-28 22:52:25 +00:00
|
|
|
summary = browse.summarize_text(text)
|
2023-03-28 22:25:42 +00:00
|
|
|
return """ "Result" : """ + summary
|
|
|
|
|
2023-03-30 09:10:52 +00:00
|
|
|
def get_hyperlinks(url):
|
2023-03-30 11:45:15 +00:00
|
|
|
link_list = browse.scrape_links(url)
|
|
|
|
return link_list
|
2023-03-30 09:10:52 +00:00
|
|
|
|
2023-03-28 22:25:42 +00:00
|
|
|
def check_news(source):
|
|
|
|
print("Checking news from BBC world instead of " + source)
|
2023-03-31 03:15:14 +00:00
|
|
|
_text = get_text_summary("https://www.bbc.com/news/world")
|
2023-03-28 22:25:42 +00:00
|
|
|
return _text
|
|
|
|
|
|
|
|
def commit_memory(string):
|
2023-03-30 09:15:35 +00:00
|
|
|
_text = f"""Committing memory with string "{string}" """
|
2023-03-28 22:25:42 +00:00
|
|
|
mem.permanent_memory.append(string)
|
|
|
|
return _text
|
|
|
|
|
|
|
|
def delete_memory(key):
|
|
|
|
if key >= 0 and key < len(mem.permanent_memory):
|
|
|
|
_text = "Deleting memory with key " + str(key)
|
|
|
|
del mem.permanent_memory[key]
|
|
|
|
print(_text)
|
|
|
|
return _text
|
|
|
|
else:
|
|
|
|
print("Invalid key, cannot delete memory.")
|
|
|
|
return None
|
|
|
|
def overwrite_memory(key, string):
|
|
|
|
if key >= 0 and key < len(mem.permanent_memory):
|
2023-03-31 03:15:14 +00:00
|
|
|
_text = "Overwriting memory with key " + \
|
|
|
|
str(key) + " and string " + string
|
2023-03-28 22:25:42 +00:00
|
|
|
mem.permanent_memory[key] = string
|
|
|
|
print(_text)
|
|
|
|
return _text
|
|
|
|
else:
|
|
|
|
print("Invalid key, cannot overwrite memory.")
|
|
|
|
return None
|
2023-03-30 09:08:13 +00:00
|
|
|
|
|
|
|
def shutdown():
|
|
|
|
print("Shutting down...")
|
|
|
|
quit()
|
|
|
|
|
2023-03-31 03:15:14 +00:00
|
|
|
def start_agent(name, task, prompt, model="gpt-3.5-turbo"):
|
|
|
|
global cfg
|
|
|
|
|
|
|
|
# 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
|
|
|
|
if cfg.speak_mode:
|
2023-03-31 21:49:17 +00:00
|
|
|
speak.say_text(agent_intro, 1)
|
2023-03-31 03:15:14 +00:00
|
|
|
key, ack = agents.create_agent(task, first_message, model)
|
2023-03-28 22:25:42 +00:00
|
|
|
|
2023-03-31 03:15:14 +00:00
|
|
|
if cfg.speak_mode:
|
|
|
|
speak.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
|
|
|
|
agent_response = message_agent(key, prompt)
|
|
|
|
|
|
|
|
return f"Agent {name} created with key {key}. First response: {agent_response}"
|
2023-03-30 09:04:09 +00:00
|
|
|
|
|
|
|
def message_agent(key, message):
|
2023-03-31 03:15:14 +00:00
|
|
|
global cfg
|
2023-03-30 09:04:09 +00:00
|
|
|
agent_response = agents.message_agent(key, message)
|
2023-03-31 03:15:14 +00:00
|
|
|
|
|
|
|
# Speak response
|
|
|
|
if cfg.speak_mode:
|
2023-03-31 21:49:17 +00:00
|
|
|
speak.say_text(agent_response, 1)
|
2023-03-31 03:15:14 +00:00
|
|
|
|
2023-03-30 09:04:09 +00:00
|
|
|
return f"Agent {key} responded: {agent_response}"
|
|
|
|
|
|
|
|
def list_agents():
|
|
|
|
return agents.list_agents()
|
|
|
|
|
|
|
|
def delete_agent(key):
|
|
|
|
result = agents.delete_agent(key)
|
|
|
|
if result == False:
|
|
|
|
return f"Agent {key} does not exist."
|
|
|
|
return f"Agent {key} deleted."
|
2023-03-28 22:25:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def navigate_website(action, username):
|
|
|
|
_text = "Navigating website with action " + action + " and username " + username
|
|
|
|
print(_text)
|
|
|
|
return "Command not implemented yet."
|
|
|
|
|
|
|
|
def register_account(username, website):
|
|
|
|
_text = "Registering account with username " + username + " and website " + website
|
|
|
|
print(_text)
|
|
|
|
return "Command not implemented yet."
|
|
|
|
|
|
|
|
def check_notifications(website):
|
|
|
|
_text = "Checking notifications from " + website
|
|
|
|
print(_text)
|
|
|
|
return "Command not implemented yet."
|