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.
Organize all the llm stuff into a subpackage (#3436)
James Collins3b74d2150e26
diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.pyindex fd6a7cb6e1..9e0f54e36b 100644--- a/autogpt/agent/agent.py+++ b/autogpt/agent/agent.py@@ -1,11 +1,10 @@from colorama import Fore, Stylefrom autogpt.app import execute_command, get_command-from autogpt.chat import chat_with_ai, create_chat_messagefrom autogpt.config import Configfrom autogpt.json_utils.json_fix_llm import fix_json_using_multiple_techniquesfrom autogpt.json_utils.utilities import validate_json-from autogpt.llm_utils import create_chat_completion+from autogpt.llm import chat_with_ai, create_chat_completion, create_chat_messagefrom autogpt.logs import logger, print_assistant_thoughtsfrom autogpt.speech import say_textfrom autogpt.spinner import Spinnerdiff --git a/autogpt/agent/agent_manager.py b/autogpt/agent/agent_manager.pyindex 257127a303..1283fdaefb 100644--- a/autogpt/agent/agent_manager.py+++ b/autogpt/agent/agent_manager.py@@ -4,7 +4,7 @@ from __future__ import annotationsfrom typing import Listfrom autogpt.config.config import Config-from autogpt.llm_utils import create_chat_completion+from autogpt.llm import create_chat_completionfrom autogpt.singleton import Singletonfrom autogpt.types.openai import Messagediff --git a/autogpt/commands/analyze_code.py b/autogpt/commands/analyze_code.pyindex 47cfc1e765..cff8a737e1 100644--- a/autogpt/commands/analyze_code.py+++ b/autogpt/commands/analyze_code.py@@ -2,7 +2,7 @@from __future__ import annotationsfrom autogpt.commands.command import command-from autogpt.llm_utils import call_ai_function+from autogpt.llm import call_ai_function@command(diff --git a/autogpt/commands/improve_code.py b/autogpt/commands/improve_code.pyindex f953cf2bd3..ff4cf48c0d 100644--- a/autogpt/commands/improve_code.py+++ b/autogpt/commands/improve_code.py@@ -3,7 +3,7 @@ from __future__ import annotationsimport jsonfrom autogpt.commands.command import command-from autogpt.llm_utils import call_ai_function+from autogpt.llm import call_ai_function@command(diff --git a/autogpt/commands/write_tests.py b/autogpt/commands/write_tests.pyindex 91cd930429..c6a0332474 100644--- a/autogpt/commands/write_tests.py+++ b/autogpt/commands/write_tests.py@@ -4,7 +4,7 @@ from __future__ import annotationsimport jsonfrom autogpt.commands.command import command-from autogpt.llm_utils import call_ai_function+from autogpt.llm import call_ai_function@command(diff --git a/autogpt/json_utils/json_fix_llm.py b/autogpt/json_utils/json_fix_llm.pyindex 10317accb9..73f766480d 100644--- a/autogpt/json_utils/json_fix_llm.py+++ b/autogpt/json_utils/json_fix_llm.py@@ -11,7 +11,7 @@ from regex import regexfrom autogpt.config import Configfrom autogpt.json_utils.json_fix_general import correct_json-from autogpt.llm_utils import call_ai_function+from autogpt.llm import call_ai_functionfrom autogpt.logs import loggerfrom autogpt.speech import say_textdiff --git a/autogpt/llm/__init__.py b/autogpt/llm/__init__.pynew file mode 100644index 0000000000..3a958285de--- /dev/null+++ b/autogpt/llm/__init__.py@@ -0,0 +1,22 @@+from autogpt.llm.api_manager import ApiManager+from autogpt.llm.chat import chat_with_ai, create_chat_message, generate_context+from autogpt.llm.llm_utils import (+ call_ai_function,+ create_chat_completion,+ get_ada_embedding,+)+from autogpt.llm.modelsinfo import COSTS+from autogpt.llm.token_counter import count_message_tokens, count_string_tokens++__all__ = [+ "ApiManager",+ "create_chat_message",+ "generate_context",+ "chat_with_ai",+ "call_ai_function",+ "create_chat_completion",+ "get_ada_embedding",+ "COSTS",+ "count_message_tokens",+ "count_string_tokens",+]diff --git a/autogpt/api_manager.py b/autogpt/llm/api_manager.pysimilarity index 98%rename from autogpt/api_manager.pyrename to autogpt/llm/api_manager.pyindex b16f3af710..9143389e88 100644--- a/autogpt/api_manager.py+++ b/autogpt/llm/api_manager.py@@ -3,8 +3,8 @@ from __future__ import annotationsimport openaifrom autogpt.config import Config+from autogpt.llm.modelsinfo import COSTSfrom autogpt.logs import logger-from autogpt.modelsinfo import COSTSfrom autogpt.singleton import Singletondiff --git a/autogpt/chat.py b/autogpt/llm/chat.pysimilarity index 94%rename from autogpt/chat.pyrename to autogpt/llm/chat.pyindex 469ec9bd38..9bfd37e025 100644--- a/autogpt/chat.py+++ b/autogpt/llm/chat.py@@ -3,10 +3,10 @@ from random import shufflefrom openai.error import RateLimitError-from autogpt import token_counter-from autogpt.api_manager import ApiManagerfrom autogpt.config import Config-from autogpt.llm_utils import create_chat_completion+from autogpt.llm.api_manager import ApiManager+from autogpt.llm.llm_utils import create_chat_completion+from autogpt.llm.token_counter import count_message_tokensfrom autogpt.logs import loggerfrom autogpt.types.openai import Message@@ -43,7 +43,7 @@ def generate_context(prompt, relevant_memory, full_message_history, model):next_message_to_add_index = len(full_message_history) - 1insertion_index = len(current_context)# Count the currently used tokens- current_tokens_used = token_counter.count_message_tokens(current_context, model)+ current_tokens_used = count_message_tokens(current_context, model)return (next_message_to_add_index,current_tokens_used,@@ -114,7 +114,7 @@ def chat_with_ai(prompt, relevant_memory, full_message_history, model)- current_tokens_used += token_counter.count_message_tokens(+ current_tokens_used += count_message_tokens([create_chat_message("user", user_input)], model) # Account for user input (appended later)@@ -122,9 +122,7 @@ def chat_with_ai(# print (f"CURRENT TOKENS USED: {current_tokens_used}")message_to_add = full_message_history[next_message_to_add_index]- tokens_to_add = token_counter.count_message_tokens(- [message_to_add], model- )+ tokens_to_add = count_message_tokens([message_to_add], model)if current_tokens_used + tokens_to_add > send_token_limit:break@@ -175,7 +173,7 @@ def chat_with_ai()if not plugin_response or plugin_response == "":continue- tokens_to_add = token_counter.count_message_tokens(+ tokens_to_add = count_message_tokens([create_chat_message("system", plugin_response)], model)if current_tokens_used + tokens_to_add > send_token_limit:diff --git a/autogpt/llm_utils.py b/autogpt/llm/llm_utils.pysimilarity index 99%rename from autogpt/llm_utils.pyrename to autogpt/llm/llm_utils.pyindex 11f8da3a16..c1ba5fa5e0 100644--- a/autogpt/llm_utils.py+++ b/autogpt/llm/llm_utils.py@@ -8,8 +8,8 @@ import openaifrom colorama import Fore, Stylefrom openai.error import APIError, RateLimitError, Timeout-from autogpt.api_manager import ApiManagerfrom autogpt.config import Config+from autogpt.llm.api_manager import ApiManagerfrom autogpt.logs import loggerfrom autogpt.types.openai import Messagediff --git a/autogpt/modelsinfo.py b/autogpt/llm/modelsinfo.pysimilarity index 100%rename from autogpt/modelsinfo.pyrename to autogpt/llm/modelsinfo.pydiff --git a/autogpt/token_counter.py b/autogpt/llm/token_counter.pysimilarity index 100%rename from autogpt/token_counter.pyrename to autogpt/llm/token_counter.pydiff --git a/autogpt/logs.py b/autogpt/logs.pyindex fc529f25b3..c1ac126cde 100644--- a/autogpt/logs.py+++ b/autogpt/logs.py@@ -1,11 +1,9 @@"""Logging module for Auto-GPT."""-import jsonimport loggingimport osimport randomimport reimport time-import tracebackfrom logging import LogRecordfrom colorama import Fore, Stylediff --git a/autogpt/memory/local.py b/autogpt/memory/local.pyindex 87d83cc596..18b7255cb5 100644--- a/autogpt/memory/local.py+++ b/autogpt/memory/local.py@@ -7,7 +7,7 @@ from typing import Any, Listimport numpy as npimport orjson-from autogpt.llm_utils import get_ada_embedding+from autogpt.llm import get_ada_embeddingfrom autogpt.memory.base import MemoryProviderSingletonEMBED_DIM = 1536diff --git a/autogpt/memory/milvus.py b/autogpt/memory/milvus.pyindex 41f9ed1309..40694df558 100644--- a/autogpt/memory/milvus.py+++ b/autogpt/memory/milvus.py@@ -4,7 +4,7 @@ import refrom pymilvus import Collection, CollectionSchema, DataType, FieldSchema, connectionsfrom autogpt.config import Config-from autogpt.llm_utils import get_ada_embedding+from autogpt.llm import get_ada_embeddingfrom autogpt.memory.base import MemoryProviderSingletondiff --git a/autogpt/memory/pinecone.py b/autogpt/memory/pinecone.pyindex 3aa78f9edf..a0bd162ac5 100644--- a/autogpt/memory/pinecone.py+++ b/autogpt/memory/pinecone.py@@ -1,7 +1,7 @@import pineconefrom colorama import Fore, Style-from autogpt.llm_utils import get_ada_embedding+from autogpt.llm import get_ada_embeddingfrom autogpt.logs import loggerfrom autogpt.memory.base import MemoryProviderSingletondiff --git a/autogpt/memory/redismem.py b/autogpt/memory/redismem.pyindex 5618bb37f9..6b51277860 100644--- a/autogpt/memory/redismem.py+++ b/autogpt/memory/redismem.py@@ -10,7 +10,7 @@ from redis.commands.search.field import TextField, VectorFieldfrom redis.commands.search.indexDefinition import IndexDefinition, IndexTypefrom redis.commands.search.query import Query-from autogpt.llm_utils import get_ada_embedding+from autogpt.llm import get_ada_embeddingfrom autogpt.logs import loggerfrom autogpt.memory.base import MemoryProviderSingletondiff --git a/autogpt/memory/weaviate.py b/autogpt/memory/weaviate.pyindex fbebbfd716..be29e1b98e 100644--- a/autogpt/memory/weaviate.py+++ b/autogpt/memory/weaviate.py@@ -3,7 +3,7 @@ from weaviate import Clientfrom weaviate.embedded import EmbeddedOptionsfrom weaviate.util import generate_uuid5-from autogpt.llm_utils import get_ada_embedding+from autogpt.llm import get_ada_embeddingfrom autogpt.memory.base import MemoryProviderSingletondiff --git a/autogpt/processing/text.py b/autogpt/processing/text.pyindex 9946951a7c..372e331d56 100644--- a/autogpt/processing/text.py+++ b/autogpt/processing/text.py@@ -4,9 +4,8 @@ from typing import Dict, Generator, Optionalimport spacyfrom selenium.webdriver.remote.webdriver import WebDriver-from autogpt import token_counterfrom autogpt.config import Config-from autogpt.llm_utils import create_chat_completion+from autogpt.llm import count_message_tokens, create_chat_completionfrom autogpt.memory import get_memoryCFG = Config()@@ -44,7 +43,7 @@ def split_text(]expected_token_usage = (- token_usage_of_chunk(messages=message_with_additional_sentence, model=model)+ count_message_tokens(messages=message_with_additional_sentence, model=model)+ 1)if expected_token_usage <= max_length:@@ -56,7 +55,7 @@ def split_text(create_message(" ".join(current_chunk), question)]expected_token_usage = (- token_usage_of_chunk(messages=message_this_sentence_only, model=model)+ count_message_tokens(messages=message_this_sentence_only, model=model)+ 1)if expected_token_usage > max_length:@@ -68,10 +67,6 @@ def split_text(yield " ".join(current_chunk)-def token_usage_of_chunk(messages, model):- return token_counter.count_message_tokens(messages, model)--def summarize_text(url: str, text: str, question: str, driver: Optional[WebDriver] = None) -> str:@@ -112,7 +107,7 @@ def summarize_text(memory.add(memory_to_add)messages = [create_message(chunk, question)]- tokens_for_chunk = token_counter.count_message_tokens(messages, model)+ tokens_for_chunk = count_message_tokens(messages, model)print(f"Summarizing chunk {i + 1} / {len(chunks)} of length {len(chunk)} characters, or {tokens_for_chunk} tokens")diff --git a/autogpt/prompts/prompt.py b/autogpt/prompts/prompt.pyindex cbfcfca93d..7a53f60313 100644--- a/autogpt/prompts/prompt.py+++ b/autogpt/prompts/prompt.py@@ -1,8 +1,8 @@from colorama import Fore-from autogpt.api_manager import ApiManagerfrom autogpt.config.ai_config import AIConfigfrom autogpt.config.config import Config+from autogpt.llm import ApiManagerfrom autogpt.logs import loggerfrom autogpt.prompts.generator import PromptGeneratorfrom autogpt.setup import prompt_userdiff --git a/autogpt/setup.py b/autogpt/setup.pyindex e3c251ecc2..ddc7c6bf43 100644--- a/autogpt/setup.py+++ b/autogpt/setup.py@@ -6,7 +6,7 @@ from colorama import Fore, Stylefrom autogpt import utilsfrom autogpt.config import Configfrom autogpt.config.ai_config import AIConfig-from autogpt.llm_utils import create_chat_completion+from autogpt.llm import create_chat_completionfrom autogpt.logs import loggerCFG = Config()diff --git a/tests/conftest.py b/tests/conftest.pyindex e3c590d288..da00058b7f 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -3,8 +3,8 @@ from pathlib import Pathimport pytestfrom pytest_mock import MockerFixture-from autogpt.api_manager import ApiManagerfrom autogpt.config import Config+from autogpt.llm import ApiManagerfrom autogpt.workspace import Workspacepytest_plugins = ["tests.integration.agent_factory"]diff --git a/tests/integration/test_llm_utils.py b/tests/integration/test_llm_utils.pyindex 7f312bde19..74b860160f 100644--- a/tests/integration/test_llm_utils.py+++ b/tests/integration/test_llm_utils.py@@ -3,7 +3,7 @@ import stringimport pytestfrom numpy.random import RandomState-from autogpt.llm_utils import get_ada_embedding+from autogpt.llm.llm_utils import get_ada_embeddingfrom tests.utils import requires_api_keydiff --git a/tests/integration/weaviate_memory_tests.py b/tests/integration/weaviate_memory_tests.pyindex f408577554..2cea871c61 100644--- a/tests/integration/weaviate_memory_tests.py+++ b/tests/integration/weaviate_memory_tests.py@@ -5,7 +5,7 @@ from weaviate import Clientfrom weaviate.util import get_valid_uuidfrom autogpt.config import Config-from autogpt.llm_utils import get_ada_embedding+from autogpt.llm import get_ada_embeddingfrom autogpt.memory.weaviate import WeaviateMemorydiff --git a/tests/test_agent.py b/tests/test_agent.pyindex 9c67f7e41e..e859673159 100644--- a/tests/test_agent.py+++ b/tests/test_agent.py@@ -3,10 +3,7 @@ from unittest.mock import MagicMockimport pytestfrom autogpt.agent import Agent-from autogpt.chat import chat_with_aifrom autogpt.config import Config-from autogpt.speech import say_text-from autogpt.utils import clean_input@pytest.fixturediff --git a/tests/test_agent_manager.py b/tests/test_agent_manager.pyindex d1bb3d8b73..e7e696ae5c 100644--- a/tests/test_agent_manager.py+++ b/tests/test_agent_manager.py@@ -1,7 +1,7 @@import pytestfrom autogpt.agent.agent_manager import AgentManager-from autogpt.llm_utils import create_chat_completion+from autogpt.llm import create_chat_completion@pytest.fixturediff --git a/tests/test_api_manager.py b/tests/test_api_manager.pyindex 71aa6d63b1..ba64a72f87 100644--- a/tests/test_api_manager.py+++ b/tests/test_api_manager.py@@ -2,7 +2,7 @@ from unittest.mock import MagicMock, patchimport pytest-from autogpt.api_manager import COSTS, ApiManager+from autogpt.llm import COSTS, ApiManagerapi_manager = ApiManager()diff --git a/tests/test_llm_utils.py b/tests/test_llm_utils.pyindex 83cac0de53..27c9ff883a 100644--- a/tests/test_llm_utils.py+++ b/tests/test_llm_utils.py@@ -1,8 +1,8 @@import pytestfrom openai.error import APIError, RateLimitError-from autogpt.llm_utils import get_ada_embedding, retry_openai_api-from autogpt.modelsinfo import COSTS+from autogpt.llm import COSTS, get_ada_embedding+from autogpt.llm.llm_utils import retry_openai_api@pytest.fixture(params=[RateLimitError, APIError])@@ -19,7 +19,7 @@ def mock_create_embedding(mocker):mock_response.usage.prompt_tokens = 5mock_response.__getitem__.side_effect = lambda key: [{"embedding": [0.1, 0.2, 0.3]}]return mocker.patch(- "autogpt.llm_utils.create_embedding", return_value=mock_response+ "autogpt.llm.llm_utils.create_embedding", return_value=mock_response)diff --git a/tests/test_token_counter.py b/tests/test_token_counter.pyindex ac25796daf..966a0aff11 100644--- a/tests/test_token_counter.py+++ b/tests/test_token_counter.py@@ -1,7 +1,6 @@import pytest-import tests.context-from autogpt.token_counter import count_message_tokens, count_string_tokens+from autogpt.llm import count_message_tokens, count_string_tokensdef test_count_message_tokens():diff --git a/tests/unit/test_chat.py b/tests/unit/test_chat.pyindex 61ea773c96..9cb7c625bd 100644--- a/tests/unit/test_chat.py+++ b/tests/unit/test_chat.py@@ -2,7 +2,7 @@import timefrom unittest.mock import patch-from autogpt.chat import create_chat_message, generate_context+from autogpt.llm import create_chat_message, generate_contextdef test_happy_path_role_content():diff --git a/tests/unit/test_get_self_feedback.py b/tests/unit/test_get_self_feedback.pyindex 54641f9168..09707270d3 100644--- a/tests/unit/test_get_self_feedback.py+++ b/tests/unit/test_get_self_feedback.py@@ -1,6 +1,6 @@from autogpt.agent.agent import Agentfrom autogpt.config import AIConfig-from autogpt.llm_utils import create_chat_completion+from autogpt.llm import create_chat_completiondef test_get_self_feedback(mocker):