A universal AI interface layer for seamless LLM provider switching
Project description
UniAI
A Universal AI Interface Layer for Python
UniAI simplifies working with multiple LLM providers by offering a unified interface with automatic context management and streaming support.
Features
- 🔄 Unified Interface: Single API for multiple LLM providers
- 🔀 Easy Provider Switching: Switch between OpenAI, DeepSeek, and more with one line
- 💬 Automatic Context Management: Built-in conversation history tracking
- 🌊 Streaming Support: Stream responses in real-time
- 🛡️ Type Safety: Full type hints and Pydantic validation
- 🔧 Extensible: Easy to add custom providers
Installation
pip install uniai
Or install from source:
git clone https://github.com/Archie818/uniai.git
cd uniai
pip install -e .
Quick Start
from uniai import UniAI
# Initialize with your preferred provider
bot = UniAI(
provider="openai",
api_key="sk-your-api-key",
model="gpt-4o-mini"
)
# Simple chat
response = bot.chat("Hello, who are you?")
print(response)
# Multi-turn conversation (context is automatically managed)
response = bot.chat("What can you help me with?")
print(response)
response = bot.chat("Tell me more about the first thing you mentioned")
print(response)
Streaming Responses
from uniai import UniAI
bot = UniAI(provider="openai", api_key="sk-...", model="gpt-4o-mini")
# Stream response chunks
for chunk in bot.stream("Tell me a story about a brave knight"):
print(chunk, end="", flush=True)
print() # New line at the end
Switching Providers
from uniai import UniAI
# Start with OpenAI
bot = UniAI(provider="openai", api_key="sk-openai-key", model="gpt-4o-mini")
response = bot.chat("Hello!")
# Switch to DeepSeek (preserves conversation history by default)
bot.switch_provider(
provider="deepseek",
api_key="sk-deepseek-key",
model="deepseek-chat"
)
response = bot.chat("Continue our conversation")
# Switch and clear history
bot.switch_provider(
provider="openai",
api_key="sk-openai-key",
model="gpt-4o",
keep_history=False
)
Supported Providers
| Provider | Models | Status |
|---|---|---|
| OpenAI | gpt-4o, gpt-4o-mini, gpt-4, gpt-3.5-turbo | ✅ Supported |
| DeepSeek | deepseek-chat, deepseek-coder | ✅ Supported |
| Claude | Coming soon | 🚧 Planned |
Advanced Usage
System Prompt
bot = UniAI(
provider="openai",
api_key="sk-...",
model="gpt-4o-mini",
system_prompt="You are a helpful coding assistant. Always provide code examples."
)
Conversation History Management
# Limit history to prevent context overflow
bot = UniAI(
provider="openai",
api_key="sk-...",
model="gpt-4o-mini",
max_history=20 # Keep only last 20 messages
)
# Get conversation history
history = bot.get_history()
print(history)
# Clear history
bot.clear_history()
# Access memory directly
print(f"Messages in memory: {len(bot.memory)}")
Full Response Object
# Get detailed response with metadata
response = bot.chat_with_response("What is Python?")
print(f"Content: {response.content}")
print(f"Model: {response.model}")
print(f"Tokens used: {response.usage.total_tokens}")
print(f"Finish reason: {response.finish_reason}")
Configuration Options
bot = UniAI(
provider="openai",
api_key="sk-...",
model="gpt-4o-mini",
base_url=None, # Custom API endpoint
system_prompt=None, # System instructions
temperature=1.0, # Randomness (0.0-2.0)
max_tokens=None, # Max response length
max_history=None, # Message history limit
timeout=60.0, # Request timeout (seconds)
max_retries=3, # Retry attempts
)
Architecture
uniai/
├── __init__.py # Package exports
├── client.py # Main UniAI class
├── exceptions.py # Custom exceptions
├── core/
│ ├── base.py # Abstract BaseProvider
│ ├── config.py # Pydantic config models
│ └── types.py # Type definitions
├── context/
│ └── memory.py # Conversation memory
└── providers/
├── openai.py # OpenAI implementation
└── deepseek.py # DeepSeek implementation
Adding Custom Providers
from uniai.core.base import BaseProvider
from uniai.providers import register_provider
class MyCustomProvider(BaseProvider):
name = "custom"
def _init_client(self):
# Initialize your client
pass
def chat(self, messages):
# Implement chat
pass
def stream_chat(self, messages):
# Implement streaming
pass
# Register the provider
register_provider("custom", MyCustomProvider)
# Now you can use it
bot = UniAI(provider="custom", api_key="...", model="...")
Error Handling
from uniai import UniAI
from uniai.exceptions import (
UniAIError,
AuthenticationError,
RateLimitError,
APIError,
)
bot = UniAI(provider="openai", api_key="sk-...", model="gpt-4o-mini")
try:
response = bot.chat("Hello!")
except AuthenticationError:
print("Invalid API key")
except RateLimitError:
print("Rate limit exceeded, please wait")
except APIError as e:
print(f"API error: {e}")
except UniAIError as e:
print(f"UniAI error: {e}")
Roadmap
- v0.1: Core functionality
- Unified interface
- OpenAI & DeepSeek support
- Context management
- Streaming
- v0.2: Extended provider support
- Claude/Anthropic
- Google Gemini
- Local models (Ollama)
- v0.3: Advanced features
- Async support
- Function calling
- Token counting
- v1.0: Agent framework
- Agent development
- Multi-model collaboration
- Tool integration
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE for details.
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 uniai-0.1.0.tar.gz.
File metadata
- Download URL: uniai-0.1.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc0ef1b871ebb7652a7a14e3c163f25622e16ac7f764ecd0f789a35913a30524
|
|
| MD5 |
dba662331f542d293bb4e828716a464f
|
|
| BLAKE2b-256 |
c0f6843359feb88efe27dd4bb20bf5a66bcd2eb46a210964628544f97163d333
|
File details
Details for the file uniai-0.1.0-py3-none-any.whl.
File metadata
- Download URL: uniai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb240fd875063309f12e6cb2c68ed47f526a12380bc98dc8568b9632535f2c0c
|
|
| MD5 |
429e0b1f829c3d0aaf375b9408a14010
|
|
| BLAKE2b-256 |
10b701fb23f8180216c2db9ae412ef37a139d371bb6d5b68f05de51d774cc243
|