diff --git a/README.md b/README.md index 2070308..e38b266 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ > `export OPENAI_API_KEY=your-key-here` ``` python -from exchange import Exchange, Message, Tool -from exchange.providers import OpenAiProvider +from ai_exchange import Exchange, Message, Tool +from ai_exchange.providers import OpenAiProvider def word_count(text: str): """Get the count of words in text @@ -85,11 +85,11 @@ Your plugin will then be available in your application or other applications bui through: ``` python -from exchange.providers import get_provider +from ai_exchange.providers import get_provider provider = get_provider('example').from_env() ``` [CONTRIBUTING]: CONTRIBUTING.md -[openaiprovider]: src/exchange/providers/openai.py +[openaiprovider]: src/ai_exchange/providers/openai.py [plugins]: https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/ diff --git a/src/ai_exchange/__init__.py b/src/ai_exchange/__init__.py new file mode 100644 index 0000000..b95b1ef --- /dev/null +++ b/src/ai_exchange/__init__.py @@ -0,0 +1,6 @@ +"""Classes for interacting with the exchange API.""" + +from ai_exchange.tool import Tool # noqa +from ai_exchange.content import Text, ToolResult, ToolUse # noqa +from ai_exchange.message import Message # noqa +from ai_exchange.exchange import Exchange # noqa diff --git a/src/exchange/checkpoint.py b/src/ai_exchange/checkpoint.py similarity index 100% rename from src/exchange/checkpoint.py rename to src/ai_exchange/checkpoint.py diff --git a/src/exchange/content.py b/src/ai_exchange/content.py similarity index 100% rename from src/exchange/content.py rename to src/ai_exchange/content.py diff --git a/src/exchange/exchange.py b/src/ai_exchange/exchange.py similarity index 95% rename from src/exchange/exchange.py rename to src/ai_exchange/exchange.py index c76159d..a4ec8aa 100644 --- a/src/exchange/exchange.py +++ b/src/ai_exchange/exchange.py @@ -6,12 +6,12 @@ from attrs import define, evolve, field from tiktoken import get_encoding -from exchange.checkpoint import Checkpoint -from exchange.content import ToolResult, ToolUse -from exchange.message import Message -from exchange.moderators import ContextTruncate, Moderator -from exchange.providers import Provider, Usage -from exchange.tool import Tool +from ai_exchange.checkpoint import Checkpoint +from ai_exchange.content import ToolResult, ToolUse +from ai_exchange.message import Message +from ai_exchange.moderators import ContextTruncate, Moderator +from ai_exchange.providers import Provider, Usage +from ai_exchange.tool import Tool def validate_tool_output(output: str) -> None: diff --git a/src/exchange/message.py b/src/ai_exchange/message.py similarity index 96% rename from src/exchange/message.py rename to src/ai_exchange/message.py index 035c603..4c0d415 100644 --- a/src/exchange/message.py +++ b/src/ai_exchange/message.py @@ -6,8 +6,8 @@ from attrs import define, field from jinja2 import Environment, FileSystemLoader -from exchange.content import CONTENT_TYPES, Content, Text, ToolResult, ToolUse -from exchange.utils import create_object_id +from ai_exchange.content import CONTENT_TYPES, Content, Text, ToolResult, ToolUse +from ai_exchange.utils import create_object_id Role = Literal["user", "assistant"] diff --git a/src/ai_exchange/moderators/__init__.py b/src/ai_exchange/moderators/__init__.py new file mode 100644 index 0000000..23e20f7 --- /dev/null +++ b/src/ai_exchange/moderators/__init__.py @@ -0,0 +1,13 @@ +from functools import cache +from typing import Type + +from ai_exchange.moderators.base import Moderator +from ai_exchange.utils import load_plugins +from ai_exchange.moderators.passive import PassiveModerator # noqa +from ai_exchange.moderators.truncate import ContextTruncate # noqa +from ai_exchange.moderators.summarizer import ContextSummarizer # noqa + + +@cache +def get_moderator(name: str) -> Type[Moderator]: + return load_plugins(group="exchange.moderator")[name] diff --git a/src/exchange/moderators/base.py b/src/ai_exchange/moderators/base.py similarity index 100% rename from src/exchange/moderators/base.py rename to src/ai_exchange/moderators/base.py diff --git a/src/exchange/moderators/passive.py b/src/ai_exchange/moderators/passive.py similarity index 75% rename from src/exchange/moderators/passive.py rename to src/ai_exchange/moderators/passive.py index e3a24ef..c1c5a33 100644 --- a/src/exchange/moderators/passive.py +++ b/src/ai_exchange/moderators/passive.py @@ -1,5 +1,5 @@ from typing import Type -from exchange.moderators.base import Moderator +from ai_exchange.moderators.base import Moderator class PassiveModerator(Moderator): diff --git a/src/exchange/moderators/summarizer.jinja b/src/ai_exchange/moderators/summarizer.jinja similarity index 100% rename from src/exchange/moderators/summarizer.jinja rename to src/ai_exchange/moderators/summarizer.jinja diff --git a/src/exchange/moderators/summarizer.py b/src/ai_exchange/moderators/summarizer.py similarity index 97% rename from src/exchange/moderators/summarizer.py rename to src/ai_exchange/moderators/summarizer.py index 504a436..b1d8fc7 100644 --- a/src/exchange/moderators/summarizer.py +++ b/src/ai_exchange/moderators/summarizer.py @@ -1,8 +1,8 @@ from typing import List, Literal, Optional, Tuple, Type, Union -from exchange import Message -from exchange.checkpoint import Checkpoint -from exchange.moderators import ContextTruncate, Moderator, PassiveModerator +from ai_exchange import Message +from ai_exchange.checkpoint import Checkpoint +from ai_exchange.moderators import ContextTruncate, Moderator, PassiveModerator MAX_TOKENS = 112000 SUMMARIZATION_OFFSET = 40000 # Keep a max of this many tokens diff --git a/src/exchange/moderators/truncate.py b/src/ai_exchange/moderators/truncate.py similarity index 95% rename from src/exchange/moderators/truncate.py rename to src/ai_exchange/moderators/truncate.py index 6a962c1..b2dd469 100644 --- a/src/exchange/moderators/truncate.py +++ b/src/ai_exchange/moderators/truncate.py @@ -1,7 +1,7 @@ from typing import Literal, Optional, Type -from exchange.moderators import PassiveModerator -from exchange.moderators.base import Moderator +from ai_exchange.moderators import PassiveModerator +from ai_exchange.moderators.base import Moderator MAX_TOKENS = 112000 diff --git a/src/ai_exchange/providers/__init__.py b/src/ai_exchange/providers/__init__.py new file mode 100644 index 0000000..4a1b56d --- /dev/null +++ b/src/ai_exchange/providers/__init__.py @@ -0,0 +1,14 @@ +from functools import cache +from typing import Type + +from ai_exchange.providers.anthropic import AnthropicProvider # noqa +from ai_exchange.providers.base import Provider, Usage # noqa +from ai_exchange.providers.databricks import DatabricksProvider # noqa +from ai_exchange.providers.openai import OpenAiProvider # noqa +from ai_exchange.providers.azure import AzureProvider # noqa +from ai_exchange.utils import load_plugins + + +@cache +def get_provider(name: str) -> Type[Provider]: + return load_plugins(group="exchange.provider")[name] diff --git a/src/exchange/providers/anthropic.py b/src/ai_exchange/providers/anthropic.py similarity index 96% rename from src/exchange/providers/anthropic.py rename to src/ai_exchange/providers/anthropic.py index 02e6361..8a35e65 100644 --- a/src/exchange/providers/anthropic.py +++ b/src/ai_exchange/providers/anthropic.py @@ -4,10 +4,10 @@ import httpx -from exchange import Message, Tool -from exchange.content import Text, ToolResult, ToolUse -from exchange.providers.base import Provider, Usage -from exchange.providers.utils import raise_for_status +from ai_exchange import Message, Tool +from ai_exchange.content import Text, ToolResult, ToolUse +from ai_exchange.providers.base import Provider, Usage +from ai_exchange.providers.utils import raise_for_status ANTHROPIC_HOST = "https://api.anthropic.com/v1/messages" diff --git a/src/exchange/providers/azure.py b/src/ai_exchange/providers/azure.py similarity index 95% rename from src/exchange/providers/azure.py rename to src/ai_exchange/providers/azure.py index aadec03..79f67c3 100644 --- a/src/exchange/providers/azure.py +++ b/src/ai_exchange/providers/azure.py @@ -3,16 +3,16 @@ import httpx -from exchange.message import Message -from exchange.providers.base import Provider, Usage -from exchange.providers.utils import ( +from ai_exchange.message import Message +from ai_exchange.providers.base import Provider, Usage +from ai_exchange.providers.utils import ( messages_to_openai_spec, openai_response_to_message, openai_single_message_context_length_exceeded, raise_for_status, tools_to_openai_spec, ) -from exchange.tool import Tool +from ai_exchange.tool import Tool class AzureProvider(Provider): diff --git a/src/exchange/providers/base.py b/src/ai_exchange/providers/base.py similarity index 89% rename from src/exchange/providers/base.py rename to src/ai_exchange/providers/base.py index e8e1d70..29af9a4 100644 --- a/src/exchange/providers/base.py +++ b/src/ai_exchange/providers/base.py @@ -2,8 +2,8 @@ from attrs import define, field from typing import List, Tuple, Type -from exchange.message import Message -from exchange.tool import Tool +from ai_exchange.message import Message +from ai_exchange.tool import Tool @define diff --git a/src/exchange/providers/bedrock.py b/src/ai_exchange/providers/bedrock.py similarity index 98% rename from src/exchange/providers/bedrock.py rename to src/ai_exchange/providers/bedrock.py index 0dd59c0..6bde98a 100644 --- a/src/exchange/providers/bedrock.py +++ b/src/ai_exchange/providers/bedrock.py @@ -9,11 +9,11 @@ import httpx -from exchange.content import Text, ToolResult, ToolUse -from exchange.message import Message -from exchange.providers import Provider, Usage -from exchange.providers.utils import raise_for_status -from exchange.tool import Tool +from ai_exchange.content import Text, ToolResult, ToolUse +from ai_exchange.message import Message +from ai_exchange.providers import Provider, Usage +from ai_exchange.providers.utils import raise_for_status +from ai_exchange.tool import Tool SERVICE = "bedrock-runtime" UTC = timezone.utc diff --git a/src/exchange/providers/databricks.py b/src/ai_exchange/providers/databricks.py similarity index 94% rename from src/exchange/providers/databricks.py rename to src/ai_exchange/providers/databricks.py index 09453f0..1dfb67c 100644 --- a/src/exchange/providers/databricks.py +++ b/src/ai_exchange/providers/databricks.py @@ -3,15 +3,15 @@ import httpx -from exchange.message import Message -from exchange.providers.base import Provider, Usage -from exchange.providers.utils import ( +from ai_exchange.message import Message +from ai_exchange.providers.base import Provider, Usage +from ai_exchange.providers.utils import ( messages_to_openai_spec, openai_response_to_message, raise_for_status, tools_to_openai_spec, ) -from exchange.tool import Tool +from ai_exchange.tool import Tool class DatabricksProvider(Provider): diff --git a/src/exchange/providers/openai.py b/src/ai_exchange/providers/openai.py similarity index 93% rename from src/exchange/providers/openai.py rename to src/ai_exchange/providers/openai.py index 1f1ac23..e357b54 100644 --- a/src/exchange/providers/openai.py +++ b/src/ai_exchange/providers/openai.py @@ -3,16 +3,16 @@ import httpx -from exchange.message import Message -from exchange.providers.base import Provider, Usage -from exchange.providers.utils import ( +from ai_exchange.message import Message +from ai_exchange.providers.base import Provider, Usage +from ai_exchange.providers.utils import ( messages_to_openai_spec, openai_response_to_message, openai_single_message_context_length_exceeded, raise_for_status, tools_to_openai_spec, ) -from exchange.tool import Tool +from ai_exchange.tool import Tool OPENAI_HOST = "https://api.openai.com/" diff --git a/src/exchange/providers/utils.py b/src/ai_exchange/providers/utils.py similarity index 97% rename from src/exchange/providers/utils.py rename to src/ai_exchange/providers/utils.py index f53cbfc..4119bda 100644 --- a/src/exchange/providers/utils.py +++ b/src/ai_exchange/providers/utils.py @@ -4,9 +4,9 @@ from typing import Any, Dict, List, Tuple import httpx -from exchange.content import Text, ToolResult, ToolUse -from exchange.message import Message -from exchange.tool import Tool +from ai_exchange.content import Text, ToolResult, ToolUse +from ai_exchange.message import Message +from ai_exchange.tool import Tool def raise_for_status(response: httpx.Response) -> httpx.Response: diff --git a/src/exchange/tool.py b/src/ai_exchange/tool.py similarity index 96% rename from src/exchange/tool.py rename to src/ai_exchange/tool.py index 4ce9e7c..ef139c2 100644 --- a/src/exchange/tool.py +++ b/src/ai_exchange/tool.py @@ -3,7 +3,7 @@ from attrs import define -from exchange.utils import json_schema, parse_docstring +from ai_exchange.utils import json_schema, parse_docstring @define diff --git a/src/exchange/utils.py b/src/ai_exchange/utils.py similarity index 100% rename from src/exchange/utils.py rename to src/ai_exchange/utils.py diff --git a/src/exchange/__init__.py b/src/exchange/__init__.py deleted file mode 100644 index 3edc6d1..0000000 --- a/src/exchange/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Classes for interacting with the exchange API.""" - -from exchange.tool import Tool # noqa -from exchange.content import Text, ToolResult, ToolUse # noqa -from exchange.message import Message # noqa -from exchange.exchange import Exchange # noqa diff --git a/src/exchange/moderators/__init__.py b/src/exchange/moderators/__init__.py deleted file mode 100644 index 56b198a..0000000 --- a/src/exchange/moderators/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -from functools import cache -from typing import Type - -from exchange.moderators.base import Moderator -from exchange.utils import load_plugins -from exchange.moderators.passive import PassiveModerator # noqa -from exchange.moderators.truncate import ContextTruncate # noqa -from exchange.moderators.summarizer import ContextSummarizer # noqa - - -@cache -def get_moderator(name: str) -> Type[Moderator]: - return load_plugins(group="exchange.moderator")[name] diff --git a/src/exchange/providers/__init__.py b/src/exchange/providers/__init__.py deleted file mode 100644 index 8b96b98..0000000 --- a/src/exchange/providers/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -from functools import cache -from typing import Type - -from exchange.providers.anthropic import AnthropicProvider # noqa -from exchange.providers.base import Provider, Usage # noqa -from exchange.providers.databricks import DatabricksProvider # noqa -from exchange.providers.openai import OpenAiProvider # noqa -from exchange.providers.azure import AzureProvider # noqa -from exchange.utils import load_plugins - - -@cache -def get_provider(name: str) -> Type[Provider]: - return load_plugins(group="exchange.provider")[name] diff --git a/tests/providers/test_anthropic.py b/tests/providers/test_anthropic.py index d72a9fb..15020de 100644 --- a/tests/providers/test_anthropic.py +++ b/tests/providers/test_anthropic.py @@ -3,10 +3,10 @@ import httpx import pytest -from exchange import Message, Text -from exchange.content import ToolResult, ToolUse -from exchange.providers.anthropic import AnthropicProvider -from exchange.tool import Tool +from ai_exchange import Message, Text +from ai_exchange.content import ToolResult, ToolUse +from ai_exchange.providers.anthropic import AnthropicProvider +from ai_exchange.tool import Tool def example_fn(param: str) -> None: diff --git a/tests/providers/test_azure.py b/tests/providers/test_azure.py index 857c00a..1d4abfa 100644 --- a/tests/providers/test_azure.py +++ b/tests/providers/test_azure.py @@ -2,8 +2,8 @@ from unittest.mock import patch import pytest -from exchange import Message, Text -from exchange.providers.azure import AzureProvider +from ai_exchange import Message, Text +from ai_exchange.providers.azure import AzureProvider @pytest.fixture diff --git a/tests/providers/test_bedrock.py b/tests/providers/test_bedrock.py index bdc8cdf..96d2779 100644 --- a/tests/providers/test_bedrock.py +++ b/tests/providers/test_bedrock.py @@ -3,10 +3,10 @@ from unittest.mock import patch import pytest -from exchange.content import Text, ToolResult, ToolUse -from exchange.message import Message -from exchange.providers.bedrock import BedrockProvider -from exchange.tool import Tool +from ai_exchange.content import Text, ToolResult, ToolUse +from ai_exchange.message import Message +from ai_exchange.providers.bedrock import BedrockProvider +from ai_exchange.tool import Tool logger = logging.getLogger(__name__) diff --git a/tests/providers/test_databricks.py b/tests/providers/test_databricks.py index 3c14211..8672901 100644 --- a/tests/providers/test_databricks.py +++ b/tests/providers/test_databricks.py @@ -2,8 +2,8 @@ from unittest.mock import patch import pytest -from exchange import Message, Text -from exchange.providers.databricks import DatabricksProvider +from ai_exchange import Message, Text +from ai_exchange.providers.databricks import DatabricksProvider @pytest.fixture diff --git a/tests/providers/test_openai.py b/tests/providers/test_openai.py index 0e0e000..bc17c86 100644 --- a/tests/providers/test_openai.py +++ b/tests/providers/test_openai.py @@ -2,8 +2,8 @@ from unittest.mock import patch import pytest -from exchange import Message, Text -from exchange.providers.openai import OpenAiProvider +from ai_exchange import Message, Text +from ai_exchange.providers.openai import OpenAiProvider @pytest.fixture diff --git a/tests/providers/test_provider_utils.py b/tests/providers/test_provider_utils.py index 5ad0135..25023db 100644 --- a/tests/providers/test_provider_utils.py +++ b/tests/providers/test_provider_utils.py @@ -6,15 +6,15 @@ import pytest from unittest.mock import patch -from exchange.content import Text, ToolResult, ToolUse -from exchange.message import Message -from exchange.providers.utils import ( +from ai_exchange.content import Text, ToolResult, ToolUse +from ai_exchange.message import Message +from ai_exchange.providers.utils import ( messages_to_openai_spec, openai_response_to_message, raise_for_status, tools_to_openai_spec, ) -from exchange.tool import Tool +from ai_exchange.tool import Tool OPEN_AI_TOOL_USE_RESPONSE = response = { "choices": [ diff --git a/tests/test_exchange.py b/tests/test_exchange.py index 7cbac1a..51d6203 100644 --- a/tests/test_exchange.py +++ b/tests/test_exchange.py @@ -1,11 +1,11 @@ from typing import List -from exchange.content import Text, ToolResult, ToolUse -from exchange.exchange import Exchange -from exchange.message import Message -from exchange.moderators import PassiveModerator -from exchange.providers import Provider, Usage -from exchange.tool import Tool +from ai_exchange.content import Text, ToolResult, ToolUse +from ai_exchange.exchange import Exchange +from ai_exchange.message import Message +from ai_exchange.moderators import PassiveModerator +from ai_exchange.providers import Provider, Usage +from ai_exchange.tool import Tool def dummy_tool() -> str: diff --git a/tests/test_exchange_frozen.py b/tests/test_exchange_frozen.py index b6a5ae1..e982212 100644 --- a/tests/test_exchange_frozen.py +++ b/tests/test_exchange_frozen.py @@ -1,11 +1,11 @@ import pytest from attr.exceptions import FrozenInstanceError -from exchange.content import Text -from exchange.exchange import Exchange -from exchange.moderators import PassiveModerator -from exchange.message import Message -from exchange.providers import Provider, Usage -from exchange.tool import Tool +from ai_exchange.content import Text +from ai_exchange.exchange import Exchange +from ai_exchange.moderators import PassiveModerator +from ai_exchange.message import Message +from ai_exchange.providers import Provider, Usage +from ai_exchange.tool import Tool def dummy_tool() -> str: diff --git a/tests/test_integration.py b/tests/test_integration.py index 7bdb26c..d3e9c38 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1,8 +1,8 @@ import pytest -from exchange.exchange import Exchange -from exchange.message import Message -from exchange.providers import get_provider -from exchange.tool import Tool +from ai_exchange.exchange import Exchange +from ai_exchange.message import Message +from ai_exchange.providers import get_provider +from ai_exchange.tool import Tool too_long_chars = "x" * (2**20 + 1) diff --git a/tests/test_message.py b/tests/test_message.py index d5442eb..bc06943 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -2,8 +2,8 @@ from pathlib import Path import pytest -from exchange.message import Message -from exchange.content import Text, ToolUse, ToolResult +from ai_exchange.message import Message +from ai_exchange.content import Text, ToolUse, ToolResult def test_user_message(): @@ -19,7 +19,7 @@ def test_assistant_message(): def test_message_tool_use(): - from exchange.content import ToolUse + from ai_exchange.content import ToolUse tu1 = ToolUse(id="1", name="tool", parameters={}) tu2 = ToolUse(id="2", name="tool", parameters={}) @@ -29,7 +29,7 @@ def test_message_tool_use(): def test_message_tool_result(): - from exchange.content import ToolResult + from ai_exchange.content import ToolResult tr1 = ToolResult(tool_use_id="1", output="result") tr2 = ToolResult(tool_use_id="2", output="result") diff --git a/tests/test_summarizer.py b/tests/test_summarizer.py index 89f131a..e131d88 100644 --- a/tests/test_summarizer.py +++ b/tests/test_summarizer.py @@ -1,8 +1,8 @@ import pytest -from exchange import Exchange, Message -from exchange.checkpoint import Checkpoint -from exchange.moderators.summarizer import ContextSummarizer -from exchange.providers import Usage +from ai_exchange import Exchange, Message +from ai_exchange.checkpoint import Checkpoint +from ai_exchange.moderators.summarizer import ContextSummarizer +from ai_exchange.providers import Usage class MockProvider: diff --git a/tests/test_tool.py b/tests/test_tool.py index 847e79f..bf1429c 100644 --- a/tests/test_tool.py +++ b/tests/test_tool.py @@ -1,5 +1,5 @@ import attrs -from exchange.tool import Tool +from ai_exchange.tool import Tool def get_current_weather(location: str) -> None: diff --git a/tests/test_truncate.py b/tests/test_truncate.py index 123dd0e..caa9918 100644 --- a/tests/test_truncate.py +++ b/tests/test_truncate.py @@ -1,12 +1,12 @@ import pytest -from exchange import Exchange -from exchange.checkpoint import Checkpoint -from exchange.message import Message -from exchange.providers import Provider, Usage -from exchange.moderators.truncate import ContextTruncate, pop_checkpoint +from ai_exchange import Exchange +from ai_exchange.checkpoint import Checkpoint +from ai_exchange.message import Message +from ai_exchange.providers import Provider, Usage +from ai_exchange.moderators.truncate import ContextTruncate, pop_checkpoint from typing import List -from exchange.content import Text -from exchange.tool import Tool +from ai_exchange.content import Text +from ai_exchange.tool import Tool class MockProvider(Provider): diff --git a/tests/test_utils.py b/tests/test_utils.py index 6bc00f9..6f28d38 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,9 +1,9 @@ import pytest -from exchange import utils +from ai_exchange import utils from unittest.mock import patch -from exchange.message import Message -from exchange.content import Text, ToolResult -from exchange.providers.utils import messages_to_openai_spec, encode_image +from ai_exchange.message import Message +from ai_exchange.content import Text, ToolResult +from ai_exchange.providers.utils import messages_to_openai_spec, encode_image def test_encode_image(): diff --git a/tests/test_vision.py b/tests/test_vision.py index 95be50f..0e6db6d 100644 --- a/tests/test_vision.py +++ b/tests/test_vision.py @@ -1,8 +1,8 @@ import pytest -from exchange.content import ToolResult, ToolUse -from exchange.exchange import Exchange -from exchange.message import Message -from exchange.providers import get_provider +from ai_exchange.content import ToolResult, ToolUse +from ai_exchange.exchange import Exchange +from ai_exchange.message import Message +from ai_exchange.providers import get_provider cases = [