Skip to main content

a uniform python SDK for message generation with LLMs

Project description

ExamplePlugins

Exchange - a uniform python SDK for message generation with LLMs

  • Provides a flexible layer for message handling and generation
  • Directly integrates python functions into tool calling
  • Persistently surfaces errors to the underlying models to support reflection

Example

[!NOTE] Before you can run this example, you need to setup an API key with export OPENAI_API_KEY=your-key-here

from exchange import Exchange, Message, Tool
from exchange.providers import OpenAiProvider

def word_count(text: str):
    """Get the count of words in text

    Args:
        text (str): The text with words to count
    """
    return len(text.split(" "))

ex = Exchange(
    provider=OpenAiProvider.from_env(),
    model="gpt-4o",
    system="You are a helpful assistant.",
    tools=[Tool.from_function(word_count)],
)
ex.add(Message.user("Count the number of words in this current message"))

# The model sees it has a word count tool, and should use it along the way to answer
# This will call all the tools as needed until the model replies with the final result
reply = ex.reply()
print(reply.text)

# you can see all the tool calls in the message history
print(ex.messages)

Plugins

exchange has a plugin mechanism to add support for additional providers and moderators. If you need a provider not supported here, we'd be happy to review contributions. But you can also consider building and using your own plugin.

To create a Provider plugin, subclass exchange.provider.Provider. You will need to implement the complete method. For example this is what we use as a mock in our tests. You can see a full implementation example of the OpenAiProvider. We also generally recommend implementing a from_env classmethod to instantiate the provider.

class MockProvider(Provider):
    def __init__(self, sequence: List[Message]):
        # We'll use init to provide a preplanned reply sequence
        self.sequence = sequence
        self.call_count = 0

    def complete(
        self, model: str, system: str, messages: List[Message], tools: List[Tool]
    ) -> Message:
        output = self.sequence[self.call_count]
        self.call_count += 1
        return output

Then use python packaging's entrypoints to register your plugin.

[project.entry-points.'exchange.provider']
example = 'path.to.plugin:ExampleProvider'

Your plugin will then be available in your application or other applications built on exchange through:

from exchange.providers import get_provider

provider = get_provider('example').from_env()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_exchange-0.8.0.tar.gz (94.2 kB view details)

Uploaded Source

Built Distribution

ai_exchange-0.8.0-py3-none-any.whl (30.2 kB view details)

Uploaded Python 3

File details

Details for the file ai_exchange-0.8.0.tar.gz.

File metadata

  • Download URL: ai_exchange-0.8.0.tar.gz
  • Upload date:
  • Size: 94.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for ai_exchange-0.8.0.tar.gz
Algorithm Hash digest
SHA256 512777301daa7bc701832d0ffc00ac12756b5b67f1dca0ccd8d7fe73cec11eba
MD5 9e1b5d4b70cdb226d4a1f50960b8ac95
BLAKE2b-256 ba468ca77fc284847c4b7bf9d7db6c0282a5057d3eec0a120c23222b909e278e

See more details on using hashes here.

File details

Details for the file ai_exchange-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: ai_exchange-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 30.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for ai_exchange-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5cc2f901b00db4943b50cf404f5ed7df0235b3afd2f1cb1c2f168363b34ec74
MD5 1fb7925bb68f2152f98b50e116c28901
BLAKE2b-256 4bf34ac5b9aab8e37787a36daa303d94ed4df58f4488647055c6566cbdf2bfaa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page