A universal AI interface layer for seamless LLM provider switching
Project description
UniAI
A Universal AI Interface Layer for Python
Seamlessly connect to and switch between LLM providers with unified context management and streaming support.
โจ Features
| Feature | Description |
|---|---|
| ๐ Unified Interface | Single API for multiple LLM providers |
| ๐ Easy Switching | Switch between OpenAI, DeepSeek, and more with one line |
| ๐ฌ Context Management | Built-in conversation history tracking |
| ๐ Streaming | Stream responses in real-time |
| ๐ก๏ธ Type Safety | Full type hints and Pydantic validation |
| ๐ง Extensible | Easy to add custom providers |
๐ฆ Installation
pip install uniai
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-...", 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?")
response = bot.chat("Tell me more about the first thing you mentioned")
๐ Streaming
for chunk in bot.stream("Tell me a story about a brave knight"):
print(chunk, end="", flush=True)
๐ Switch Providers
# Start with OpenAI
bot = UniAI(provider="openai", api_key="sk-openai-key", model="gpt-4o-mini")
bot.chat("Hello!")
# Switch to DeepSeek (preserves history)
bot.switch_provider(provider="deepseek", api_key="sk-deepseek-key", model="deepseek-chat")
bot.chat("Continue our conversation")
# Switch and clear history
bot.switch_provider(provider="openai", api_key="sk-...", 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 |
| Gemini | gemini-2.0-flash, gemini-1.5-pro | โ Supported |
| Claude | claude-3.5-sonnet, claude-3-opus | ๐ง Coming Soon |
โ๏ธ Configuration
bot = UniAI(
provider="openai",
api_key="sk-...",
model="gpt-4o-mini",
system_prompt="You are a helpful assistant.", # System instructions
temperature=0.7, # Randomness (0.0-2.0)
max_tokens=2048, # Max response length
max_history=20, # Conversation history limit
timeout=60.0, # Request timeout (seconds)
max_retries=3, # Retry attempts
)
๐ Advanced Usage
Full Response Object
response = bot.chat_with_response("What is Python?")
print(f"Content: {response.content}")
print(f"Model: {response.model}")
print(f"Tokens: {response.usage.total_tokens}")
print(f"Finish reason: {response.finish_reason}")
History Management
history = bot.get_history() # Get conversation history
bot.clear_history() # Clear history
print(len(bot.memory)) # Messages count
Custom Providers
from uniai.core.base import BaseProvider
from uniai.providers import register_provider
class MyProvider(BaseProvider):
name = "custom"
def _init_client(self):
pass
def chat(self, messages):
# Your implementation
pass
def stream_chat(self, messages):
# Your implementation
pass
register_provider("custom", MyProvider)
bot = UniAI(provider="custom", api_key="...", model="...")
๐ก๏ธ Error Handling
from uniai.exceptions import AuthenticationError, RateLimitError, APIError
try:
response = bot.chat("Hello!")
except AuthenticationError:
print("Invalid API key")
except RateLimitError:
print("Rate limit exceeded")
except APIError as e:
print(f"API error: {e}")
๐๏ธ Architecture
uniai/
โโโ 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 provider
โโโ deepseek.py # DeepSeek provider
โโโ gemini.py # Gemini provider
๐บ๏ธ Roadmap
- v0.1 - Core functionality (Unified interface, OpenAI & DeepSeek, Context management, Streaming)
- v0.1.3 - Gemini support
- v0.2 - Extended providers (Claude, Ollama)
- v0.3 - Advanced features (Async support, Function calling, Token counting)
- v1.0 - Agent framework (Multi-model collaboration, Tool integration)
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ License
MIT License ยฉ 2024 UniAI Contributors
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.3.tar.gz.
File metadata
- Download URL: uniai-0.1.3.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
268f701ddcddf5c7dec1f2fa3fe43481d0b4d3217a02318ab7629f193c349cda
|
|
| MD5 |
b9a28287df6150fe372f83f00ce71013
|
|
| BLAKE2b-256 |
d0086890fe4d7119be9b477a25cef2557ca53eb0c097e0007333e874356b6492
|
File details
Details for the file uniai-0.1.3-py3-none-any.whl.
File metadata
- Download URL: uniai-0.1.3-py3-none-any.whl
- Upload date:
- Size: 18.0 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 |
2d42e6cc448e09cac73fd19039115d0efba12035ad4913aedbc2fff14885541a
|
|
| MD5 |
0333feff61c4da82509692f2a580379b
|
|
| BLAKE2b-256 |
995c0d054edc558a4989a9381777ad075dc598f0fc62d758772589f4e28be416
|