Skip to content

Commit

Permalink
Fix all commands and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSchumacher committed Apr 19, 2023
1 parent 23c650c commit d7679d7
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 83 deletions.
48 changes: 4 additions & 44 deletions autogpt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,9 @@
from typing import Dict, List, NoReturn, Union

from autogpt.agent.agent_manager import AgentManager
from autogpt.commands.analyze_code import analyze_code
from autogpt.commands.audio_text import read_audio_from_file
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,
execute_shell_popen,
)
from autogpt.commands.file_operations import (
append_to_file,
delete_file,
download_file,
read_file,
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_utils.json_fix_llm import fix_and_parse_json
from autogpt.memory import get_memory
from autogpt.processing.text import summarize_text
from autogpt.prompts.generator import PromptGenerator
Expand Down Expand Up @@ -137,26 +113,8 @@ def execute_command(
command_name = map_command_synonyms(command_name.lower())

if command_name == "memory_add":
return memory.add(arguments["string"])
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 == "analyze_code":
return analyze_code(arguments["code"])
elif command_name == "download_file":
if not CFG.allow_downloads:
return "Error: You do not have user authorization to download files locally."
return download_file(arguments["url"], arguments["file"])
elif command_name == "execute_shell_popen":
if CFG.execute_local_commands:
return execute_shell_popen(arguments["command_line"])
else:
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."
)
return get_memory(CFG).add(arguments["string"])

# 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
Expand All @@ -177,6 +135,7 @@ def execute_command(
return f"Error: {str(e)}"


@command("get_text_summary", "Get text summary", '"url": "<url>", "question": "<question>"')
def get_text_summary(url: str, question: str) -> str:
"""Return the results of a Google search
Expand All @@ -192,6 +151,7 @@ def get_text_summary(url: str, question: str) -> str:
return f""" "Result" : {summary}"""


@command("get_hyperlinks", "Get text summary", '"url": "<url>"')
def get_hyperlinks(url: str) -> Union[str, List[str]]:
"""Return the results of a Google search
Expand Down
3 changes: 2 additions & 1 deletion autogpt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ def main(
cfg.set_plugins(scan_plugins(cfg, cfg.debug_mode))
# Create a CommandRegistry instance and scan default folder
command_registry = CommandRegistry()
command_registry.import_commands("autogpt.commands.analyze_code")
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.improve_code")
command_registry.import_commands("autogpt.commands.twitter")
command_registry.import_commands("autogpt.commands.web_selenium")
command_registry.import_commands("autogpt.commands.write_tests")
Expand Down
6 changes: 6 additions & 0 deletions autogpt/commands/analyze_code.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""Code evaluation module."""
from __future__ import annotations

from autogpt.commands.command import command
from autogpt.llm_utils import call_ai_function


@command(
"analyze_code",
"Analyze Code",
'"code": "<full_code_string>"',
)
def analyze_code(code: str) -> list[str]:
"""
A function that takes in a string and returns a response from create chat
Expand Down
6 changes: 3 additions & 3 deletions autogpt/commands/audio_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
@command(
"read_audio_from_file",
"Convert Audio to text",
'"file": "<file>"',
'"filename": "<filename>"',
CFG.huggingface_audio_to_text_model,
"Configure huggingface_audio_to_text_model.",
)
def read_audio_from_file(audio_path: str) -> str:
def read_audio_from_file(filename: str) -> str:
"""
Convert audio to text.
Expand All @@ -27,7 +27,7 @@ def read_audio_from_file(audio_path: str) -> str:
Returns:
str: The text from the audio
"""
audio_path = path_in_workspace(audio_path)
audio_path = path_in_workspace(filename)
with open(audio_path, "rb") as audio_file:
audio = audio_file.read()
return read_audio(audio)
Expand Down
4 changes: 2 additions & 2 deletions autogpt/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(
name: str,
description: str,
method: Callable[..., Any],
signature: str = None,
signature: str = '',
enabled: bool = True,
disabled_reason: Optional[str] = None,
):
Expand Down Expand Up @@ -126,7 +126,7 @@ def import_commands(self, module_name: str) -> None:
def command(
name: str,
description: str,
signature: str = None,
signature: str = '',
enabled: bool = True,
disabled_reason: Optional[str] = None,
) -> Callable[..., Any]:
Expand Down
19 changes: 13 additions & 6 deletions autogpt/commands/execute_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
CFG = Config()


@command("execute_python_file", "Execute Python File", '"file": "<file>"')
def execute_python_file(file: str) -> str:
@command("execute_python_file", "Execute Python File", '"filename": "<filename>"')
def execute_python_file(filename: str) -> str:
"""Execute a Python file in a Docker container and return the output
Args:
file (str): The name of the file to execute
filename (str): The name of the file to execute
Returns:
str: The output of the file
"""

file = filename
print(f"Executing file '{file}' in workspace '{WORKSPACE_PATH}'")

if not file.endswith(".py"):
Expand Down Expand Up @@ -138,9 +138,16 @@ def execute_shell(command_line: str) -> str:

os.chdir(current_dir)

return output


@command(
"execute_shell_popen",
"Execute Shell Command, non-interactive commands only",
'"command_line": "<command_line>"',
CFG.execute_local_commands,
"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.",
)
def execute_shell_popen(command_line) -> str:
"""Execute a shell command with Popen and returns an english description
of the event and the process id
Expand Down
12 changes: 8 additions & 4 deletions autogpt/commands/file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
from colorama import Back, Fore
from requests.adapters import HTTPAdapter, Retry

from autogpt.config import Config
from autogpt.commands.command import command
from autogpt.spinner import Spinner
from autogpt.utils import readable_file_size
from autogpt.workspace import WORKSPACE_PATH, path_in_workspace

CFG = Config()
LOG_FILE = "file_logger.txt"
LOG_FILE_PATH = WORKSPACE_PATH / LOG_FILE

Expand Down Expand Up @@ -82,7 +84,7 @@ def split_file(
start += max_length - overlap


@command("read_file", "Read file", '"file": "<file>"')
@command("read_file", "Read file", '"filename": "<filename>"')
def read_file(filename: str) -> str:
"""Read a file and return the contents
Expand Down Expand Up @@ -135,7 +137,7 @@ def ingest_file(
print(f"Error while ingesting file '{filename}': {str(e)}")


@command("write_to_file", "Write to file", '"file": "<file>", "text": "<text>"')
@command("write_to_file", "Write to file", '"filename": "<filename>", "text": "<text>"')
def write_to_file(filename: str, text: str) -> str:
"""Write text to a file
Expand All @@ -161,7 +163,7 @@ def write_to_file(filename: str, text: str) -> str:
return f"Error: {str(e)}"


@command("append_to_file", "Append to file", '"file": "<file>", "text": "<text>"')
@command("append_to_file", "Append to file", '"filename": "<filename>", "text": "<text>"')
def append_to_file(filename: str, text: str, shouldLog: bool = True) -> str:
"""Append text to a file
Expand All @@ -185,7 +187,7 @@ def append_to_file(filename: str, text: str, shouldLog: bool = True) -> str:
return f"Error: {str(e)}"


@command("delete_file", "Delete file", '"file": "<file>"')
@command("delete_file", "Delete file", '"filename": "<filename>"')
def delete_file(filename: str) -> str:
"""Delete a file
Expand Down Expand Up @@ -233,6 +235,8 @@ def search_files(directory: str) -> list[str]:
return found_files



@command("download_file", "Search Files", '"url": "<url>", "filename": "<filename>"', CFG.allow_downloads, "Error: You do not have user authorization to download files locally.")
def download_file(url, filename):
"""Downloads a file
Args:
Expand Down
10 changes: 5 additions & 5 deletions autogpt/commands/git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@
@command(
"clone_repository",
"Clone Repositoryy",
'"repository_url": "<url>", "clone_path": "<directory>"',
'"repository_url": "<repository_url>", "clone_path": "<clone_path>"',
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:
def clone_repository(repository_url: str, clone_path: str) -> str:
"""Clone a GitHub repository locally
Args:
repo_url (str): The URL of the repository to clone
repository_url (str): The URL of the repository to clone
clone_path (str): The path to clone the repository to
Returns:
str: The result of the clone operation"""
split_url = repo_url.split("//")
split_url = repository_url.split("//")
auth_repo_url = f"//{CFG.github_username}:{CFG.github_api_key}@".join(split_url)
safe_clone_path = path_in_workspace(clone_path)
try:
Repo.clone_from(auth_repo_url, safe_clone_path)
return f"""Cloned {repo_url} to {safe_clone_path}"""
return f"""Cloned {repository_url} to {safe_clone_path}"""
except Exception as e:
return f"Error: {str(e)}"
4 changes: 2 additions & 2 deletions autogpt/commands/google_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
CFG = Config()


@command("google", "Google Search", '"query": "<search>"', not CFG.google_api_key)
@command("google", "Google Search", '"query": "<query>"', not CFG.google_api_key)
def google_search(query: str, num_results: int = 8) -> str:
"""Return the results of a Google search
Expand Down Expand Up @@ -40,7 +40,7 @@ def google_search(query: str, num_results: int = 8) -> str:
@command(
"google",
"Google Search",
'"query": "<search>"',
'"query": "<query>"',
bool(CFG.google_api_key),
"Configure google_api_key.",
)
Expand Down
2 changes: 1 addition & 1 deletion autogpt/commands/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@command(
"send_tweet",
"Send Tweet",
'"text": "<text>"',
'"tweet_text": "<tweet_text>"',
)
def send_tweet(tweet_text: str) -> str:
"""
Expand Down
5 changes: 3 additions & 2 deletions autogpt/config/ai_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

from autogpt.prompts.generator import PromptGenerator

# Soon this will go in a folder where it remembers more stuff about the run(s)
SAVE_FILE = str(Path(os.getcwd()) / "ai_settings.yaml")


class AIConfig:
"""
Expand Down Expand Up @@ -44,8 +47,6 @@ def __init__(
self.prompt_generator = None
self.command_registry = None

# Soon this will go in a folder where it remembers more stuff about the run(s)
SAVE_FILE = Path(os.getcwd()) / "ai_settings.yaml"

@staticmethod
def load(config_file: str = SAVE_FILE) -> "AIConfig":
Expand Down
4 changes: 3 additions & 1 deletion autogpt/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ def create_chat_completion(
temperature=temperature,
max_tokens=max_tokens,
):
return plugin.handle_chat_completion(
message = plugin.handle_chat_completion(
messages=messages,
model=model,
temperature=temperature,
max_tokens=max_tokens,
)
if message is not None:
return message
response = None
for attempt in range(num_retries):
backoff = 2 ** (attempt + 2)
Expand Down
Loading

0 comments on commit d7679d7

Please sign in to comment.