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.
Have api manager use singleton pattern (#3269)
James Collins0ff471a49a04
diff --git a/autogpt/api_manager.py b/autogpt/api_manager.pyindex ace64fbdaf..52b94ff7ac 100644--- a/autogpt/api_manager.py+++ b/autogpt/api_manager.py@@ -1,22 +1,19 @@-from typing import List+from __future__ import annotationsimport openaifrom autogpt.config import Configfrom autogpt.logs import loggerfrom autogpt.modelsinfo import COSTS+from autogpt.singleton import Singleton-cfg = Config()-print_total_cost = cfg.debug_mode--class ApiManager:- def __init__(self, debug=False):+class ApiManager(metaclass=Singleton):+ def __init__(self):self.total_prompt_tokens = 0self.total_completion_tokens = 0self.total_cost = 0self.total_budget = 0- self.debug = debugdef reset(self):self.total_prompt_tokens = 0@@ -28,7 +25,7 @@ class ApiManager:self,messages: list, # type: ignoremodel: str | None = None,- temperature: float = cfg.temperature,+ temperature: float = None,max_tokens: int | None = None,deployment_id=None,) -> str:@@ -42,6 +39,9 @@ class ApiManager:Returns:str: The AI's response."""+ cfg = Config()+ if temperature is None:+ temperature = cfg.temperatureif deployment_id is not None:response = openai.ChatCompletion.create(deployment_id=deployment_id,@@ -59,8 +59,7 @@ class ApiManager:max_tokens=max_tokens,api_key=cfg.openai_api_key,)- if self.debug:- logger.debug(f"Response: {response}")+ logger.debug(f"Response: {response}")prompt_tokens = response.usage.prompt_tokenscompletion_tokens = response.usage.completion_tokensself.update_cost(prompt_tokens, completion_tokens, model)@@ -81,8 +80,7 @@ class ApiManager:prompt_tokens * COSTS[model]["prompt"]+ completion_tokens * COSTS[model]["completion"]) / 1000- if print_total_cost:- print(f"Total running cost: ${self.total_cost:.3f}")+ logger.debug(f"Total running cost: ${self.total_cost:.3f}")def set_total_budget(self, total_budget):"""@@ -128,6 +126,3 @@ class ApiManager:float: The total budget for API calls."""return self.total_budget---api_manager = ApiManager(cfg.debug_mode)diff --git a/autogpt/chat.py b/autogpt/chat.pyindex 21eab6a08e..4b906a0015 100644--- a/autogpt/chat.py+++ b/autogpt/chat.py@@ -3,7 +3,7 @@ import timefrom openai.error import RateLimitErrorfrom autogpt import token_counter-from autogpt.api_manager import api_manager+from autogpt.api_manager import ApiManagerfrom autogpt.config import Configfrom autogpt.llm_utils import create_chat_completionfrom autogpt.logs import logger@@ -134,6 +134,7 @@ def chat_with_ai(# Move to the next most recent message in the full message historynext_message_to_add_index -= 1+ api_manager = ApiManager()# inform the AI about its remaining budget (if it has one)if api_manager.get_total_budget() > 0.0:remaining_budget = (diff --git a/autogpt/llm_utils.py b/autogpt/llm_utils.pyindex 212928ca08..a98b12a327 100644--- a/autogpt/llm_utils.py+++ b/autogpt/llm_utils.py@@ -8,7 +8,7 @@ import openaifrom colorama import Fore, Stylefrom openai.error import APIError, RateLimitError, Timeout-from autogpt.api_manager import api_manager+from autogpt.api_manager import ApiManagerfrom autogpt.config import Configfrom autogpt.logs import loggerfrom autogpt.types.openai import Message@@ -147,6 +147,7 @@ def create_chat_completion()if message is not None:return message+ api_manager = ApiManager()response = Nonefor attempt in range(num_retries):backoff = 2 ** (attempt + 2)@@ -228,6 +229,7 @@ def get_ada_embedding(text: str) -> List[float]:kwargs = {"model": model}embedding = create_embedding(text, **kwargs)+ api_manager = ApiManager()api_manager.update_cost(prompt_tokens=embedding.usage.prompt_tokens,completion_tokens=0,diff --git a/autogpt/memory/base.py b/autogpt/memory/base.pyindex a6c92db6fd..d99cadde8e 100644--- a/autogpt/memory/base.py+++ b/autogpt/memory/base.py@@ -1,11 +1,8 @@"""Base class for memory providers."""import abc-from autogpt.config import Configfrom autogpt.singleton import AbstractSingleton-cfg = Config()-class MemoryProviderSingleton(AbstractSingleton):@abc.abstractmethoddiff --git a/autogpt/prompts/prompt.py b/autogpt/prompts/prompt.pyindex cc06fabb1f..68d8a3362e 100644--- a/autogpt/prompts/prompt.py+++ b/autogpt/prompts/prompt.py@@ -1,6 +1,6 @@from colorama import Fore-from autogpt.api_manager import api_manager+from autogpt.api_manager import ApiManagerfrom autogpt.config.ai_config import AIConfigfrom autogpt.config.config import Configfrom autogpt.logs import logger@@ -115,6 +115,7 @@ Continue (y/n): """config.save(CFG.ai_settings_file)# set the total api budget+ api_manager = ApiManager()api_manager.set_total_budget(config.api_budget)# Agent Created, print messagediff --git a/tests/conftest.py b/tests/conftest.pyindex 9ab4918933..745f8b5dd8 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -3,7 +3,6 @@ from pathlib import Pathimport pytestfrom autogpt.api_manager import ApiManager-from autogpt.api_manager import api_manager as api_manager_from autogpt.config import Configfrom autogpt.workspace import Workspace@@ -32,7 +31,6 @@ def config(workspace: Workspace) -> Config:@pytest.fixture()def api_manager() -> ApiManager:- old_attrs = api_manager_.__dict__.copy()- api_manager_.reset()- yield api_manager_- api_manager_.__dict__.update(old_attrs)+ if ApiManager in ApiManager._instances:+ del ApiManager._instances[ApiManager]+ return ApiManager()diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.pyindex a749cc6dfa..8596e1c9bb 100644--- a/tests/unit/test_commands.py+++ b/tests/unit/test_commands.py@@ -3,8 +3,7 @@ from unittest.mock import MagicMock, patchimport pytest-import autogpt.agent.agent_manager as agent_manager-from autogpt.app import execute_command, list_agents, start_agent+from autogpt.app import list_agents, start_agentfrom tests.utils import requires_api_key@@ -14,9 +13,11 @@ def test_make_agent() -> None:"""Test that an agent can be created"""# Use the mock agent manager to avoid creating a real agentwith patch("openai.ChatCompletion.create") as mock:- obj = MagicMock()- obj.response.choices[0].messages[0].content = "Test message"- mock.return_value = obj+ response = MagicMock()+ response.choices[0].messages[0].content = "Test message"+ response.usage.prompt_tokens = 1+ response.usage.completion_tokens = 1+ mock.return_value = responsestart_agent("Test Agent", "chat", "Hello, how are you?", "gpt-3.5-turbo")agents = list_agents()assert "List of agents:\n0: chat" == agents