Universal Python LLM Provider Abstraction Library
Project description
LLMBridgeKit
Version: 1.0.0
Author: sreeyenan
License: MIT
Python: 3.10+
Universal Python LLM Provider Abstraction Library
A provider-agnostic Python library that lets any AI feature call multiple LLM providers through one consistent interface.
Features
๐ Multi-Provider Support
- Cloud Providers: OpenAI, Azure OpenAI, Google Gemini, Anthropic Claude, Mistral AI, Cohere
- Fast Inference: Groq, Together AI
- Model Routers: OpenRouter, Hugging Face Inference Providers
- Local Models: Ollama, LM Studio
- Custom: Your own HTTP endpoint
๐ฏ Core Capabilities
- Unified API: Single interface for all providers
- Text Generation: Simple
.generate()method - Chat: Message-based
.chat()method - Structured JSON:
.generate_json()with schema validation - Tool Calling:
.generate_with_tools()for function calling - Streaming: Real-time token streaming
- Async Support: Full async/await support
๐ก๏ธ Reliability Features
- Fallback Chains: Automatic provider failover
- Retry Logic: Exponential backoff with jitter
- Timeout Control: Per-request and per-provider timeouts
- Error Normalization: Consistent error handling across providers
- Rate Limiting: Respect provider rate limits
๐ Observability
- Usage Tracking: Token counts and model usage
- Cost Estimation: Calculate API costs
- Latency Metrics: Track request duration
- Audit Logging: Request/response logging with redaction
- Request IDs: Distributed tracing support
๐ Security
- PII Redaction: Mask sensitive data before sending
- Secret Management: Environment variable support
- API Key Rotation: Hot-reload credentials
Installation
# Core installation
pip install llmbridgekit
# With specific providers
pip install llmbridgekit[openai]
pip install llmbridgekit[gemini]
pip install llmbridgekit[anthropic]
# With multiple providers
pip install llmbridgekit[openai,gemini,groq]
# Local model support
pip install llmbridgekit[local]
# Everything
pip install llmbridgekit[all]
Quick Start
Basic Text Generation
from llmbridgekit import LLMClient
client = LLMClient(provider="gemini")
response = client.generate(
prompt="Explain what a materialized view is in ClickHouse."
)
print(response.text)
print(f"Tokens: {response.usage['total_tokens']}")
Chat with Messages
response = client.chat(
messages=[
{"role": "system", "content": "You are a data analytics assistant."},
{"role": "user", "content": "Show revenue by region."}
],
provider="openai",
model="gpt-4o"
)
print(response.text)
Structured JSON Output
schema = {
"type": "object",
"properties": {
"intent": {
"type": "string",
"enum": ["query", "chart", "dashboard", "report"]
},
"confidence": {"type": "number", "minimum": 0, "maximum": 1},
"entities": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["intent", "confidence"]
}
response = client.generate_json(
prompt="User asks: 'Show me sales by region for last quarter'",
schema=schema
)
print(response.json_data)
# {'intent': 'query', 'confidence': 0.95, 'entities': ['sales', 'region', 'last quarter']}
Streaming
for chunk in client.stream(
prompt="Write a detailed analysis of Q4 performance.",
provider="anthropic"
):
print(chunk.text, end="", flush=True)
Fallback Chain
response = client.generate(
prompt="Generate a SQL query for revenue by category.",
fallback_chain=[
{"provider": "gemini", "model": "gemini-2.0-flash-exp"},
{"provider": "groq", "model": "llama-3.3-70b-versatile"},
{"provider": "ollama", "model": "qwen2.5:3b"}
]
)
Tool Calling
tools = [
{
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}
]
response = client.generate_with_tools(
prompt="What's the weather in San Francisco?",
tools=tools,
provider="openai"
)
if response.tool_calls:
for tool_call in response.tool_calls:
print(f"Tool: {tool_call['name']}")
print(f"Args: {tool_call['arguments']}")
Configuration
YAML Configuration
# config.yaml
default_provider: gemini
default_timeout: 60
default_retries: 2
providers:
openai:
api_key_env: OPENAI_API_KEY
model: gpt-4o
temperature: 0
max_tokens: 1000
gemini:
api_key_env: GEMINI_API_KEY
model: gemini-2.0-flash-exp
temperature: 0
ollama:
base_url: http://localhost:11434
model: qwen2.5:3b
temperature: 0
fallback_chains:
default:
- provider: gemini
model: gemini-2.0-flash-exp
- provider: groq
model: llama-3.3-70b-versatile
tasks:
classification:
provider: groq
model: llama-3.3-70b-versatile
temperature: 0
max_tokens: 500
text_to_sql:
provider: gemini
model: gemini-2.0-flash-exp
temperature: 0
fallback_chain: default
Load configuration:
from llmbridgekit import LLMClient
client = LLMClient.from_yaml("config.yaml")
# Use task-based routing
response = client.run_task(
task="text_to_sql",
prompt="Show me revenue by product category"
)
Supported Providers
| Provider | Structured Output | Tool Calling | Streaming | Local | Status |
|---|---|---|---|---|---|
| OpenAI | โ | โ | โ | โ | Stable |
| Azure OpenAI | โ | โ | โ | โ | Stable |
| Google Gemini | โ | โ | โ | โ | Stable |
| Anthropic Claude | โ | โ | โ | โ | Stable |
| Groq | โ | โ | โ | โ | Stable |
| Mistral AI | โ | โ | โ | โ | Stable |
| Cohere | โ | โ | โ | โ | Stable |
| Ollama | โ | โ | โ | โ | Stable |
| LM Studio | โ | โ | โ | โ | Stable |
| OpenRouter | โ | โ | โ | โ | Stable |
| Hugging Face | โ | โ | โ | โ | Stable |
| Together AI | โ | โ | โ | โ | Stable |
| Custom HTTP | โ | โ | โ | โ ๏ธ | Stable |
Use Cases
NLQ (Natural Language Query)
# Context resolution
response = client.generate_json(
prompt=context_resolver_prompt,
schema=context_resolution_schema,
provider="ollama", # Local fallback
model="qwen2.5:3b"
)
# Text-to-SQL generation
response = client.generate(
prompt=sql_generation_prompt,
provider="gemini",
fallback_chain="default"
)
RAG (Retrieval Augmented Generation)
# Generate embeddings
embeddings = client.embed(
texts=["chunk1", "chunk2", "chunk3"],
provider="openai"
)
# Generate answer with context
response = client.chat(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Context: {context}\n\nQuestion: {question}"}
]
)
Chart/Dashboard Generation
response = client.generate_json(
prompt=f"Generate chart config for: {user_request}",
schema=chart_config_schema,
provider="openai",
model="gpt-4o"
)
chart_config = response.json_data
Document Q&A
response = client.chat(
messages=[
{"role": "system", "content": "Answer based on the document."},
{"role": "user", "content": f"Document: {document}\n\nQuestion: {question}"}
],
provider="anthropic",
model="claude-opus-4"
)
Advanced Features
Async Support
import asyncio
from llmbridgekit import LLMClient
async def main():
client = LLMClient(provider="gemini")
response = await client.agenerate(
prompt="Explain async/await in Python"
)
print(response.text)
asyncio.run(main())
Cost Tracking
response = client.generate(
prompt="Analyze sales data",
provider="openai",
model="gpt-4o"
)
print(f"Cost: ${response.cost['total_cost']:.4f}")
print(f"Input tokens: {response.usage['prompt_tokens']}")
print(f"Output tokens: {response.usage['completion_tokens']}")
Redaction
from llmbridgekit import LLMClient, RedactionConfig
client = LLMClient(
provider="openai",
redaction=RedactionConfig(
mask_email=True,
mask_phone=True,
mask_api_keys=True,
custom_patterns=[
r'\b\d{3}-\d{2}-\d{4}\b' # SSN
]
)
)
response = client.generate(
prompt="User email: john@example.com, phone: 555-1234"
)
# Prompt sent: "User email: [EMAIL_REDACTED], phone: [PHONE_REDACTED]"
Custom Provider
from llmbridgekit.providers import BaseProvider, ProviderFeatures
from llmbridgekit import provider_registry
class MyCustomProvider(BaseProvider):
name = "my_custom"
features = ProviderFeatures(
chat=True,
structured_outputs=True,
streaming=True
)
def generate(self, prompt, config):
# Your implementation
response = self._call_api(prompt, config)
return self._parse_response(response)
# Register
provider_registry.register("my_custom", MyCustomProvider)
# Use
client = LLMClient(provider="my_custom")
response = client.generate("Hello!")
Testing
# Install dev dependencies
pip install llm-gateway[dev]
# Run tests
pytest
# Run with coverage
pytest --cov=llmbridgekit --cov-report=html
# Run specific provider tests
pytest tests/providers/test_openai_provider.py -v
Documentation
- User Manual - Complete usage guide
- Changelog - Version history
- Environment Variables - Configuration reference
- Examples - Code examples for common use cases
Architecture
Application Layer
โ
LLMClient (llmbridgekit/client.py)
โ
Task Router / Fallback Chain
โ
Provider Registry
โ
BaseProvider
โ
OpenAI | Gemini | Anthropic | Groq | ... | Custom HTTP
License
MIT License - see LICENSE file for details.
Support
- Issues: https://github.com/sreeyenan/llmbridgekit/issues
- Discussions: https://github.com/analytic-ai/llm-gateway/discussions
- Documentation: https://llm-gateway.readthedocs.io
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llmbridgekit-1.0.3.tar.gz.
File metadata
- Download URL: llmbridgekit-1.0.3.tar.gz
- Upload date:
- Size: 63.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8aa076113394f26147cffbb6cb40dbb406b485ba003d9dcd384974375684aec7
|
|
| MD5 |
a83a7528d2c655d2642be9a40a7b786b
|
|
| BLAKE2b-256 |
4fc5fea05b5e9f2cc391ed27cc410eceb3309c16b0e8276bd2621d95a7a1c6a1
|
Provenance
The following attestation bundles were made for llmbridgekit-1.0.3.tar.gz:
Publisher:
publish.yml on sreeyenan/llmbridgekit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llmbridgekit-1.0.3.tar.gz -
Subject digest:
8aa076113394f26147cffbb6cb40dbb406b485ba003d9dcd384974375684aec7 - Sigstore transparency entry: 1711257541
- Sigstore integration time:
-
Permalink:
sreeyenan/llmbridgekit@1e173ecb2582a50516a98603f35af96f99c71521 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/sreeyenan
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1e173ecb2582a50516a98603f35af96f99c71521 -
Trigger Event:
push
-
Statement type:
File details
Details for the file llmbridgekit-1.0.3-py3-none-any.whl.
File metadata
- Download URL: llmbridgekit-1.0.3-py3-none-any.whl
- Upload date:
- Size: 57.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7a6cb311b93d426040ae2e6fa0328b0fb1eab8c93401b8945315278ac0ce7ce
|
|
| MD5 |
b5c7780226802d503d5378cb6500c38a
|
|
| BLAKE2b-256 |
aeae73c0710badb0acbd0f6e68f545fbda8face69d0258a1a4a58d92ed6f25b2
|
Provenance
The following attestation bundles were made for llmbridgekit-1.0.3-py3-none-any.whl:
Publisher:
publish.yml on sreeyenan/llmbridgekit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llmbridgekit-1.0.3-py3-none-any.whl -
Subject digest:
f7a6cb311b93d426040ae2e6fa0328b0fb1eab8c93401b8945315278ac0ce7ce - Sigstore transparency entry: 1711257798
- Sigstore integration time:
-
Permalink:
sreeyenan/llmbridgekit@1e173ecb2582a50516a98603f35af96f99c71521 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/sreeyenan
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1e173ecb2582a50516a98603f35af96f99c71521 -
Trigger Event:
push
-
Statement type: