Skip to content

Commit

Permalink
Finish integrating command registry
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSchumacher committed Apr 17, 2023
1 parent 167628c commit c110f34
Show file tree
Hide file tree
Showing 50 changed files with 235 additions and 231 deletions.
28 changes: 19 additions & 9 deletions autogpt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import logging
import os
from pathlib import Path

from colorama import Fore

from autogpt.agent.agent import Agent
from autogpt.args import parse_arguments
from autogpt.commands.command import CommandRegistry
from autogpt.config import Config, check_openai_api_key
from autogpt.logs import logger
from autogpt.memory import get_memory

from autogpt.prompts.prompt import construct_main_ai_config
from autogpt.plugins import load_plugins

from autogpt.prompts.prompt import construct_main_ai_config

# Load environment variables from .env file

Expand Down Expand Up @@ -47,13 +47,20 @@ def main() -> None:
cfg.set_plugins(loaded_plugins)
# Create a CommandRegistry instance and scan default folder
command_registry = CommandRegistry()
command_registry.import_commands("scripts.ai_functions")
command_registry.import_commands("scripts.commands")
command_registry.import_commands("scripts.execute_code")
command_registry.import_commands("scripts.agent_manager")
command_registry.import_commands("scripts.file_operations")
command_registry.import_commands("autogpt.commands.audio_text")
command_registry.import_commands("autogpt.commands.evaluate_code")
command_registry.import_commands("autogpt.commands.execute_code")
command_registry.import_commands("autogpt.commands.file_operations")
command_registry.import_commands("autogpt.commands.git_operations")
command_registry.import_commands("autogpt.commands.google_search")
command_registry.import_commands("autogpt.commands.image_gen")
command_registry.import_commands("autogpt.commands.twitter")
command_registry.import_commands("autogpt.commands.web_selenium")
command_registry.import_commands("autogpt.commands.write_tests")
command_registry.import_commands("autogpt.app")
ai_name = ""
ai_config = construct_main_ai_config()
ai_config.command_registry = command_registry
# print(prompt)
# Initialize variables
full_message_history = []
Expand All @@ -70,14 +77,17 @@ def main() -> None:
f"Using memory of type:", Fore.GREEN, f"{memory.__class__.__name__}"
)
logger.typewriter_log(f"Using Browser:", Fore.GREEN, cfg.selenium_web_browser)
prompt = ai_config.construct_full_prompt()
if cfg.debug_mode:
logger.typewriter_log("Prompt:", Fore.GREEN, prompt)
agent = Agent(
ai_name=ai_name,
memory=memory,
full_message_history=full_message_history,
next_action_count=next_action_count,
command_registry=command_registry,
config=ai_config,
prompt=ai_config.construct_full_prompt(),
prompt=prompt,
user_input=user_input,
)
agent.start_interaction_loop()
Expand Down
2 changes: 1 addition & 1 deletion autogpt/agent/agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from colorama import Fore, Style
from autogpt.app import execute_command, get_command

from autogpt.app import execute_command, get_command
from autogpt.chat import chat_with_ai, create_chat_message
from autogpt.config import Config
from autogpt.json_fixes.bracket_termination import (
Expand Down
2 changes: 1 addition & 1 deletion autogpt/agent/agent_manager.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Agent manager for managing GPT agents"""
from __future__ import annotations

from autogpt.config.config import Config, Singleton
from autogpt.llm_utils import create_chat_completion
from autogpt.config.config import Singleton, Config


class AgentManager(metaclass=Singleton):
Expand Down
92 changes: 16 additions & 76 deletions autogpt/app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
""" Command and Control """
import json
from typing import List, NoReturn, Union

from autogpt.agent.agent_manager import AgentManager
from autogpt.commands.command import command, CommandRegistry
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
from autogpt.config import Config
from autogpt.commands.image_gen import generate_image
from autogpt.commands.audio_text import read_audio_from_file
from autogpt.commands.web_requests import scrape_links, scrape_text
from autogpt.commands.command import CommandRegistry, command
from autogpt.commands.evaluate_code import evaluate_code
from autogpt.commands.execute_code import execute_python_file, execute_shell
from autogpt.commands.file_operations import (
append_to_file,
Expand All @@ -19,15 +14,20 @@
search_files,
write_to_file,
)
from autogpt.commands.git_operations import clone_repository
from autogpt.commands.google_search import google_official_search, google_search
from autogpt.commands.image_gen import generate_image
from autogpt.commands.improve_code import improve_code
from autogpt.commands.twitter import send_tweet
from autogpt.commands.web_requests import scrape_links, scrape_text
from autogpt.commands.web_selenium import browse_website
from autogpt.commands.write_tests import write_tests
from autogpt.config import Config
from autogpt.json_fixes.parsing import fix_and_parse_json
from autogpt.memory import get_memory
from autogpt.processing.text import summarize_text
from autogpt.prompts.generator import PromptGenerator
from autogpt.speech import say_text
from autogpt.commands.web_selenium import browse_website
from autogpt.commands.git_operations import clone_repository
from autogpt.commands.twitter import send_tweet


CFG = Config()
AGENT_MANAGER = AgentManager()
Expand Down Expand Up @@ -132,76 +132,16 @@ def execute_command(

# TODO: Remove commands below after they are moved to the command registry.
command_name = map_command_synonyms(command_name)
if command_name == "google":
# Check if the Google API key is set and use the official search method
# If the API key is not set or has only whitespaces, use the unofficial
# search method
key = CFG.google_api_key
if key and key.strip() and key != "your-google-api-key":
google_result = google_official_search(arguments["input"])
return google_result
else:
google_result = google_search(arguments["input"])

# google_result can be a list or a string depending on the search results
if isinstance(google_result, list):
safe_message = [
google_result_single.encode("utf-8", "ignore")
for google_result_single in google_result
]
else:
safe_message = google_result.encode("utf-8", "ignore")

return str(safe_message)
elif command_name == "memory_add":

if command_name == "memory_add":
return memory.add(arguments["string"])
elif command_name == "start_agent":
return start_agent(
arguments["name"], arguments["task"], arguments["prompt"]
)
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"])
elif command_name == "get_text_summary":
return get_text_summary(arguments["url"], arguments["question"])
elif command_name == "get_hyperlinks":
return get_hyperlinks(arguments["url"])
elif command_name == "clone_repository":
return clone_repository(
arguments["repository_url"], arguments["clone_path"]
)
elif command_name == "read_file":
return read_file(arguments["file"])
elif command_name == "write_to_file":
return write_to_file(arguments["file"], arguments["text"])
elif command_name == "append_to_file":
return append_to_file(arguments["file"], arguments["text"])
elif command_name == "delete_file":
return delete_file(arguments["file"])
elif command_name == "search_files":
return search_files(arguments["directory"])
elif command_name == "browse_website":
return browse_website(arguments["url"], arguments["question"])
# 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"
elif command_name == "evaluate_code":
return evaluate_code(arguments["code"])
elif command_name == "improve_code":
return improve_code(arguments["suggestions"], arguments["code"])
elif command_name == "write_tests":
return write_tests(arguments["code"], arguments.get("focus"))
elif command_name == "execute_python_file": # Add this command
return execute_python_file(arguments["file"])
elif command_name == "read_audio_from_file":
return read_audio_from_file(arguments["file"])
elif command_name == "generate_image":
return generate_image(arguments["prompt"])
elif command_name == "send_tweet":
return send_tweet(arguments["text"])
# filepath, write your code to file and try again
elif command_name == "do_nothing":
return "No action performed."
elif command_name == "task_complete":
Expand Down Expand Up @@ -305,7 +245,7 @@ def message_agent(key: str, message: str) -> str:


@command("list_agents", "List GPT Agents", "")
def list_agents():
def list_agents() -> str:
"""List all agents
Returns:
Expand Down
1 change: 1 addition & 0 deletions autogpt/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import argparse

from colorama import Fore

from autogpt import utils
from autogpt.config import Config
from autogpt.logs import logger
Expand Down
42 changes: 35 additions & 7 deletions autogpt/commands/audio_text.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
import requests
"""Commands for converting audio to text."""
import json

import requests

from autogpt.commands.command import command
from autogpt.config import Config
from autogpt.workspace import path_in_workspace

cfg = Config()
CFG = Config()


@command(
"read_audio_from_file",
"Convert Audio to text",
'"file": "<file>"',
CFG.huggingface_audio_to_text_model,
"Configure huggingface_audio_to_text_model.",
)
def read_audio_from_file(audio_path: str) -> str:
"""
Convert audio to text.
def read_audio_from_file(audio_path):
Args:
audio_path (str): The path to the audio file
Returns:
str: The text from the audio
"""
audio_path = path_in_workspace(audio_path)
with open(audio_path, "rb") as audio_file:
audio = audio_file.read()
return read_audio(audio)


def read_audio(audio):
model = cfg.huggingface_audio_to_text_model
def read_audio(audio: bytes) -> str:
"""
Convert audio to text.
Args:
audio (bytes): The audio to convert
Returns:
str: The text from the audio
"""
model = CFG.huggingface_audio_to_text_model
api_url = f"https://api-inference.huggingface.co/models/{model}"
api_token = cfg.huggingface_api_token
api_token = CFG.huggingface_api_token
headers = {"Authorization": f"Bearer {api_token}"}

if api_token is None:
Expand All @@ -32,4 +60,4 @@ def read_audio(audio):
)

text = json.loads(response.content.decode("utf-8"))["text"]
return "The audio says: " + text
return f"The audio says: {text}"
6 changes: 3 additions & 3 deletions autogpt/commands/command.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import sys
import importlib
import inspect
from typing import Callable, Any, List, Optional
import os
import sys
from typing import Any, Callable, List, Optional

# Unique identifier for auto-gpt commands
AUTO_GPT_COMMAND_IDENTIFIER = "auto_gpt_command"
Expand Down
2 changes: 1 addition & 1 deletion autogpt/commands/evaluate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from autogpt.llm_utils import call_ai_function


@command("evaluate_code", "Evaluate Code", '"code": "<full _code_string>"')
@command("evaluate_code", "Evaluate Code", '"code": "<full_code_string>"')
def evaluate_code(code: str) -> list[str]:
"""
A function that takes in a string and returns a response from create chat
Expand Down
5 changes: 3 additions & 2 deletions autogpt/commands/execute_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import docker
from docker.errors import ImageNotFound
from autogpt.config import Config

from autogpt.commands.command import command
from autogpt.workspace import path_in_workspace, WORKSPACE_PATH
from autogpt.config import Config
from autogpt.workspace import WORKSPACE_PATH, path_in_workspace

CFG = Config()

Expand Down
3 changes: 2 additions & 1 deletion autogpt/commands/file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import os.path
from pathlib import Path
from typing import Generator

from autogpt.commands.command import command
from autogpt.workspace import path_in_workspace, WORKSPACE_PATH
from autogpt.workspace import WORKSPACE_PATH, path_in_workspace

LOG_FILE = "file_logger.txt"
LOG_FILE_PATH = WORKSPACE_PATH / LOG_FILE
Expand Down
13 changes: 11 additions & 2 deletions autogpt/commands/git_operations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
"""Git operations for autogpt"""
import git
from git.repo import Repo

from autogpt.commands.command import command
from autogpt.config import Config

CFG = Config()


@command(
"clone_repository",
"Clone Repositoryy",
'"repository_url": "<url>", "clone_path": "<directory>"',
CFG.github_username and CFG.github_api_key,
"Configure github_username and github_api_key.",
)
def clone_repository(repo_url: str, clone_path: str) -> str:
"""Clone a github repository locally
Expand All @@ -17,7 +26,7 @@ def clone_repository(repo_url: str, clone_path: str) -> str:
split_url = repo_url.split("//")
auth_repo_url = f"//{CFG.github_username}:{CFG.github_api_key}@".join(split_url)
try:
git.Repo.clone_from(auth_repo_url, clone_path)
Repo.clone_from(auth_repo_url, clone_path)
return f"""Cloned {repo_url} to {clone_path}"""
except Exception as e:
return f"Error: {str(e)}"
Loading

0 comments on commit c110f34

Please sign in to comment.