Skip to main content

A multi-modal AI provider router supporting LLM, image generation, and embeddings

Project description

Weav Provider Router

PyPI version Tests Python 3.9+ License: MIT

A unified multi-modal AI provider router supporting LLM chat, image generation, and embeddings. Seamlessly integrate with 16+ LLM providers including OpenAI, Anthropic, Google, and Chinese providers like Moonshot, Baidu, MiniMax, and ByteDance.

Features

  • ๐Ÿค– LLM Chat: 16 providers for text generation
  • ๐ŸŽจ Image Generation: DALL-E support with more coming
  • ๐Ÿ”ข Vector Embeddings: OpenAI embeddings
  • ๐Ÿš€ Unified API: Single interface across all providers
  • ๐Ÿ”„ Async Support: Full async/await for all operations
  • ๐ŸŒŠ Streaming: Real-time streaming responses
  • ๐Ÿ”Œ Extensible: Easy to add new providers
  • ๐ŸŽฏ Type-Safe: Complete type hints
  • ๐Ÿงช Well-Tested: Comprehensive test coverage
  • ๐ŸŒ Global Coverage: International and Chinese providers

Supported Providers

๐Ÿค– LLM Chat Providers (16)

Provider Chat Streaming List Models Notes
OpenAI โœ… โœ… โœ… GPT-4, GPT-3.5, O1
Anthropic โœ… โœ… โœ… Claude 3/4
Google โœ… โœ… โœ… Gemini
Moonshot โœ… โœ… โœ… Kimi 128K context
MiniMax ๐Ÿ†• โœ… โœ… โœ… ๆตท่žบAI abab models
ByteDance ๐Ÿ†• โœ… โœ… โœ… Doubao (่ฑ†ๅŒ…/ไบ‘้›€)
NVIDIA ๐Ÿ†• โœ… โœ… โœ… NIM enterprise API
Baidu โœ… โœ… โœ… ERNIE (ๆ–‡ๅฟƒไธ€่จ€)
Mistral โœ… โœ… โœ… Mistral AI
Groq โœ… โœ… โœ… Ultra-fast (750 tok/s)
Together AI โœ… โœ… โœ… Multi-model
Cohere โœ… โœ… โœ… Command R/R+
DeepSeek โœ… โœ… โœ… Code + chat
Qwen โœ… โœ… โœ… ้€šไน‰ๅƒ้—ฎ
Zhipu โœ… โœ… โœ… ๆ™บ่ฐฑ GLM
Ollama โœ… โœ… โœ… Local models

๐ŸŽจ Image Generation (1)

Provider Generate Edit Variations
OpenAI DALL-E ๐Ÿ†• โœ… โœ… โœ…

๐Ÿ”ข Vector Embeddings (1)

Provider Batch Embed Single Query
OpenAI ๐Ÿ†• โœ… โœ…

Installation

pip install weav-provider-router

Or install with extras for specific providers:

# For OpenAI/DeepSeek
pip install weav-provider-router[openai]

# For Anthropic
pip install weav-provider-router[anthropic]

# For Google
pip install weav-provider-router[google]

# For all providers
pip install weav-provider-router[all]

Quick Start

Synchronous Chat

from weav_provider_router import chat

# OpenAI
response = chat(
    provider="openai",
    api_key="your-api-key",
    question="What is the capital of France?",
    model="gpt-4",
    temperature=0.7
)
print(response)

# Anthropic
response = chat(
    provider="anthropic",
    api_key="your-api-key",
    question="Explain quantum computing in simple terms",
    model="claude-3-5-sonnet-20241022"
)
print(response)

# Ollama (local)
response = chat(
    provider="ollama",
    api_key=None,
    question="Tell me a joke",
    model="llama3.2",
    base_url="http://localhost:11434"
)
print(response)

Asynchronous Chat

import asyncio
from weav_provider_router import chat_async

async def main():
    response = await chat_async(
        provider="openai",
        api_key="your-api-key",
        question="What is machine learning?",
        model="gpt-4o",
        temperature=0.5,
        max_tokens=500
    )
    print(response)

asyncio.run(main())

Streaming Responses

import asyncio
from weav_provider_router.providers import build_provider
from weav_provider_router.base import CompletionConfig

async def stream_example():
    llm = build_provider("openai", api_key="your-api-key")
    config = CompletionConfig(
        model="gpt-4o",
        temperature=0.7
    )
    
    messages = [{"role": "user", "content": "Write a short story"}]
    
    async for chunk in llm.stream(messages, config):
        print(chunk, end="", flush=True)

asyncio.run(stream_example())

Text Completion

from weav_provider_router import complete

response = complete(
    provider="openai",
    api_key="your-api-key",
    prompt="Once upon a time",
    model="gpt-4",
    max_tokens=100
)
print(response)

List Available Models

from weav_provider_router import list_models

# List OpenAI models
models = list_models("openai", api_key="your-api-key")
print(models)

# List Ollama local models
models = list_models("ollama", base_url="http://localhost:11434")
print(models)

๐ŸŽจ Image Generation (NEW!)

from weav_provider_router import build_image_provider, ImageConfig

# Create image provider
image_provider = build_image_provider("openai", api_key="your-api-key")

# Generate image
config = ImageConfig(
    model="dall-e-3",
    size="1024x1024",
    quality="hd",
    style="vivid"
)

response = await image_provider.generate(
    "A serene landscape with mountains and a lake at sunset",
    config
)
print(f"Image URL: {response.url}")
print(f"Revised prompt: {response.revised_prompt}")

# Edit existing image
with open("original.png", "rb") as f:
    image_bytes = f.read()

edited = await image_provider.edit(
    image=image_bytes,
    prompt="Add a rainbow in the sky",
    config=config
)
print(f"Edited image: {edited.url}")

๐Ÿ”ข Vector Embeddings (NEW!)

from weav_provider_router import build_embedding_provider, EmbeddingConfig

# Create embedding provider
embedding_provider = build_embedding_provider("openai", api_key="your-api-key")

# Generate embeddings
config = EmbeddingConfig(
    model="text-embedding-3-large",
    dimensions=1536
)

texts = [
    "The quick brown fox jumps over the lazy dog",
    "Machine learning is a subset of artificial intelligence",
    "Python is a popular programming language"
]

embeddings = await embedding_provider.embed(texts, config)
print(f"Generated {len(embeddings)} embeddings")
print(f"Embedding dimension: {len(embeddings[0])}")

# Single query embedding
query_embedding = await embedding_provider.embed_query(
    "What is AI?",
    config
)
print(f"Query embedding: {len(query_embedding)} dimensions")

print(models)

List Ollama local models

models = list_models("ollama", base_url="http://localhost:11434") print(models)


## Advanced Usage

### Building Provider Directly

```python
from weav_provider_router.providers import build_provider
from weav_provider_router.base import CompletionConfig

# Create provider instance
llm = build_provider(
    provider="anthropic",
    api_key="your-api-key"
)

# Configure completion
config = CompletionConfig(
    model="claude-3-5-sonnet-20241022",
    temperature=0.7,
    max_tokens=1000,
    top_p=0.9,
    stop=["END"],
    extra={"thinking": {"enabled": True}}
)

# Use the provider
messages = [
    {"role": "user", "content": "Explain the theory of relativity"}
]
response = await llm.chat(messages, config)
print(response)

Custom Base URL

from weav_provider_router import chat

# Use custom OpenAI-compatible endpoint
response = chat(
    provider="openai",
    api_key="your-api-key",
    question="Hello, world!",
    model="custom-model",
    base_url="https://your-custom-endpoint.com/v1"
)

Error Handling

from weav_provider_router import chat

try:
    response = chat(
        provider="unsupported_provider",
        api_key="key",
        question="test"
    )
except ValueError as e:
    print(f"Provider error: {e}")

try:
    response = chat(
        provider="openai",
        api_key="invalid-key",
        question="test",
        model="gpt-4"
    )
except Exception as e:
    print(f"API error: {e}")

Configuration Options

CompletionConfig

All completion and chat operations accept these configuration parameters:

Parameter Type Default Description
model str Required Model identifier (e.g., "gpt-4", "claude-3-5-sonnet-20241022")
temperature float 0.7 Sampling temperature (0.0 to 2.0)
max_tokens int None Maximum tokens to generate
top_p float 1.0 Nucleus sampling parameter
stop list[str] None Stop sequences
extra dict {} Provider-specific extra parameters

Provider-Specific Notes

OpenAI

  • Supports all GPT models including GPT-4, GPT-4o, GPT-3.5-turbo
  • Special handling for O1/O2 models (different parameter requirements)
  • Custom base_url support for OpenAI-compatible endpoints

Anthropic

  • Supports Claude 3 family (Opus, Sonnet, Haiku)
  • Extended thinking mode via extra parameter
  • Streaming support with delta chunks

Google

  • Supports Gemini models (gemini-pro, gemini-1.5-pro, etc.)
  • Integrates with Google AI Studio

Ollama

  • Local model serving
  • Default endpoint: http://localhost:11434
  • Supports all locally available models

DeepSeek, Qwen, Zhipu

  • Chinese LLM providers
  • OpenAI-compatible API format
  • Require API keys from respective platforms

Moonshot (Kimi)

  • Long-context specialist (8K/32K/128K)
  • API endpoint: https://api.moonshot.cn/v1
  • OpenAI-compatible format

Baidu ERNIE (ๆ–‡ๅฟƒไธ€่จ€)

  • Requires both api_key and secret_key
  • OAuth-based authentication
  • Use secret_key parameter when building provider
from weav_provider_router import chat

response = chat(
    provider="baidu",
    api_key="your-api-key",
    secret_key="your-secret-key",  # Required for Baidu
    question="ไฝ ๅฅฝ",
    model="ernie-4.0-8k"
)

Mistral AI

  • European AI provider
  • Supports Mistral Large, Medium, Small
  • Open-source model options

Groq

  • Ultra-fast LLM inference (up to 750 tokens/sec)
  • Supports Llama, Mixtral, Gemma models
  • Best for low-latency applications

Together AI

  • Multi-model aggregation platform
  • Access to Llama, Mistral, Qwen, DeepSeek models
  • Competitive pricing

Cohere

  • Enterprise-focused LLM provider
  • Command R/R+ models
  • Specialized for business applications

MiniMax (ๆตท่žบAI) ๐Ÿ†•

  • Chinese LLM provider
  • Requires both api_key and group_id
  • abab series models
from weav_provider_router.providers import build_provider

llm = build_provider(
    provider="minimax",
    api_key="your-api-key",
    group_id="your-group-id"  # Required!
)

ByteDance Doubao (่ฑ†ๅŒ…) ๐Ÿ†•

  • ByteDance's LLM service
  • Doubao-pro and Doubao-lite models
  • OpenAI-compatible API
response = chat(
    provider="bytedance",
    api_key="your-api-key",
    question="ๅ†™ไธ€้ฆ–่ฏ—",
    model="doubao-pro-32k"
)

NVIDIA NIM ๐Ÿ†•

  • Enterprise GPU-accelerated inference
  • Access to Llama, Mistral, Nemotron models
  • High-performance API
response = chat(
    provider="nvidia",
    api_key="your-nvidia-api-key",
    question="Explain neural networks",
    model="nvidia/llama-3.1-nemotron-70b-instruct"
)

API Reference

High-Level Functions

chat(provider, api_key, question, **kwargs) -> str

Synchronous chat completion.

Parameters:

  • provider (str): Provider name ("openai", "anthropic", etc.)
  • api_key (str | None): API key for the provider
  • question (str): User question/prompt
  • model (str, optional): Model identifier
  • base_url (str, optional): Custom API endpoint
  • temperature (float, optional): Sampling temperature
  • max_tokens (int, optional): Maximum tokens
  • top_p (float, optional): Nucleus sampling
  • stop (list[str], optional): Stop sequences
  • extra (dict, optional): Provider-specific parameters

Returns: Generated response as string

chat_async(provider, api_key, question, **kwargs) -> str

Asynchronous chat completion. Same parameters as chat().

complete(provider, api_key, prompt, **kwargs) -> str

Synchronous text completion.

complete_async(provider, api_key, prompt, **kwargs) -> str

Asynchronous text completion.

list_models(provider, api_key, *, base_url) -> list[str]

List available models for a provider.

Provider Builder

build_provider(provider, api_key, base_url) -> LLMBase

Create a provider adapter instance.

Parameters:

  • provider (str): Provider name
  • api_key (str | None): API key
  • base_url (str | None): Custom endpoint

Returns: Provider adapter instance

Development

Setting Up Development Environment

# Clone the repository
git clone <repository-url>
cd weav-provider-router

# Install in development mode
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=weav_provider_router --cov-report=html

Running Tests

# Run all tests
pytest

# Run specific test file
pytest tests/test_providers.py

# Run with verbose output
pytest -v

# Run integration tests (requires API keys)
pytest -m integration

Project Structure

weav_provider_router/
โ”œโ”€โ”€ __init__.py           # Public API exports
โ”œโ”€โ”€ api.py                # High-level API functions
โ”œโ”€โ”€ providers.py          # Provider builder and registry
โ”œโ”€โ”€ base.py               # Base classes and interfaces
โ””โ”€โ”€ adapters/             # Provider adapters
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ openai.py
    โ”œโ”€โ”€ anthropic.py
    โ”œโ”€โ”€ google.py
    โ”œโ”€โ”€ ollama.py
    โ”œโ”€โ”€ deepseek.py
    โ”œโ”€โ”€ qwen.py
    โ””โ”€โ”€ zhipu.py

Testing

The package includes comprehensive tests:

  • Unit Tests: Test individual adapters and functions
  • Integration Tests: Test real API calls (require API keys)
  • Mock Tests: Test without external dependencies

Set environment variables for integration tests:

export OPENAI_API_KEY="your-key"
export ANTHROPIC_API_KEY="your-key"
export GOOGLE_API_KEY="your-key"
export DEEPSEEK_API_KEY="your-key"
export QWEN_API_KEY="your-key"
export ZHIPU_API_KEY="your-key"

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Adding a New Provider

  1. Create a new adapter in adapters/your_provider.py
  2. Implement the LLMBase interface
  3. Register the provider in providers.py
  4. Add tests in tests/test_your_provider.py
  5. Update documentation

Example adapter template:

from __future__ import annotations
from typing import AsyncIterator
from ..base import CompletionConfig, LLMBase

class YourProviderChat(LLMBase):
    def __init__(self, api_key: str | None = None, base_url: str | None = None):
        # Initialize your provider client
        pass
    
    async def chat(self, messages: list[dict[str, str]], config: CompletionConfig) -> str:
        # Implement chat completion
        pass
    
    async def complete(self, prompt: str, config: CompletionConfig) -> str:
        # Implement text completion
        pass
    
    async def stream(self, messages: list[dict[str, str]], config: CompletionConfig) -> AsyncIterator[str]:
        # Implement streaming
        pass
    
    def list_models(self) -> list[str]:
        # List available models
        pass

License

MIT License - See LICENSE file for details

Support

For issues and questions:

  • GitHub Issues: [Report a bug or request a feature]
  • Documentation: [Link to full documentation]

Changelog

See CHANGELOG.md for version history and updates.

Acknowledgments

Built with โค๏ธ for the Weav ecosystem.

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

weav_provider_router-0.2.0.tar.gz (45.3 kB view details)

Uploaded Source

Built Distribution

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

weav_provider_router-0.2.0-py3-none-any.whl (36.4 kB view details)

Uploaded Python 3

File details

Details for the file weav_provider_router-0.2.0.tar.gz.

File metadata

  • Download URL: weav_provider_router-0.2.0.tar.gz
  • Upload date:
  • Size: 45.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for weav_provider_router-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d7a688d6f50221021a37587e58a08dc375555d15faddf37a26143eb1bc749402
MD5 658796086e66b35145225b41a7975b70
BLAKE2b-256 11f9996e34ad70e71c116ac410d2cc8cb2fa7eaa9327a0d58930f9c42c3c71bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for weav_provider_router-0.2.0.tar.gz:

Publisher: publish.yml on zeturn/weav-provider-router

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file weav_provider_router-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for weav_provider_router-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b9f090bd2008e8957c1dd0201c61daeefd0fd4c7cff1cbd6ad32bcfdba50074d
MD5 eec52ca0de5d5aeba9af25e5030a9816
BLAKE2b-256 c68cee2ee7a8b495debe2fa0f3a7438504d92e2d4799b0543bc1131e3ce0d642

See more details on using hashes here.

Provenance

The following attestation bundles were made for weav_provider_router-0.2.0-py3-none-any.whl:

Publisher: publish.yml on zeturn/weav-provider-router

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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