Skip to main content

A universal AI interface layer for seamless LLM provider switching

Project description

UniAI

PyPI version Python Versions License: MIT GitHub

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
Claude claude-3.5-sonnet, claude-3-opus ๐Ÿšง Coming Soon
Gemini gemini-pro ๐Ÿšง Planned

โš™๏ธ 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

๐Ÿ—บ๏ธ Roadmap

  • v0.1 - Core functionality (Unified interface, OpenAI & DeepSeek, Context management, Streaming)
  • v0.2 - Extended providers (Claude, Gemini, 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

uniai-0.1.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

uniai-0.1.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file uniai-0.1.1.tar.gz.

File metadata

  • Download URL: uniai-0.1.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for uniai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3062a54a75d6a536144054ed29e3e4b583d3c2472a7e06d29d93c09ae1c96e46
MD5 9891976a81ad645f4a67abc8a9bc7fb2
BLAKE2b-256 8ca5978a90b5a5c8be98279170589c88cecf5efc5e6d4c643a41b627bbfb0f4d

See more details on using hashes here.

File details

Details for the file uniai-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: uniai-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for uniai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8aa5a288e240036964fe083f051a89b59d29cb5b23fec53e128cec891c14fedb
MD5 377f0697fab97c7c900945730f6b14c4
BLAKE2b-256 2b3ef3ec62896ae29555b550009b8d5c74dc6d61a1da769561dc547ce7d485d0

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