Skip to content

Commit

Permalink
Organize all the llm stuff into a subpackage (Significant-Gravitas#3436)
Browse files Browse the repository at this point in the history
  • Loading branch information
collijk authored Apr 28, 2023
1 parent ee4043a commit 3b74d21
Show file tree
Hide file tree
Showing 31 changed files with 59 additions and 51 deletions.
3 changes: 1 addition & 2 deletions autogpt/agent/agent.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from colorama import Fore, Style

from autogpt.app import execute_command, get_command
from autogpt.chat import chat_with_ai, create_chat_message
from autogpt.config import Config
from autogpt.json_utils.json_fix_llm import fix_json_using_multiple_techniques
from 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_message
from autogpt.logs import logger, print_assistant_thoughts
from autogpt.speech import say_text
from autogpt.spinner import Spinner
Expand Down
2 changes: 1 addition & 1 deletion autogpt/agent/agent_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List

from autogpt.config.config import Config
from autogpt.llm_utils import create_chat_completion
from autogpt.llm import create_chat_completion
from autogpt.singleton import Singleton
from autogpt.types.openai import Message

Expand Down
2 changes: 1 addition & 1 deletion autogpt/commands/analyze_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

from autogpt.commands.command import command
from autogpt.llm_utils import call_ai_function
from autogpt.llm import call_ai_function


@command(
Expand Down
2 changes: 1 addition & 1 deletion autogpt/commands/improve_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json

from autogpt.commands.command import command
from autogpt.llm_utils import call_ai_function
from autogpt.llm import call_ai_function


@command(
Expand Down
2 changes: 1 addition & 1 deletion autogpt/commands/write_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json

from autogpt.commands.command import command
from autogpt.llm_utils import call_ai_function
from autogpt.llm import call_ai_function


@command(
Expand Down
2 changes: 1 addition & 1 deletion autogpt/json_utils/json_fix_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from autogpt.config import Config
from autogpt.json_utils.json_fix_general import correct_json
from autogpt.llm_utils import call_ai_function
from autogpt.llm import call_ai_function
from autogpt.logs import logger
from autogpt.speech import say_text

Expand Down
22 changes: 22 additions & 0 deletions autogpt/llm/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
]
2 changes: 1 addition & 1 deletion autogpt/api_manager.py → autogpt/llm/api_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import openai

from autogpt.config import Config
from autogpt.llm.modelsinfo import COSTS
from autogpt.logs import logger
from autogpt.modelsinfo import COSTS
from autogpt.singleton import Singleton


Expand Down
16 changes: 7 additions & 9 deletions autogpt/chat.py → autogpt/llm/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from openai.error import RateLimitError

from autogpt import token_counter
from autogpt.api_manager import ApiManager
from 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_tokens
from autogpt.logs import logger
from autogpt.types.openai import Message

Expand Down Expand Up @@ -43,7 +43,7 @@ def generate_context(prompt, relevant_memory, full_message_history, model):
next_message_to_add_index = len(full_message_history) - 1
insertion_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,
Expand Down Expand Up @@ -114,17 +114,15 @@ 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)

while next_message_to_add_index >= 0:
# 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

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion autogpt/llm_utils.py → autogpt/llm/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from colorama import Fore, Style
from openai.error import APIError, RateLimitError, Timeout

from autogpt.api_manager import ApiManager
from autogpt.config import Config
from autogpt.llm.api_manager import ApiManager
from autogpt.logs import logger
from autogpt.types.openai import Message

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions autogpt/logs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Logging module for Auto-GPT."""
import json
import logging
import os
import random
import re
import time
import traceback
from logging import LogRecord

from colorama import Fore, Style
Expand Down
2 changes: 1 addition & 1 deletion autogpt/memory/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
import orjson

from autogpt.llm_utils import get_ada_embedding
from autogpt.llm import get_ada_embedding
from autogpt.memory.base import MemoryProviderSingleton

EMBED_DIM = 1536
Expand Down
2 changes: 1 addition & 1 deletion autogpt/memory/milvus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pymilvus import Collection, CollectionSchema, DataType, FieldSchema, connections

from autogpt.config import Config
from autogpt.llm_utils import get_ada_embedding
from autogpt.llm import get_ada_embedding
from autogpt.memory.base import MemoryProviderSingleton


Expand Down
2 changes: 1 addition & 1 deletion autogpt/memory/pinecone.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pinecone
from colorama import Fore, Style

from autogpt.llm_utils import get_ada_embedding
from autogpt.llm import get_ada_embedding
from autogpt.logs import logger
from autogpt.memory.base import MemoryProviderSingleton

Expand Down
2 changes: 1 addition & 1 deletion autogpt/memory/redismem.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
from redis.commands.search.query import Query

from autogpt.llm_utils import get_ada_embedding
from autogpt.llm import get_ada_embedding
from autogpt.logs import logger
from autogpt.memory.base import MemoryProviderSingleton

Expand Down
2 changes: 1 addition & 1 deletion autogpt/memory/weaviate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from weaviate.embedded import EmbeddedOptions
from weaviate.util import generate_uuid5

from autogpt.llm_utils import get_ada_embedding
from autogpt.llm import get_ada_embedding
from autogpt.memory.base import MemoryProviderSingleton


Expand Down
13 changes: 4 additions & 9 deletions autogpt/processing/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import spacy
from selenium.webdriver.remote.webdriver import WebDriver

from autogpt import token_counter
from autogpt.config import Config
from autogpt.llm_utils import create_chat_completion
from autogpt.llm import count_message_tokens, create_chat_completion
from autogpt.memory import get_memory

CFG = Config()
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion autogpt/prompts/prompt.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from colorama import Fore

from autogpt.api_manager import ApiManager
from autogpt.config.ai_config import AIConfig
from autogpt.config.config import Config
from autogpt.llm import ApiManager
from autogpt.logs import logger
from autogpt.prompts.generator import PromptGenerator
from autogpt.setup import prompt_user
Expand Down
2 changes: 1 addition & 1 deletion autogpt/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from autogpt import utils
from autogpt.config import Config
from autogpt.config.ai_config import AIConfig
from autogpt.llm_utils import create_chat_completion
from autogpt.llm import create_chat_completion
from autogpt.logs import logger

CFG = Config()
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pytest
from pytest_mock import MockerFixture

from autogpt.api_manager import ApiManager
from autogpt.config import Config
from autogpt.llm import ApiManager
from autogpt.workspace import Workspace

pytest_plugins = ["tests.integration.agent_factory"]
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from numpy.random import RandomState

from autogpt.llm_utils import get_ada_embedding
from autogpt.llm.llm_utils import get_ada_embedding
from tests.utils import requires_api_key


Expand Down
2 changes: 1 addition & 1 deletion tests/integration/weaviate_memory_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from weaviate.util import get_valid_uuid

from autogpt.config import Config
from autogpt.llm_utils import get_ada_embedding
from autogpt.llm import get_ada_embedding
from autogpt.memory.weaviate import WeaviateMemory


Expand Down
3 changes: 0 additions & 3 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import pytest

from autogpt.agent import Agent
from autogpt.chat import chat_with_ai
from autogpt.config import Config
from autogpt.speech import say_text
from autogpt.utils import clean_input


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_agent_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from autogpt.agent.agent_manager import AgentManager
from autogpt.llm_utils import create_chat_completion
from autogpt.llm import create_chat_completion


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from autogpt.api_manager import COSTS, ApiManager
from autogpt.llm import COSTS, ApiManager

api_manager = ApiManager()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_llm_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from 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])
Expand All @@ -19,7 +19,7 @@ def mock_create_embedding(mocker):
mock_response.usage.prompt_tokens = 5
mock_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
)


Expand Down
3 changes: 1 addition & 2 deletions tests/test_token_counter.py
Original file line number Diff line number Diff line change
@@ -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_tokens


def test_count_message_tokens():
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time
from unittest.mock import patch

from autogpt.chat import create_chat_message, generate_context
from autogpt.llm import create_chat_message, generate_context


def test_happy_path_role_content():
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_get_self_feedback.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from autogpt.agent.agent import Agent
from autogpt.config import AIConfig
from autogpt.llm_utils import create_chat_completion
from autogpt.llm import create_chat_completion


def test_get_self_feedback(mocker):
Expand Down

0 comments on commit 3b74d21

Please sign in to comment.