Skip to main content

A unified interface for multiple LLM providers

Project description

LLM Provider Factory

A unified, extensible interface for multiple Large Language Model providers. Built with clean architecture principles and SOLID design patterns.

Features

  • 🏭 Factory Pattern: Clean, consistent interface across providers
  • 🔌 Extensible: Easy to add new LLM providers
  • 🛡️ Type Safe: Full TypeScript-style typing support
  • 🚀 Production Ready: Comprehensive error handling and logging
  • 📦 Zero Dependencies: Only requires requests for HTTP calls
  • 🎯 SOLID Principles: Clean, maintainable architecture

Supported Providers

  • OpenAI (GPT-3.5, GPT-4, etc.)
  • Anthropic (Claude models)
  • Google Gemini (Gemini Pro, Flash, etc.)

Installation

pip install llm-provider

Quick Start

Method 1: Using Provider Instance (Recommended)

from llm_provider import LLMProviderFactory, OpenAI

# Initialize with provider instance
provider = LLMProviderFactory(OpenAI(api_key="your-openai-key"))

# Generate response
response = provider.generate(
    prompt="Hello, how are you?",
    history=[]
)

print(response.content)

Method 2: Using Factory Method

from llm_provider import LLMProviderFactory

# Create provider using factory method
provider = LLMProviderFactory.create_provider(
    "openai", 
    api_key="your-openai-key"
)

response = provider.generate(prompt="Hello", history=[])
print(response.content)

Advanced Usage

Working with Conversation History

from llm_provider import LLMProviderFactory, OpenAI, Message

provider = LLMProviderFactory(OpenAI(api_key="your-key"))

# Using Message objects
history = [
    Message(role="user", content="What's the capital of France?"),
    Message(role="assistant", content="The capital of France is Paris."),
]

response = provider.generate(
    prompt="What's its population?",
    history=history
)

# Or using dictionaries
history_dict = [
    {"role": "user", "content": "What's the capital of France?"},
    {"role": "assistant", "content": "The capital of France is Paris."},
]

response = provider.generate(
    prompt="What's its population?",
    history=history_dict
)

Custom Generation Parameters

response = provider.generate(
    prompt="Write a creative story",
    temperature=0.9,
    max_tokens=500,
    top_p=0.95
)

Using Different Providers

from llm_provider import LLMProviderFactory, OpenAI, Anthropic, Gemini

# OpenAI
openai_provider = LLMProviderFactory(OpenAI(api_key="openai-key"))

# Anthropic
anthropic_provider = LLMProviderFactory(Anthropic(api_key="anthropic-key"))

# Gemini
gemini_provider = LLMProviderFactory(Gemini(api_key="gemini-key"))

# Switch between providers
factory = LLMProviderFactory(openai_provider.provider)
factory.switch_provider(anthropic_provider.provider)

Error Handling

from llm_provider import (
    LLMProviderFactory, 
    OpenAI, 
    APIError, 
    RateLimitError, 
    AuthenticationError
)

try:
    provider = LLMProviderFactory(OpenAI(api_key="your-key"))
    response = provider.generate("Hello")
    
except AuthenticationError as e:
    print(f"Authentication failed: {e}")
    
except RateLimitError as e:
    print(f"Rate limit exceeded: {e}")
    
except APIError as e:
    print(f"API error: {e}")

Getting Available Models

provider = LLMProviderFactory(OpenAI(api_key="your-key"))
models = provider.get_available_models()
print(f"Available models: {models}")

Provider Information

provider = LLMProviderFactory(OpenAI(api_key="your-key"))
info = provider.get_provider_info()
print(f"Provider: {info['name']}")
print(f"Class: {info['class']}")

Extending with Custom Providers

from llm_provider import BaseLLMProvider, LLMResponse, LLMProviderFactory

class CustomProvider(BaseLLMProvider):
    def _validate_config(self):
        if not self.config.get('api_key'):
            raise ConfigurationError("API key required")
    
    def generate(self, prompt, history=None, **kwargs):
        # Your custom implementation
        return LLMResponse(
            content="Custom response",
            model="custom-model",
            metadata={"provider": "custom"}
        )
    
    def get_available_models(self):
        return ["custom-model-1", "custom-model-2"]

# Register the custom provider
LLMProviderFactory.register_provider("custom", CustomProvider)

# Use it
provider = LLMProviderFactory.create_provider("custom", api_key="test")

Configuration

Environment Variables

export LLM_PROVIDER_TIMEOUT=30
export LLM_PROVIDER_MAX_RETRIES=3
export LLM_PROVIDER_LOG_LEVEL=INFO

Custom Configuration

from llm_provider import Config, LLMProviderFactory, OpenAI

config = Config(
    default_timeout=60,
    default_max_retries=5,
    log_level="DEBUG"
)

provider = LLMProviderFactory(
    OpenAI(api_key="your-key"), 
    config=config
)

API Reference

LLMProviderFactory

  • __init__(provider, config=None): Initialize with provider instance
  • generate(prompt, history=None, **kwargs): Generate response
  • get_available_models(): Get available models
  • get_provider_info(): Get provider information
  • switch_provider(provider): Switch to different provider
  • create_provider(name, **kwargs): Class method to create provider
  • register_provider(name, class): Class method to register custom provider

LLMResponse

  • content: Generated text content
  • model: Model used for generation
  • usage: Usage statistics (tokens, etc.)
  • metadata: Additional metadata

Message

  • role: Message role ('user', 'assistant', 'system')
  • content: Message content
  • to_dict(): Convert to dictionary

Development

Setup Development Environment

git clone https://github.com/sadikhanecioglu/llm-provider
cd llm-provider
pip install -e ".[dev]"

Run Tests

pytest

Code Formatting

black src/ tests/
isort src/ tests/

Type Checking

mypy src/

Architecture

This package follows SOLID principles:

  • Single Responsibility: Each class has one reason to change
  • Open/Closed: Open for extension, closed for modification
  • Liskov Substitution: Providers are interchangeable
  • Interface Segregation: Minimal, focused interfaces
  • Dependency Inversion: Depend on abstractions, not concretions

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Run the test suite
  6. Submit a pull request

Changelog

v1.0.0

  • Initial release
  • Support for OpenAI, Anthropic, and Gemini
  • Factory pattern implementation
  • Comprehensive error handling
  • Full test coverage

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

llm_provider-1.0.0.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

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

llm_provider-1.0.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file llm_provider-1.0.0.tar.gz.

File metadata

  • Download URL: llm_provider-1.0.0.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for llm_provider-1.0.0.tar.gz
Algorithm Hash digest
SHA256 068741adfc09ef35968270ff1895fef96741a8d66a77c0b438ccd2ec2e4b91f1
MD5 1f87b4fd10332715b92a45ca1819fb05
BLAKE2b-256 81e10cd572e2c9071ae823c60713b144e4423b443a7b83451969fefa7aaa4936

See more details on using hashes here.

File details

Details for the file llm_provider-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: llm_provider-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for llm_provider-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6f3024f7ab48b5a133b18787e8d947002443f40bb0802b4c7f03d5996889ddd
MD5 4080e2dd6525847a824ae89bea1916bd
BLAKE2b-256 88772a978538810b32ef3e51844a1319762b59bca416bc55cb046e8fc3a88057

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