Skip to main content

Unified Python interface for multiple AI providers with support for text, images, and documents.

Project description

Multi AI Handler

A unified Python library for interacting with multiple AI providers through a consistent interface. Supports text and file inputs across OpenAI, Anthropic Claude, Google Gemini, OpenRouter, Cerebras and Ollama (local LLMs).

Features

  • Unified interface for multiple AI providers
  • Conversation history for multi-turn interactions
  • Streaming support for real-time token output
  • Async support for concurrent workloads
  • Support for images and documents (PDF)
  • Local LLM support with Ollama
  • Advanced document processing with Docling (OCR, table extraction)
  • Model information retrieval

Installation

pip install multi-ai-handler

Optional dependencies:

pip install multi-ai-handler[ollama]   # Local LLM support
pip install multi-ai-handler[docling]  # Document processing (OCR, tables)
pip install multi-ai-handler[all]      # All optional dependencies

Setup

Create a .env file with your API keys:

ANTHROPIC_API_KEY=your_anthropic_api_key_here
CEREBRAS_API_KEY=your_cerebras_api_key_here
GEMINI_API_KEY=your_gemini_api_key_here
OPENAI_API_KEY=your_openai_api_key_here
OPENROUTER_API_KEY=your_openrouter_api_key_here

Usage

Basic Request

from multi_ai_handler import request_ai

response = request_ai(
    provider="google",  # or "anthropic", "openai", "openrouter", "cerebras", "ollama"
    model="gemini-2.5-flash",
    system_prompt="You are a helpful assistant.",
    user_text="What is the capital of France?"
)

JSON Output

data = request_ai(
    provider="openai",
    model="gpt-4o-mini",
    system_prompt="Return valid JSON only.",
    user_text="Convert to JSON: Name: Alice, Age: 25",
    json_output=True
)
# Returns: {'name': 'Alice', 'age': 25}

File Processing

response = request_ai(
    provider="anthropic",
    model="claude-sonnet-4-5-20250929",
    system_prompt="Summarize this document.",
    file="document.pdf"
)

Streaming

from multi_ai_handler import stream_ai

for chunk in stream_ai(provider="cerebras", model="llama-3.3-70b", user_text="Write a poem"):
    print(chunk, end="", flush=True)

Async Support

import asyncio
from multi_ai_handler import arequest_ai, astream_ai

async def main():
    # Concurrent requests
    responses = await asyncio.gather(
        arequest_ai(provider="google", model="gemini-2.0-flash", user_text="Hello"),
        arequest_ai(provider="anthropic", model="claude-sonnet-4-20250514", user_text="Hello"),
    )

    # Async streaming
    async for chunk in astream_ai(provider="openai", model="gpt-4o-mini", user_text="Hi"):
        print(chunk, end="", flush=True)

asyncio.run(main())

Conversation History

Use the Conversation class for multi-turn interactions:

from multi_ai_handler import AIProviderManager

manager = AIProviderManager()
conv = manager.conversation(
    provider="anthropic",
    model="claude-sonnet-4-20250514",
    system_prompt="You are a helpful assistant.",
)

response = conv.send("My name is Alice.")
print(response.content)

response = conv.send("What's my name?")  # Remembers context
print(response.content)

conv.clear()  # Reset conversation

With file processing:

conv = manager.conversation(provider="google", model="gemini-2.0-flash")

response = conv.send("Summarize this document", file="report.pdf")
print(response.content)

response = conv.send("What are the key findings?")  # Follow-up without re-sending file
print(response.content)

Model Information

from multi_ai_handler import list_models, get_model_info

all_models = list_models()  # {'google': [...], 'anthropic': [...], ...}
info = get_model_info(provider="anthropic", model="claude-sonnet-4-20250514")

API Reference

Functions

Function Description
request_ai(provider, model, ...) Generate a response
stream_ai(provider, model, ...) Stream response tokens
arequest_ai(provider, model, ...) Async generation
astream_ai(provider, model, ...) Async streaming
list_models() List all available models
get_model_info(provider, model) Get model metadata

Parameters

Parameter Type Description
provider str "google", "anthropic", "openai", "openrouter", "cerebras", "ollama"
model str Model name (e.g., "gemini-2.5-flash", "claude-sonnet-4-5-20250929")
system_prompt str System instruction
user_text str User input text
messages list[dict] Conversation history from previous response.history
file str/Path File path for images or documents
temperature float Randomness (0.0-1.0), default: 0.2
json_output bool Parse response as JSON, default: False
local bool Use local text extraction (Docling), default: False

Classes

  • AIProviderManager - Manage providers, register custom providers
  • Conversation - Multi-turn conversation with automatic history management
  • AIProvider - Abstract base class for implementing custom providers
  • Provider classes: AnthropicProvider, GoogleProvider, OpenAIProvider, OpenrouterProvider, OllamaProvider, CerebrasProvider

License

MIT

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Support

For issues and questions, please open an issue on the GitHub repository.

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

multi_ai_handler-2.2.0.tar.gz (168.8 kB view details)

Uploaded Source

Built Distribution

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

multi_ai_handler-2.2.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file multi_ai_handler-2.2.0.tar.gz.

File metadata

  • Download URL: multi_ai_handler-2.2.0.tar.gz
  • Upload date:
  • Size: 168.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for multi_ai_handler-2.2.0.tar.gz
Algorithm Hash digest
SHA256 0a0da21f5645e43e04fb633f6d5c8967d2ee802f96ed0382be86310b1780eb67
MD5 98d331cc7df8947babcc17e51eb28c23
BLAKE2b-256 135f3286b9cc739e32e6d4636834a33511e682e453376b5c4f943c50f74ed49b

See more details on using hashes here.

Provenance

The following attestation bundles were made for multi_ai_handler-2.2.0.tar.gz:

Publisher: publish.yml on vsharha/multi-ai-handler

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file multi_ai_handler-2.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for multi_ai_handler-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 77622c725a6543913b68df7e9a99827705f371d00f345732d0c483f6ff7f3ccc
MD5 71ffcb79d1fe44e4168eff7f96cf5d48
BLAKE2b-256 3c3d7b421b8747d540e23c6fb0a41a566e754ac1c71541b3464283e5449ef6d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for multi_ai_handler-2.2.0-py3-none-any.whl:

Publisher: publish.yml on vsharha/multi-ai-handler

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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