Loading...
Official Rax AI SDK for Python applications
Choose your preferred package manager
pip install rax-aipipenv install rax-aipoetry add rax-aiconda install -c conda-forge rax-aiRequirements: Python 3.8 or higher. The SDK uses httpx for HTTP requests and supports both sync and async operations.
Best practice: Use environment variables
# Using environment variables (recommended)
import os
from rax_ai import RaxAI
# Set in your environment or .env file
# export RAX_API_KEY="rax_your_api_key"
# Client will auto-detect from environment
client = RaxAI()
# Or use python-dotenv
from dotenv import load_dotenv
load_dotenv()
client = RaxAI(api_key=os.getenv("RAX_API_KEY"))Get up and running in under 5 minutes
from rax_ai import RaxAI
# Initialize the client
client = RaxAI(api_key="rax_your_api_key")
# Make your first request
response = client.chat(
model="rax-4.0",
messages=[
{"role": "user", "content": "Hello, how are you?"}
]
)
print(response.choices[0].message.content)Send messages and receive AI responses
from rax_ai import RaxAI
client = RaxAI(api_key="rax_your_api_key")
# Simple chat completion
response = client.chat(
model="rax-4.0",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
],
temperature=0.7,
max_tokens=1000
)
# Get the response
print(response.choices[0].message.content)
# Output: "The capital of France is Paris."
# Access usage information
print(f"Tokens used: {response.usage.total_tokens}")Get responses in real-time as they're generated
from rax_ai import RaxAI
client = RaxAI(api_key="rax_your_api_key")
# Stream responses in real-time
stream = client.chat_stream(
model="rax-4.5",
messages=[
{"role": "user", "content": "Write a short poem about Python."}
]
)
# Process chunks as they arrive
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
print() # New line at the endUse async for better performance in async applications
import asyncio
from rax_ai import AsyncRaxAI
async def main():
# Initialize async client
client = AsyncRaxAI(api_key="rax_your_api_key")
# Async chat completion
response = await client.chat(
model="rax-4.0",
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(response.choices[0].message.content)
# Async streaming
stream = await client.chat_stream(
model="rax-4.5",
messages=[
{"role": "user", "content": "Tell me a joke."}
]
)
async for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
# Don't forget to close the client
await client.close()
# Run the async function
asyncio.run(main())Tip: Use AsyncRaxAI when building async applications with FastAPI, aiohttp, or other async frameworks. Don't forget to call await client.close() when done.
Build chatbots with conversation history
from rax_ai import RaxAI
client = RaxAI(api_key="rax_your_api_key")
# Maintain conversation history
messages = [
{"role": "system", "content": "You are a helpful coding assistant."}
]
def chat(user_message: str) -> str:
"""Send a message and get a response, maintaining history."""
# Add user message
messages.append({"role": "user", "content": user_message})
# Get response
response = client.chat(
model="rax-4.5",
messages=messages
)
assistant_message = response.choices[0].message.content
# Add to history for context
messages.append({"role": "assistant", "content": assistant_message})
return assistant_message
# Multi-turn conversation
print(chat("How do I read a file in Python?"))
print(chat("What about handling exceptions?"))
print(chat("Can you show me a complete example?"))Built-in exception classes for easy handling
from rax_ai import RaxAI, RaxAIError, RateLimitError, AuthError
client = RaxAI(api_key="rax_your_api_key")
try:
response = client.chat(
model="rax-4.0",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
except RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after} seconds")
# Implement exponential backoff
except AuthError as e:
print("Invalid API key. Please check your credentials.")
except RaxAIError as e:
# Catch all Rax AI errors
print(f"API Error: {e.message}")
print(f"Status: {e.status_code}")
print(f"Code: {e.error_code}")
except Exception as e:
# Network or other errors
print(f"Unexpected error: {e}")ValidationErrorInvalid requestAuthErrorInvalid API keyRateLimitErrorToo many requestsServerErrorServer errorComplete type annotations for better IDE support
from rax_ai import RaxAI
from rax_ai.types import (
ChatMessage,
ChatResponse,
StreamChunk,
Model,
Usage
)
# Type-annotated client
client: RaxAI = RaxAI(api_key="rax_your_api_key")
# Type-safe messages
messages: list[ChatMessage] = [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Hello!"}
]
# Type-safe response
response: ChatResponse = client.chat(
model="rax-4.0",
messages=messages
)
# Access typed properties
content: str = response.choices[0].message.content
usage: Usage = response.usage
tokens: int = usage.total_tokens
# Type-safe streaming
from typing import Iterator
stream: Iterator[StreamChunk] = client.chat_stream(
model="rax-4.5",
messages=messages
)
for chunk in stream:
delta_content: str | None = chunk.choices[0].delta.contentFast and efficient for everyday tasks
rax-4.0Enhanced reasoning for complex tasks
rax-4.5Complete API reference
from rax_ai import RaxAI, AsyncRaxAI
# Sync Client
client = RaxAI(
api_key: str, # Your API key (required)
base_url: str = None, # Custom API URL (optional)
timeout: float = 30.0, # Request timeout in seconds
max_retries: int = 2, # Auto-retry count
)
# Methods
client.chat(...) # Chat completion
client.chat_stream(...) # Streaming chat
client.models() # List models
client.usage() # Get usage stats
# Async Client
async_client = AsyncRaxAI(
api_key: str,
base_url: str = None,
timeout: float = 30.0,
max_retries: int = 2,
)
# Async Methods
await async_client.chat(...)
await async_client.chat_stream(...)
await async_client.models()
await async_client.usage()
await async_client.close() # Clean up