A unified client for various AI providers
Project description
IndoxRouter Client
A unified client for various AI providers, including OpenAI, Anthropic, Cohere, 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
client = Client(api_key="your_api_key", base_url="http://your-server-url:8000")
# Or initialize with username and password
client = Client(
username="your_username",
password="your_password",
base_url="http://your-server-url:8000"
)
# Using environment variables
# Set INDOXROUTER_API_KEY or INDOXROUTER_USERNAME and INDOXROUTER_PASSWORD
client = Client(base_url="http://your-server-url:8000")
Chat Completions
response = client.chat(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a joke."}
],
provider="openai",
model="gpt-3.5-turbo",
temperature=0.7
)
print(response["choices"][0]["message"]["content"])
Text Completions
response = client.completion(
prompt="Once upon a time,",
provider="openai",
model="gpt-3.5-turbo-instruct",
max_tokens=100
)
print(response["choices"][0]["text"])
Embeddings
response = client.embeddings(
text=["Hello world", "AI is amazing"],
provider="openai",
model="text-embedding-ada-002"
)
print(f"Dimensions: {response['dimensions']}")
print(f"First embedding: {response['embeddings'][0][:5]}...")
Image Generation
response = client.images(
prompt="A serene landscape with mountains and a lake",
provider="openai",
model="dall-e-3",
size="1024x1024"
)
print(f"Image URL: {response['images'][0]['url']}")
Streaming Responses
for chunk in client.chat(
messages=[{"role": "user", "content": "Write a short story."}],
stream=True
):
if "choices" in chunk 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['name']}")
# 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", base_url="http://your-server-url:8000")
response = client.chat(
messages=[{"role": "user", "content": "Hello"}],
provider="nonexistent",
model="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", base_url="http://your-server-url:8000") as client:
response = client.chat([{"role": "user", "content": "Hello!"}])
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.3.tar.gz
(19.0 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.3.tar.gz.
File metadata
- Download URL: indoxrouter-0.1.3.tar.gz
- Upload date:
- Size: 19.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f7c4201f4ecae3dae70201eeae8ed16778b4e89208b09eaa506a0dbc18a2391
|
|
| MD5 |
6d6f3eecda624e98247aac1f858ec435
|
|
| BLAKE2b-256 |
b36a93022ec715163dcaa01a58e0a7a6ad7eb56b7c77869abf5206b317e9852b
|
File details
Details for the file indoxrouter-0.1.3-py3-none-any.whl.
File metadata
- Download URL: indoxrouter-0.1.3-py3-none-any.whl
- Upload date:
- Size: 2.8 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 |
c601d5b0571b9d71f1944fb83fe3760f5bfd42f252d671293c5df7bf2e3abcae
|
|
| MD5 |
2c7c69d3866b99ca4189a45f8ca80bcb
|
|
| BLAKE2b-256 |
fcbd05f48ec63ab2088ae7d9fb84ff58153e4fa0db157730504b4cdb71e79be5
|