Skip to main content

A unified async client for interacting with multiple LLM providers

Project description

Unified LLM Client

A unified async client library for interacting with multiple LLM providers (OpenAI, Anthropic, and more) using a consistent API.

Features

  • Unified interface for multiple LLM providers (OpenAI, Anthropic, Ollama)
  • Async-first design for high-performance applications
  • Tool/function calling support with a consistent interface
  • Rich error handling and logging
  • Support for both OpenAI Completions and Responses APIs
  • Support for local models via Ollama integration
  • Type hints throughout the codebase

Installation

pip install unified-llm-client

Quick Start

import asyncio
from llm import AsyncLLMClient, ToolRegistry, llm_tool

# Define a tool function using the decorator
@llm_tool
async def get_weather(location: str, unit: str = "celsius"):
    """Get the current weather for a location."""
    # Mock implementation
    return f"The weather in {location} is sunny and 22 degrees {unit}"

async def main():
    # Create a tool registry and register your tools
    tools = ToolRegistry()
    tools.register("get_weather", get_weather)
    
    # Initialize the client with the tool registry
    client = AsyncLLMClient(tool_registry=tools)
    
    # Use the client with different models
    response = await client.response(
        "What's the weather like in Paris?",
        model="gpt-4o-mini",  # Default model is gpt-4o-mini
        instructions="You are a helpful assistant that provides weather information."
    )
    
    print(response["text"])
    
    # Use with Anthropic's Claude
    claude_response = await client.response(
        "What's the weather like in Tokyo?",
        model="claude-3-opus-20240229",
        instructions="You are a helpful assistant that provides weather information."
    )
    
    print(claude_response["text"])
    
    # Use with Ollama (local models)
    # Requires Ollama to be installed: https://ollama.com
    ollama_response = await client.response(
        "What's the weather like in London?",
        model="llama3",  # Or any model you've pulled with Ollama
        instructions="You are a helpful assistant that provides weather information.",
        base_url="http://localhost:11434/v1",
        api_key="ollama",
        use_responses_api=False
    )
    
    print(ollama_response["text"])

if __name__ == "__main__":
    asyncio.run(main())

Advanced Features

Using Conversations

import asyncio
from llm import AsyncLLMClient, Message

async def main():
    client = AsyncLLMClient()
    
    # Create a conversation with multiple messages
    messages = [
        {"role": "system", "content": "You are a helpful coding assistant."},
        {"role": "user", "content": "How do I read a file in Python?"},
        {"role": "assistant", "content": "You can use the built-in `open()` function..."},
        {"role": "user", "content": "Can you show me how to read a JSON file specifically?"}
    ]
    
    response = await client.response(
        messages,
        model="gpt-4o"
    )
    
    print(response["text"])

if __name__ == "__main__":
    asyncio.run(main())

Tool/Function Calling

The library supports a consistent interface for tool calling across providers:

import asyncio
import json
from llm import AsyncLLMClient, ToolRegistry, llm_tool

@llm_tool
async def search_database(query: str, limit: int = 5):
    """Search a database for information."""
    # Mock implementation
    results = [{"id": i, "title": f"Result {i} for {query}"} for i in range(limit)]
    return results

async def main():
    tools = ToolRegistry()
    tools.register("search_database", search_database)
    
    client = AsyncLLMClient(tool_registry=tools)
    
    response = await client.response(
        "Find information about machine learning frameworks",
        model="gpt-4o",
        instructions="You are a helpful assistant with access to a database."
    )
    
    print(response["text"])

if __name__ == "__main__":
    asyncio.run(main())

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Installation

To set up the development environment:

git clone https://github.com/skitsanos/unified-llm-client.git
cd unified-llm-client
pip install -e .
pip install -r requirements-dev.txt

Testing

Run tests with pytest:

pytest

Publishing to PyPI

See Publishing Documentation for detailed instructions on publishing releases to PyPI.

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

unified_llm_client-0.1.0.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unified_llm_client-0.1.0-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file unified_llm_client-0.1.0.tar.gz.

File metadata

  • Download URL: unified_llm_client-0.1.0.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for unified_llm_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1accad8f147ffed8ca6f3b0751d4841ebc3a6605d62a06cde3704a317041b4d5
MD5 4e931fea626fa0072fe0ca5ec8278540
BLAKE2b-256 4d2e86fd752c00c32277ab1a1d15f6d0553e160c161a38f746d9a2f8c1905655

See more details on using hashes here.

File details

Details for the file unified_llm_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for unified_llm_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1009a7d90491dce4d60523d4a7fd3e98d2ca49b4b6990d7b9a9632cf2c03ffcb
MD5 67b0247971e7df5295e576e95c8c7f5d
BLAKE2b-256 17ebc9e525033eff6652e5ed4cc78c1f5e466d0bf9812b7c5d9a5671a50403d4

See more details on using hashes here.

Supported by

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