From 71ca4ea9904ecd07a1287fa5b146490be6bd2945 Mon Sep 17 00:00:00 2001 From: Luke <2609441+lc0rp@users.noreply.github.com> Date: Mon, 19 Jun 2023 19:36:46 +0000 Subject: [PATCH] Updates to sync 0.4.1 to master --- autogpt/app.py | 6 ---- tests/unit/test_api_manager.py | 64 +++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/autogpt/app.py b/autogpt/app.py index 8586653c..78e3a4dd 100644 --- a/autogpt/app.py +++ b/autogpt/app.py @@ -3,12 +3,6 @@ import json from typing import Dict from 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_url def is_valid_int(value: str) -> bool: diff --git a/tests/unit/test_api_manager.py b/tests/unit/test_api_manager.py index 4a21a891..e259f56a 100644 --- a/tests/unit/test_api_manager.py +++ b/tests/unit/test_api_manager.py @@ -1,4 +1,4 @@ -from unittest.mock import patch +from unittest.mock import MagicMock, patch import pytest from pytest_mock import MockerFixture @@ -29,6 +29,68 @@ 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(600, 1200, "gpt-3.5-turbo")