Skip to content

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.

Fix test API manager

Merwane Hamadi162d77707b0d
diff --git a/tests/unit/test_api_manager.py b/tests/unit/test_api_manager.py
index c29aadf5d6..04242d576d 100644
--- a/tests/unit/test_api_manager.py
+++ b/tests/unit/test_api_manager.py
@@ -4,7 +4,7 @@ import pytest
from pytest_mock import MockerFixture
from autogpt.llm.api_manager import ApiManager
-from autogpt.llm.providers.openai import OPEN_AI_MODELS
+from autogpt.llm.providers.openai import OPEN_AI_CHAT_MODELS, OPEN_AI_EMBEDDING_MODELS
api_manager = ApiManager()
@@ -18,80 +18,18 @@ def reset_api_manager():
@pytest.fixture(autouse=True)
def mock_costs(mocker: MockerFixture):
mocker.patch.multiple(
- OPEN_AI_MODELS["gpt-3.5-turbo"],
+ OPEN_AI_CHAT_MODELS["gpt-3.5-turbo"],
prompt_token_cost=0.0013,
completion_token_cost=0.0025,
)
mocker.patch.multiple(
- OPEN_AI_MODELS["text-embedding-ada-002"],
+ OPEN_AI_EMBEDDING_MODELS["text-embedding-ada-002"],
prompt_token_cost=0.0004,
)
yield
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(600, 1200, "gpt-3.5-turbo")