A unified client for various AI providers
Project description
IndoxRouter Client
A unified client for various AI providers, including OpenAI, Anthropic, Google, and Mistral.
Features
- Unified API: Access multiple AI providers through a single API
- Simple Interface: Easy-to-use methods for chat, completion, embeddings, and image generation
- Error Handling: Standardized error handling across providers
- Authentication: Automatic token management
Installation
pip install indoxrouter
Usage
Initialization
from indoxrouter import Client
# Initialize with API key (default connects to localhost:8000)
client = Client(api_key="your_api_key")
# Using environment variables
# Set INDOX_ROUTER_API_KEY environment variable
import os
os.environ["INDOX_ROUTER_API_KEY"] = "your_api_key"
client = Client()
Chat Completions
response = client.chat(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a joke."}
],
model="openai/gpt-4o-mini", # Provider/model format
temperature=0.7
)
print(response["choices"][0]["message"]["content"])
Text Completions
response = client.completion(
prompt="Once upon a time,",
model="openai/gpt-4o-mini",
max_tokens=100
)
print(response["choices"][0]["text"])
Embeddings
response = client.embeddings(
text=["Hello world", "AI is amazing"],
model="openai/text-embedding-3-small"
)
print(f"Dimensions: {len(response['data'][0]['embedding'])}")
print(f"First embedding: {response['data'][0]['embedding'][:5]}...")
Image Generation
response = client.images(
prompt="A serene landscape with mountains and a lake",
model="openai/dall-e-3",
size="1024x1024"
)
print(f"Image URL: {response['data'][0]['url']}")
Streaming Responses
for chunk in client.chat(
messages=[{"role": "user", "content": "Write a short story."}],
model="openai/gpt-4o-mini",
stream=True
):
if chunk.get("choices") and len(chunk["choices"]) > 0:
content = chunk["choices"][0].get("delta", {}).get("content", "")
print(content, end="", flush=True)
Getting Available Models
# Get all providers and models
providers = client.models()
for provider in providers:
print(f"Provider: {provider['name']}")
for model in provider["models"]:
print(f" - {model['id']}: {model['description'] or ''}")
# Get models for a specific provider
openai_provider = client.models("openai")
print(f"OpenAI models: {[m['id'] for m in openai_provider['models']]}")
Error Handling
from indoxrouter import Client, ModelNotFoundError, ProviderError
try:
client = Client(api_key="your_api_key")
response = client.chat(
messages=[{"role": "user", "content": "Hello"}],
model="nonexistent-provider/nonexistent-model"
)
except ModelNotFoundError as e:
print(f"Model not found: {e}")
except ProviderError as e:
print(f"Provider error: {e}")
Context Manager
with Client(api_key="your_api_key") as client:
response = client.chat(
messages=[{"role": "user", "content": "Hello!"}],
model="openai/gpt-4o-mini"
)
print(response["choices"][0]["message"]["content"])
# Client is automatically closed when exiting the block
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
indoxrouter-0.1.7.tar.gz
(19.9 kB
view details)
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 indoxrouter-0.1.7.tar.gz.
File metadata
- Download URL: indoxrouter-0.1.7.tar.gz
- Upload date:
- Size: 19.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c72ffca68bf2f0423d881e68a075bd029406da30560cdfc5ac9814dfdf0c79c8
|
|
| MD5 |
47c9c71ac98ce0fee1664a95744362d5
|
|
| BLAKE2b-256 |
5205c0dbd7dfa2ff2cf4def6459c4c8402a25a8f763bca1d72f8c28bee7aa86b
|
File details
Details for the file indoxrouter-0.1.7-py3-none-any.whl.
File metadata
- Download URL: indoxrouter-0.1.7-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c705e5a66c58ab61bc2e4cc1ae50007ebd27b1246945ee0eec141008a15b4ad8
|
|
| MD5 |
6ac1ac29538cd5a410653c0900a632a8
|
|
| BLAKE2b-256 |
c9a6abf579811620b2f34e1e4dd2676061a6495aa5503fd65c854dfb14d166cf
|