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
requestsfor 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 instancegenerate(prompt, history=None, **kwargs): Generate responseget_available_models(): Get available modelsget_provider_info(): Get provider informationswitch_provider(provider): Switch to different providercreate_provider(name, **kwargs): Class method to create providerregister_provider(name, class): Class method to register custom provider
LLMResponse
content: Generated text contentmodel: Model used for generationusage: Usage statistics (tokens, etc.)metadata: Additional metadata
Message
role: Message role ('user', 'assistant', 'system')content: Message contentto_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
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Run the test suite
- 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
Metadata
Release files for llm-provider 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llm_provider-1.0.0.tar.gz | 17.8 kB | Without provenance | Details for llm_provider-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llm_provider-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for llm_provider-1.0.0-py3-none-any.whl |
Total release size: 34.8 kB
Release files / llm_provider-1.0.0.tar.gz
| Download URL | llm_provider-1.0.0.tar.gz |
|---|---|
| Size | 17.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
068741adfc09ef35968270ff1895fef96741a8d66a77c0b438ccd2ec2e4b91f1
|
|
BLAKE2b-256 checksum How to use checksums |
81e10cd572e2c9071ae823c60713b144e4423b443a7b83451969fefa7aaa4936
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / llm_provider-1.0.0-py3-none-any.whl
| Download URL | llm_provider-1.0.0-py3-none-any.whl |
|---|---|
| Size | 17.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d6f3024f7ab48b5a133b18787e8d947002443f40bb0802b4c7f03d5996889ddd
|
|
BLAKE2b-256 checksum How to use checksums |
88772a978538810b32ef3e51844a1319762b59bca416bc55cb046e8fc3a88057
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Security for llm-provider 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.