From a593c3272735bf4ffbd839a6c5b895a84ca1cfe5 Mon Sep 17 00:00:00 2001 From: James Collins Date: Tue, 1 Aug 2023 10:48:20 -0700 Subject: [PATCH] Move more app files to app package (#5036) Co-authored-by: Nicholas Tindle --- autogpt/app/main.py | 16 +-- autogpt/app/setup.py | 2 +- autogpt/{ => app}/spinner.py | 0 autogpt/app/utils.py | 147 +++++++++++++++++++++++++++ autogpt/processing/text.py | 10 +- autogpt/utils.py | 170 +------------------------------- tests/challenges/utils.py | 4 +- tests/integration/test_setup.py | 6 +- tests/unit/test_spinner.py | 2 +- tests/unit/test_utils.py | 30 +++--- 10 files changed, 186 insertions(+), 201 deletions(-) rename autogpt/{ => app}/spinner.py (100%) create mode 100644 autogpt/app/utils.py diff --git a/autogpt/app/main.py b/autogpt/app/main.py index e7d51ce6361c..ed77cd438132 100644 --- a/autogpt/app/main.py +++ b/autogpt/app/main.py @@ -13,6 +13,14 @@ from autogpt.agents import Agent, AgentThoughts, CommandArgs, CommandName from autogpt.app.configurator import create_config from autogpt.app.setup import prompt_user +from autogpt.app.spinner import Spinner +from autogpt.app.utils import ( + clean_input, + get_current_git_branch, + get_latest_bulletin, + get_legal_warning, + markdown_to_ansi_style, +) from autogpt.commands import COMMAND_CATEGORIES from autogpt.config import AIConfig, Config, ConfigBuilder, check_openai_api_key from autogpt.llm.api_manager import ApiManager @@ -22,14 +30,6 @@ from autogpt.plugins import scan_plugins from autogpt.prompts.prompt import DEFAULT_TRIGGERING_PROMPT from autogpt.speech import say_text -from autogpt.spinner import Spinner -from autogpt.utils import ( - clean_input, - get_current_git_branch, - get_latest_bulletin, - get_legal_warning, - markdown_to_ansi_style, -) from autogpt.workspace import Workspace from scripts.install_plugin_deps import install_plugin_dependencies diff --git a/autogpt/app/setup.py b/autogpt/app/setup.py index cb6073adc0dd..f2879af6b361 100644 --- a/autogpt/app/setup.py +++ b/autogpt/app/setup.py @@ -5,7 +5,7 @@ from colorama import Fore, Style from jinja2 import Template -from autogpt import utils +from autogpt.app import utils from autogpt.config import Config from autogpt.config.ai_config import AIConfig from autogpt.llm.base import ChatSequence, Message diff --git a/autogpt/spinner.py b/autogpt/app/spinner.py similarity index 100% rename from autogpt/spinner.py rename to autogpt/app/spinner.py diff --git a/autogpt/app/utils.py b/autogpt/app/utils.py new file mode 100644 index 000000000000..5bf0d6c7cc79 --- /dev/null +++ b/autogpt/app/utils.py @@ -0,0 +1,147 @@ +import os +import re + +import requests +from colorama import Fore, Style +from git.repo import Repo +from prompt_toolkit import ANSI, PromptSession +from prompt_toolkit.history import InMemoryHistory + +from autogpt.config import Config +from autogpt.logs import logger + +session = PromptSession(history=InMemoryHistory()) + + +def clean_input(config: Config, prompt: str = "", talk=False): + try: + if config.chat_messages_enabled: + for plugin in config.plugins: + if not hasattr(plugin, "can_handle_user_input"): + continue + if not plugin.can_handle_user_input(user_input=prompt): + continue + plugin_response = plugin.user_input(user_input=prompt) + if not plugin_response: + continue + if plugin_response.lower() in [ + "yes", + "yeah", + "y", + "ok", + "okay", + "sure", + "alright", + ]: + return config.authorise_key + elif plugin_response.lower() in [ + "no", + "nope", + "n", + "negative", + ]: + return config.exit_key + return plugin_response + + # ask for input, default when just pressing Enter is y + logger.info("Asking user via keyboard...") + + # handle_sigint must be set to False, so the signal handler in the + # autogpt/main.py could be employed properly. This referes to + # https://github.com/Significant-Gravitas/Auto-GPT/pull/4799/files/3966cdfd694c2a80c0333823c3bc3da090f85ed3#r1264278776 + answer = session.prompt(ANSI(prompt), handle_sigint=False) + return answer + except KeyboardInterrupt: + logger.info("You interrupted Auto-GPT") + logger.info("Quitting...") + exit(0) + + +def get_bulletin_from_web(): + try: + response = requests.get( + "https://raw.githubusercontent.com/Significant-Gravitas/Auto-GPT/master/BULLETIN.md" + ) + if response.status_code == 200: + return response.text + except requests.exceptions.RequestException: + pass + + return "" + + +def get_current_git_branch() -> str: + try: + repo = Repo(search_parent_directories=True) + branch = repo.active_branch + return branch.name + except: + return "" + + +def get_latest_bulletin() -> tuple[str, bool]: + exists = os.path.exists("data/CURRENT_BULLETIN.md") + current_bulletin = "" + if exists: + current_bulletin = open( + "data/CURRENT_BULLETIN.md", "r", encoding="utf-8" + ).read() + new_bulletin = get_bulletin_from_web() + is_new_news = new_bulletin != "" and new_bulletin != current_bulletin + + news_header = Fore.YELLOW + "Welcome to Auto-GPT!\n" + if new_bulletin or current_bulletin: + news_header += ( + "Below you'll find the latest Auto-GPT News and updates regarding features!\n" + "If you don't wish to see this message, you " + "can run Auto-GPT with the *--skip-news* flag.\n" + ) + + if new_bulletin and is_new_news: + open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8").write(new_bulletin) + current_bulletin = f"{Fore.RED}::NEW BULLETIN::{Fore.RESET}\n\n{new_bulletin}" + + return f"{news_header}\n{current_bulletin}", is_new_news + + +def markdown_to_ansi_style(markdown: str): + ansi_lines: list[str] = [] + for line in markdown.split("\n"): + line_style = "" + + if line.startswith("# "): + line_style += Style.BRIGHT + else: + line = re.sub( + r"(? str: + legal_text = """ +## DISCLAIMER AND INDEMNIFICATION AGREEMENT +### PLEASE READ THIS DISCLAIMER AND INDEMNIFICATION AGREEMENT CAREFULLY BEFORE USING THE AUTOGPT SYSTEM. BY USING THE AUTOGPT SYSTEM, YOU AGREE TO BE BOUND BY THIS AGREEMENT. + +## Introduction +AutoGPT (the "System") is a project that connects a GPT-like artificial intelligence system to the internet and allows it to automate tasks. While the System is designed to be useful and efficient, there may be instances where the System could perform actions that may cause harm or have unintended consequences. + +## No Liability for Actions of the System +The developers, contributors, and maintainers of the AutoGPT project (collectively, the "Project Parties") make no warranties or representations, express or implied, about the System's performance, accuracy, reliability, or safety. By using the System, you understand and agree that the Project Parties shall not be liable for any actions taken by the System or any consequences resulting from such actions. + +## User Responsibility and Respondeat Superior Liability +As a user of the System, you are responsible for supervising and monitoring the actions of the System while it is operating on your +behalf. You acknowledge that using the System could expose you to potential liability including but not limited to respondeat superior and you agree to assume all risks and liabilities associated with such potential liability. + +## Indemnification +By using the System, you agree to indemnify, defend, and hold harmless the Project Parties from and against any and all claims, liabilities, damages, losses, or expenses (including reasonable attorneys' fees and costs) arising out of or in connection with your use of the System, including, without limitation, any actions taken by the System on your behalf, any failure to properly supervise or monitor the System, and any resulting harm or unintended consequences. + """ + return legal_text diff --git a/autogpt/processing/text.py b/autogpt/processing/text.py index faaa50e000db..dc245bb2aed8 100644 --- a/autogpt/processing/text.py +++ b/autogpt/processing/text.py @@ -10,7 +10,15 @@ from autogpt.llm.providers.openai import OPEN_AI_MODELS from autogpt.llm.utils import count_string_tokens, create_chat_completion from autogpt.logs import logger -from autogpt.utils import batch + + +def batch(iterable, max_batch_length: int, overlap: int = 0): + """Batch data from iterable into slices of length N. The last batch may be shorter.""" + # batched('ABCDEFG', 3) --> ABC DEF G + if max_batch_length < 1: + raise ValueError("n must be at least one") + for i in range(0, len(iterable), max_batch_length - overlap): + yield iterable[i : i + max_batch_length] def _max_chunk_length(model: str, max: Optional[int] = None) -> int: diff --git a/autogpt/utils.py b/autogpt/utils.py index 28c4be517fee..f69fe50f8622 100644 --- a/autogpt/utils.py +++ b/autogpt/utils.py @@ -1,70 +1,5 @@ -import os -import re - -import requests import yaml -from colorama import Fore, Style -from git.repo import Repo -from prompt_toolkit import ANSI, PromptSession -from prompt_toolkit.history import InMemoryHistory - -from autogpt.config import Config -from autogpt.logs import logger - -session = PromptSession(history=InMemoryHistory()) - - -def batch(iterable, max_batch_length: int, overlap: int = 0): - """Batch data from iterable into slices of length N. The last batch may be shorter.""" - # batched('ABCDEFG', 3) --> ABC DEF G - if max_batch_length < 1: - raise ValueError("n must be at least one") - for i in range(0, len(iterable), max_batch_length - overlap): - yield iterable[i : i + max_batch_length] - - -def clean_input(config: Config, prompt: str = "", talk=False): - try: - if config.chat_messages_enabled: - for plugin in config.plugins: - if not hasattr(plugin, "can_handle_user_input"): - continue - if not plugin.can_handle_user_input(user_input=prompt): - continue - plugin_response = plugin.user_input(user_input=prompt) - if not plugin_response: - continue - if plugin_response.lower() in [ - "yes", - "yeah", - "y", - "ok", - "okay", - "sure", - "alright", - ]: - return config.authorise_key - elif plugin_response.lower() in [ - "no", - "nope", - "n", - "negative", - ]: - return config.exit_key - return plugin_response - - # ask for input, default when just pressing Enter is y - logger.info("Asking user via keyboard...") - - # handle_sigint must be set to False, so the signal handler in the - # autogpt/main.py could be employed properly. This referes to - # https://github.com/Significant-Gravitas/Auto-GPT/pull/4799/files/3966cdfd694c2a80c0333823c3bc3da090f85ed3#r1264278776 - answer = session.prompt(ANSI(prompt), handle_sigint=False) - return answer - except KeyboardInterrupt: - logger.info("You interrupted Auto-GPT") - logger.info("Quitting...") - exit(0) +from colorama import Fore def validate_yaml_file(file: str): @@ -80,106 +15,3 @@ def validate_yaml_file(file: str): ) return (True, f"Successfully validated {Fore.CYAN}`{file}`{Fore.RESET}!") - - -def readable_file_size(size, decimal_places=2): - """Converts the given size in bytes to a readable format. - Args: - size: Size in bytes - decimal_places (int): Number of decimal places to display - """ - for unit in ["B", "KB", "MB", "GB", "TB"]: - if size < 1024.0: - break - size /= 1024.0 - return f"{size:.{decimal_places}f} {unit}" - - -def get_bulletin_from_web(): - try: - response = requests.get( - "https://raw.githubusercontent.com/Significant-Gravitas/Auto-GPT/master/BULLETIN.md" - ) - if response.status_code == 200: - return response.text - except requests.exceptions.RequestException: - pass - - return "" - - -def get_current_git_branch() -> str: - try: - repo = Repo(search_parent_directories=True) - branch = repo.active_branch - return branch.name - except: - return "" - - -def get_latest_bulletin() -> tuple[str, bool]: - exists = os.path.exists("data/CURRENT_BULLETIN.md") - current_bulletin = "" - if exists: - current_bulletin = open( - "data/CURRENT_BULLETIN.md", "r", encoding="utf-8" - ).read() - new_bulletin = get_bulletin_from_web() - is_new_news = new_bulletin != "" and new_bulletin != current_bulletin - - news_header = Fore.YELLOW + "Welcome to Auto-GPT!\n" - if new_bulletin or current_bulletin: - news_header += ( - "Below you'll find the latest Auto-GPT News and updates regarding features!\n" - "If you don't wish to see this message, you " - "can run Auto-GPT with the *--skip-news* flag.\n" - ) - - if new_bulletin and is_new_news: - open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8").write(new_bulletin) - current_bulletin = f"{Fore.RED}::NEW BULLETIN::{Fore.RESET}\n\n{new_bulletin}" - - return f"{news_header}\n{current_bulletin}", is_new_news - - -def markdown_to_ansi_style(markdown: str): - ansi_lines: list[str] = [] - for line in markdown.split("\n"): - line_style = "" - - if line.startswith("# "): - line_style += Style.BRIGHT - else: - line = re.sub( - r"(? str: - legal_text = """ -## DISCLAIMER AND INDEMNIFICATION AGREEMENT -### PLEASE READ THIS DISCLAIMER AND INDEMNIFICATION AGREEMENT CAREFULLY BEFORE USING THE AUTOGPT SYSTEM. BY USING THE AUTOGPT SYSTEM, YOU AGREE TO BE BOUND BY THIS AGREEMENT. - -## Introduction -AutoGPT (the "System") is a project that connects a GPT-like artificial intelligence system to the internet and allows it to automate tasks. While the System is designed to be useful and efficient, there may be instances where the System could perform actions that may cause harm or have unintended consequences. - -## No Liability for Actions of the System -The developers, contributors, and maintainers of the AutoGPT project (collectively, the "Project Parties") make no warranties or representations, express or implied, about the System's performance, accuracy, reliability, or safety. By using the System, you understand and agree that the Project Parties shall not be liable for any actions taken by the System or any consequences resulting from such actions. - -## User Responsibility and Respondeat Superior Liability -As a user of the System, you are responsible for supervising and monitoring the actions of the System while it is operating on your -behalf. You acknowledge that using the System could expose you to potential liability including but not limited to respondeat superior and you agree to assume all risks and liabilities associated with such potential liability. - -## Indemnification -By using the System, you agree to indemnify, defend, and hold harmless the Project Parties from and against any and all claims, liabilities, damages, losses, or expenses (including reasonable attorneys' fees and costs) arising out of or in connection with your use of the System, including, without limitation, any actions taken by the System on your behalf, any failure to properly supervise or monitor the System, and any resulting harm or unintended consequences. - """ - return legal_text diff --git a/tests/challenges/utils.py b/tests/challenges/utils.py index dd661b6e3fc6..67d7425c8dd3 100644 --- a/tests/challenges/utils.py +++ b/tests/challenges/utils.py @@ -38,7 +38,9 @@ def input_generator() -> Generator[str, None, None]: yield from input_sequence gen = input_generator() - monkeypatch.setattr("autogpt.utils.session.prompt", lambda _, **kwargs: next(gen)) + monkeypatch.setattr( + "autogpt.app.utils.session.prompt", lambda _, **kwargs: next(gen) + ) def setup_mock_log_cycle_agent_name( diff --git a/tests/integration/test_setup.py b/tests/integration/test_setup.py index f4bb9a5c8ba4..ff83eee6b53b 100644 --- a/tests/integration/test_setup.py +++ b/tests/integration/test_setup.py @@ -10,7 +10,7 @@ @pytest.mark.requires_openai_api_key def test_generate_aiconfig_automatic_default(patched_api_requestor, config): user_inputs = [""] - with patch("autogpt.utils.session.prompt", side_effect=user_inputs): + with patch("autogpt.app.utils.session.prompt", side_effect=user_inputs): ai_config = prompt_user(config) assert isinstance(ai_config, AIConfig) @@ -43,7 +43,7 @@ def test_generate_aiconfig_automatic_fallback(patched_api_requestor, config): "", "", ] - with patch("autogpt.utils.session.prompt", side_effect=user_inputs): + with patch("autogpt.app.utils.session.prompt", side_effect=user_inputs): ai_config = prompt_user(config) assert isinstance(ai_config, AIConfig) @@ -64,7 +64,7 @@ def test_prompt_user_manual_mode(patched_api_requestor, config): "", "", ] - with patch("autogpt.utils.session.prompt", side_effect=user_inputs): + with patch("autogpt.app.utils.session.prompt", side_effect=user_inputs): ai_config = prompt_user(config) assert isinstance(ai_config, AIConfig) diff --git a/tests/unit/test_spinner.py b/tests/unit/test_spinner.py index 4b22f24cbd78..8f894b79d56f 100644 --- a/tests/unit/test_spinner.py +++ b/tests/unit/test_spinner.py @@ -1,7 +1,7 @@ # Generated by CodiumAI import time -from autogpt.spinner import Spinner +from autogpt.app.spinner import Spinner """ Code Analysis diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index eb49908f3942..43d8dc39ed0c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -4,15 +4,14 @@ import pytest import requests -from autogpt.config import Config -from autogpt.json_utils.utilities import extract_dict_from_response, validate_dict -from autogpt.utils import ( +from autogpt.app.utils import ( get_bulletin_from_web, get_current_git_branch, get_latest_bulletin, - readable_file_size, - validate_yaml_file, ) +from autogpt.config import Config +from autogpt.json_utils.utilities import extract_dict_from_response, validate_dict +from autogpt.utils import validate_yaml_file from tests.utils import skip_in_ci @@ -77,13 +76,6 @@ def test_validate_yaml_file_invalid(): assert "There was an issue while trying to read" in message -def test_readable_file_size(): - size_in_bytes = 1024 * 1024 * 3.5 # 3.5 MB - readable_size = readable_file_size(size_in_bytes) - - assert readable_size == "3.50 MB" - - @patch("requests.get") def test_get_bulletin_from_web_success(mock_get): expected_content = "Test bulletin from web" @@ -127,7 +119,7 @@ def test_get_latest_bulletin_with_file(): with open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8") as f: f.write(expected_content) - with patch("autogpt.utils.get_bulletin_from_web", return_value=""): + with patch("autogpt.app.utils.get_bulletin_from_web", return_value=""): bulletin, is_new = get_latest_bulletin() assert expected_content in bulletin assert is_new == False @@ -140,7 +132,9 @@ def test_get_latest_bulletin_with_new_bulletin(): f.write("Old bulletin") expected_content = "New bulletin from web" - with patch("autogpt.utils.get_bulletin_from_web", return_value=expected_content): + with patch( + "autogpt.app.utils.get_bulletin_from_web", return_value=expected_content + ): bulletin, is_new = get_latest_bulletin() assert "::NEW BULLETIN::" in bulletin assert expected_content in bulletin @@ -154,7 +148,9 @@ def test_get_latest_bulletin_new_bulletin_same_as_old_bulletin(): with open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8") as f: f.write(expected_content) - with patch("autogpt.utils.get_bulletin_from_web", return_value=expected_content): + with patch( + "autogpt.app.utils.get_bulletin_from_web", return_value=expected_content + ): bulletin, is_new = get_latest_bulletin() assert expected_content in bulletin assert is_new == False @@ -170,7 +166,7 @@ def test_get_current_git_branch(): assert branch_name != "" -@patch("autogpt.utils.Repo") +@patch("autogpt.app.utils.Repo") def test_get_current_git_branch_success(mock_repo): mock_repo.return_value.active_branch.name = "test-branch" branch_name = get_current_git_branch() @@ -178,7 +174,7 @@ def test_get_current_git_branch_success(mock_repo): assert branch_name == "test-branch" -@patch("autogpt.utils.Repo") +@patch("autogpt.app.utils.Repo") def test_get_current_git_branch_failure(mock_repo): mock_repo.side_effect = Exception() branch_name = get_current_git_branch()