Skip to main content

A Python library for interfacing with various LLM inference endpoints

Project description

SimpleModelRouter

A Python library for interfacing with various Large Language Model (LLM) inference endpoints, including OpenAI, Anthropic, and Ollama. The library provides a unified, async-first interface for interacting with different LLM providers.

Features

  • Support for multiple LLM providers:
    • OpenAI (GPT-3.5, GPT-4)
    • Anthropic (Claude)
    • Ollama (Local models)
  • Async HTTP support using httpx
  • Streaming responses for real-time text generation
  • Unified interface across providers
  • Type hints and comprehensive documentation
  • Configurable API endpoints and models
  • Error handling and retries
  • Resource cleanup and connection management

Installation

Using pip

pip install simplemodelrouter

Using Poetry (recommended)

poetry add simplemodelrouter

For development:

# Clone the repository
git clone https://github.com/yourusername/simplemodelrouter.git
cd simplemodelrouter

# Install Poetry if you haven't already
curl -sSL https://install.python-poetry.org | python3 -

# Install dependencies including development tools
poetry install

Quick Start

import asyncio
from simplemodelrouter import OpenAIProvider, Message

async def main():
    provider = OpenAIProvider(api_key="your-api-key")
    messages = [Message(role="user", content="Hello!")]
    
    response = await provider.chat(messages)
    print(response.message.content)
    
    await provider.close()

asyncio.run(main())

Detailed Usage

Provider Configuration

Each provider can be configured with:

  • API key (required for OpenAI and Anthropic)
  • Base URL (optional, for custom deployments)
  • Default model (optional)
# OpenAI with custom configuration
openai = OpenAIProvider(
    api_key="your-api-key",
    base_url="https://api.custom-deployment.com/v1",
    default_model="gpt-4"
)

# Anthropic with default configuration
anthropic = AnthropicProvider(
    api_key="your-api-key"
)

# Ollama for local deployment
ollama = OllamaProvider(
    base_url="http://localhost:11434",
    default_model="llama2"
)

Chat Interface

The chat interface supports conversations with multiple messages:

messages = [
    Message(role="system", content="You are a helpful assistant."),
    Message(role="user", content="What's the weather like?"),
    Message(role="assistant", content="I don't have access to current weather data."),
    Message(role="user", content="What can you help me with?")
]

response = await provider.chat(
    messages=messages,
    temperature=0.7,
    stream=False
)

Streaming Responses

All providers support streaming for both chat and completion endpoints:

async for chunk in await provider.chat(messages, stream=True):
    print(chunk.message.content, end="", flush=True)

async for chunk in await provider.complete(prompt, stream=True):
    print(chunk.text, end="", flush=True)

Error Handling

The library provides consistent error handling across providers:

try:
    response = await provider.chat(messages)
except httpx.HTTPStatusError as e:
    print(f"API error: {e.response.status_code}")
except httpx.RequestError as e:
    print(f"Network error: {str(e)}")
finally:
    await provider.close()

Resource Management

Always close providers when done to clean up resources:

try:
    provider = OpenAIProvider(api_key="your-api-key")
    # ... use provider ...
finally:
    await provider.close()

Or use async context managers (coming soon):

async with OpenAIProvider(api_key="your-api-key") as provider:
    response = await provider.chat(messages)

Examples

Check out the examples/ directory for more detailed examples:

  • chat_comparison.py: Compare responses from different providers
  • streaming_example.py: Demonstrate streaming capabilities
  • error_handling.py: Show error handling scenarios

Development

  1. Clone the repository:

    git clone https://github.com/yourusername/simplemodelrouter.git
    cd simplemodelrouter
    
  2. Install Poetry:

    curl -sSL https://install.python-poetry.org | python3 -
    
  3. Install dependencies:

    poetry install
    
  4. Activate the virtual environment:

    poetry shell
    
  5. Run tests:

    poetry run pytest
    
  6. Format code:

    poetry run black simplemodelrouter
    poetry run isort simplemodelrouter
    
  7. Type check:

    poetry run mypy simplemodelrouter
    

Contributing

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

License

MIT License

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

simplemodelrouter-0.1.1.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

simplemodelrouter-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file simplemodelrouter-0.1.1.tar.gz.

File metadata

  • Download URL: simplemodelrouter-0.1.1.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.3 Darwin/23.5.0

File hashes

Hashes for simplemodelrouter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 36d47f2ca07e91cf6bbae21925938aadc6f2b20bc99bf7a0554c6b4489192ba7
MD5 a09eb6b13f36950abd9c2d192e2a7e5a
BLAKE2b-256 b83a5944baabe39674cb2f534479e17f8a2d1293ca3064595c04b65c1cdf3cbf

See more details on using hashes here.

File details

Details for the file simplemodelrouter-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: simplemodelrouter-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.3 Darwin/23.5.0

File hashes

Hashes for simplemodelrouter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a6e5ca5ef058c59eb1c99e7c19e0a2829e8eff991782a9d482d6c0e32785f1e2
MD5 af38dbd895e08b1ecec9aee00275b4d2
BLAKE2b-256 01f1d7518d54e2e8b17b7f17cc296e5657a54df8aa68493f7fb9939afbe67f5b

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