AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
Merge remote-tracking branch 'upstream/master' into Release-v0.4.1
Luke K (pr-0f3t)eb2410ac6ca7
diff --cc autogpt/app.pyindex 780b74a019,78e3a4dd20..8586653cc3--- a/autogpt/app.py+++ b/autogpt/app.py@@@ -1,14 -1,8 +1,14 @@@""" Command and Control """import json- from typing import Dict, List, Union+ from typing import Dictfrom autogpt.agent.agent import Agent+from autogpt.agent.agent_manager import AgentManager+from autogpt.commands.command import command+from autogpt.commands.web_requests import scrape_links, scrape_text+from autogpt.processing.text import summarize_text+from autogpt.speech import say_text+from autogpt.url_utils.validators import validate_urldef is_valid_int(value: str) -> bool:diff --cc autogpt/commands/file_operations.pyindex b851d66255,ebf15ac09e..d059493f09--- a/autogpt/commands/file_operations.py+++ b/autogpt/commands/file_operations.py@@@ -4,22 -4,15 +4,16 @@@ from __future__ import annotationimport hashlibimport osimport os.path- import refrom typing import Generator, Literal- import requests- from colorama import Back, Forefrom confection import Config- from requests.adapters import HTTPAdapter, Retryfrom autogpt.agent.agent import Agent- from autogpt.commands.command import command, ignore_unexpected_kwargs+ from autogpt.command_decorator import commandfrom autogpt.commands.file_operations_utils import read_textual_file+from autogpt.config import Configfrom autogpt.logs import loggerfrom autogpt.memory.vector import MemoryItem, VectorMemory- from autogpt.spinner import Spinner- from autogpt.utils import readable_file_sizeOperation = Literal["write", "append", "delete"]diff --cc autogpt/llm/api_manager.pyindex acc38c44cd,bf276f5a11..aaf9342160--- a/autogpt/llm/api_manager.py+++ b/autogpt/llm/api_manager.py@@@ -5,9 -5,7 +5,13 @@@ from typing import List, Optionaimport openaifrom openai import Model++<<<<<<< HEAD+from autogpt.config import Config+from autogpt.llm.base import CompletionModelInfo, MessageDict+from autogpt.llm.providers.openai import OPEN_AI_MODELS++=======+ from autogpt.llm.modelsinfo import COSTS++>>>>>>> upstream/masterfrom autogpt.logs import loggerfrom autogpt.singleton import Singleton@@@ -26,53 -24,8 +30,8 @@@ class ApiManager(metaclass=Singleton)self.total_cost = 0self.total_budget = 0.0self.models = None-- def create_chat_completion(- self,- messages: list[MessageDict],- model: str | None = None,- temperature: float = None,- max_tokens: int | None = None,- deployment_id=None,- ):- """- Create a chat completion and update the cost.- Args:- messages (list): The list of messages to send to the API.- model (str): The model to use for the API call.- temperature (float): The temperature to use for the API call.- max_tokens (int): The maximum number of tokens for the API call.- Returns:- str: The AI's response.- """- cfg = Config()- if temperature is None:- temperature = cfg.temperature- if deployment_id is not None:- response = openai.ChatCompletion.create(- deployment_id=deployment_id,- model=model,- messages=messages,- temperature=temperature,- max_tokens=max_tokens,- api_key=cfg.openai_api_key,- )- else:- response = openai.ChatCompletion.create(- model=model,- messages=messages,- temperature=temperature,- max_tokens=max_tokens,- api_key=cfg.openai_api_key,- )- if not hasattr(response, "error"):- logger.debug(f"Response: {response}")- prompt_tokens = response.usage.prompt_tokens- completion_tokens = response.usage.completion_tokens- self.update_cost(prompt_tokens, completion_tokens, model)- return response--- def update_cost(self, prompt_tokens, completion_tokens, model: str):- def update_cost(self, prompt_tokens, completion_tokens, model):++++ def update_cost(self, prompt_tokens, completion_tokens, model)"""Update the total cost, prompt tokens, and completion tokens.diff --cc autogpt/speech/base.pyindex 07c8d9fe7b,a75acaeded..7b8e7146b4--- a/autogpt/speech/base.py+++ b/autogpt/speech/base.py@@@ -1,8 -1,8 +1,9 @@@"""Base class for all voice classes."""import abc+import refrom threading import Lock+ from autogpt.config import Configfrom autogpt.singleton import AbstractSingletondiff --cc tests/unit/test_api_manager.pyindex e259f56adc,2b4ad90a43..4a21a891bc--- a/tests/unit/test_api_manager.py+++ b/tests/unit/test_api_manager.py@@@ -1,9 -1,8 +1,9 @@@- from unittest.mock import MagicMock, patch+ from unittest.mock import patchimport pytest+from pytest_mock import MockerFixture-from autogpt.llm.api_manager import COSTS, ApiManager+from autogpt.llm.api_manager import OPEN_AI_MODELS, ApiManagerapi_manager = ApiManager()@@@ -29,75 -27,13 +29,13 @@@ def mock_costs(mocker: MockerFixture)class TestApiManager:- @staticmethod- def test_create_chat_completion_debug_mode(caplog):- """Test if debug mode logs response."""- api_manager_debug = ApiManager(debug=True)- messages = [- {"role": "system", "content": "You are a helpful assistant."},- {"role": "user", "content": "Who won the world series in 2020?"},- ]- model = "gpt-3.5-turbo"-- with patch("openai.ChatCompletion.create") as mock_create:- mock_response = MagicMock()- del mock_response.error- mock_response.usage.prompt_tokens = 10- mock_response.usage.completion_tokens = 20- mock_create.return_value = mock_response-- api_manager_debug.create_chat_completion(messages, model=model)-- assert "Response" in caplog.text-- @staticmethod- def test_create_chat_completion_empty_messages():- """Test if empty messages result in zero tokens and cost."""- messages = []- model = "gpt-3.5-turbo"-- with patch("openai.ChatCompletion.create") as mock_create:- mock_response = MagicMock()- del mock_response.error- mock_response.usage.prompt_tokens = 0- mock_response.usage.completion_tokens = 0- mock_create.return_value = mock_response-- api_manager.create_chat_completion(messages, model=model)-- assert api_manager.get_total_prompt_tokens() == 0- assert api_manager.get_total_completion_tokens() == 0- assert api_manager.get_total_cost() == 0-- @staticmethod- def test_create_chat_completion_valid_inputs():- """Test if valid inputs result in correct tokens and cost."""- messages = [- {"role": "system", "content": "You are a helpful assistant."},- {"role": "user", "content": "Who won the world series in 2020?"},- ]- model = "gpt-3.5-turbo"-- with patch("openai.ChatCompletion.create") as mock_create:- mock_response = MagicMock()- del mock_response.error- mock_response.usage.prompt_tokens = 10- mock_response.usage.completion_tokens = 20- mock_create.return_value = mock_response-- api_manager.create_chat_completion(messages, model=model)-- assert api_manager.get_total_prompt_tokens() == 10- assert api_manager.get_total_completion_tokens() == 20- assert api_manager.get_total_cost() == (10 * 0.0013 + 20 * 0.0025) / 1000-def test_getter_methods(self):"""Test the getter methods for total tokens, cost, and budget."""- api_manager.update_cost(60, 120, "gpt-3.5-turbo")+ api_manager.update_cost(600, 1200, "gpt-3.5-turbo")api_manager.set_total_budget(10.0)- assert api_manager.get_total_prompt_tokens() == 60- assert api_manager.get_total_completion_tokens() == 120- assert api_manager.get_total_cost() == (60 * 0.002 + 120 * 0.002) / 1000+ assert api_manager.get_total_prompt_tokens() == 600+ assert api_manager.get_total_completion_tokens() == 1200+ assert api_manager.get_total_cost() == (600 * 0.0013 + 1200 * 0.0025) / 1000assert api_manager.get_total_budget() == 10.0@staticmethod