mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-18 22:44:21 +01:00
* Implemented running cost counter for chat completions This data is known to the AI as additional system context, and is printed out to the user * Added comments to api_manager.py * Added user-defined API budget. The user is now prompted if they want to give the AI a budget for API calls. If they enter nothing, there is no monetary limit, but if they define a budget then the AI will be told to shut down gracefully once it has come within 1 cent of its limit, and to shut down immediately once it has exceeded its limit. If a budget is defined, Auto-GPT is always aware of how much it was given and how much remains to be spent. * Chat completion calls are now done through api_manager. Total running cost is printed. * Implemented api budget setting and tracking User can now configure a maximum api budget, and the AI is aware of that and its remaining budget. The AI is instructed to shut down when exceeding the budget. * Update autogpt/api_manager.py Change "per token" to "per 1000 tokens" in a comment on the api cost Co-authored-by: Rob Luke <code@robertluke.net> * Fixed lint errors * Include embedding costs * Add embedding completion cost * lint * Added 'requires_api_key' decorator to test_commands.py, switched to a valid chat completions model * Refactor API manager, add debug mode, and add tests - Extract model costs to to avoid duplication - Add debug mode parameter to ApiManager class - Move debug mode configuration to - Log AI response and budget messages in debug mode - Implement 'test_api_manager.py' * Fixed test_setup failing. An extra user input is needed for api budget * Linting --------- Co-authored-by: Rob Luke <code@robertluke.net> Co-authored-by: Nicholas Tindle <nick@ntindle.com>
79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
import unittest
|
|
from io import StringIO
|
|
from unittest.mock import patch
|
|
|
|
from autogpt.config.ai_config import AIConfig
|
|
from autogpt.setup import (
|
|
generate_aiconfig_automatic,
|
|
generate_aiconfig_manual,
|
|
prompt_user,
|
|
)
|
|
from tests.utils import requires_api_key
|
|
|
|
|
|
class TestAutoGPT(unittest.TestCase):
|
|
@requires_api_key("OPENAI_API_KEY")
|
|
def test_generate_aiconfig_automatic_default(self):
|
|
user_inputs = [""]
|
|
with patch("builtins.input", side_effect=user_inputs):
|
|
ai_config = prompt_user()
|
|
|
|
self.assertIsInstance(ai_config, AIConfig)
|
|
self.assertIsNotNone(ai_config.ai_name)
|
|
self.assertIsNotNone(ai_config.ai_role)
|
|
self.assertGreaterEqual(len(ai_config.ai_goals), 1)
|
|
self.assertLessEqual(len(ai_config.ai_goals), 5)
|
|
|
|
@requires_api_key("OPENAI_API_KEY")
|
|
def test_generate_aiconfig_automatic_typical(self):
|
|
user_prompt = "Help me create a rock opera about cybernetic giraffes"
|
|
ai_config = generate_aiconfig_automatic(user_prompt)
|
|
|
|
self.assertIsInstance(ai_config, AIConfig)
|
|
self.assertIsNotNone(ai_config.ai_name)
|
|
self.assertIsNotNone(ai_config.ai_role)
|
|
self.assertGreaterEqual(len(ai_config.ai_goals), 1)
|
|
self.assertLessEqual(len(ai_config.ai_goals), 5)
|
|
|
|
@requires_api_key("OPENAI_API_KEY")
|
|
def test_generate_aiconfig_automatic_fallback(self):
|
|
user_inputs = [
|
|
"T&GF£OIBECC()!*",
|
|
"Chef-GPT",
|
|
"an AI designed to browse bake a cake.",
|
|
"Purchase ingredients",
|
|
"Bake a cake",
|
|
"",
|
|
"",
|
|
]
|
|
with patch("builtins.input", side_effect=user_inputs):
|
|
ai_config = prompt_user()
|
|
|
|
self.assertIsInstance(ai_config, AIConfig)
|
|
self.assertEqual(ai_config.ai_name, "Chef-GPT")
|
|
self.assertEqual(ai_config.ai_role, "an AI designed to browse bake a cake.")
|
|
self.assertEqual(ai_config.ai_goals, ["Purchase ingredients", "Bake a cake"])
|
|
|
|
@requires_api_key("OPENAI_API_KEY")
|
|
def test_prompt_user_manual_mode(self):
|
|
user_inputs = [
|
|
"--manual",
|
|
"Chef-GPT",
|
|
"an AI designed to browse bake a cake.",
|
|
"Purchase ingredients",
|
|
"Bake a cake",
|
|
"",
|
|
"",
|
|
]
|
|
with patch("builtins.input", side_effect=user_inputs):
|
|
ai_config = prompt_user()
|
|
|
|
self.assertIsInstance(ai_config, AIConfig)
|
|
self.assertEqual(ai_config.ai_name, "Chef-GPT")
|
|
self.assertEqual(ai_config.ai_role, "an AI designed to browse bake a cake.")
|
|
self.assertEqual(ai_config.ai_goals, ["Purchase ingredients", "Bake a cake"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|