Official Python SDK for Rax AI Platform - Simple, powerful, and integrated.
pip install rax-aifromrax_aiimportRaxAIrax=RaxAI(api_key="your-api-key")
response=rax.chat(
model="rax-4.0",
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(response["choices"][0]["message"]["content"])- ✅ Simple API - Clean, intuitive methods
- ✅ Python 3.7+ - Compatible with most Python versions
- ✅ Auto Retry - Smart error handling with exponential backoff
- ✅ Type Hints - Full typing support
- ✅ Models API - List and manage available models
- ✅ Usage Tracking - Monitor your API consumption
- ✅ Streaming - Real-time response streaming (coming soon)
fromrax_aiimportRaxAIrax=RaxAI(
api_key="your-key",
base_url="https://ai.raxcore.dev/api", # defaulttimeout=30# seconds, default
)response=rax.chat(
model="rax-4.0",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing"}
],
max_tokens=150,
temperature=0.7,
top_p=0.9
)
print(response["choices"][0]["message"]["content"])forchunkinrax.chat_stream(
model="rax-4.0",
messages=[{"role": "user", "content": "Tell me a story..."}]
):
print(chunk, end="", flush=True)# Get available modelsmodels=rax.get_models()
print("Available models:", [m["id"] forminmodels["data"]])
# Validate API keyis_valid=rax.validate_key()
print("Key valid:", is_valid)# Get current usageusage=rax.get_usage()
print("Total requests:", usage["total_requests"])
print("Total tokens:", usage["total_tokens"])
# Get usage for date rangemonthly_usage=rax.get_usage(
start_date="2024-01-01", end_date="2024-01-31"
)
print("Monthly breakdown:", monthly_usage["daily_breakdown"])fromrax_aiimportRaxAI, RaxAIErrortry:
response=rax.chat(
model="rax-4.0",
messages=[{"role": "user", "content": "Hello!"}]
)
exceptRaxAIErrorase:
print(f"API Error: {e.message}")
print(f"Status: {e.status_code}")
print(f"Type: {e.error_type}")
print(f"Details: {e.to_dict()}")# .env
RAX_AI_API_KEY=your-key-hereimportosfromrax_aiimportRaxAIrax=RaxAI(api_key=os.getenv("RAX_AI_API_KEY"))fromfastapiimportFastAPIfromrax_aiimportRaxAIapp=FastAPI()
rax=RaxAI(api_key=os.getenv("RAX_AI_API_KEY"))
@app.post("/chat")asyncdefchat_endpoint(message: str):
response=rax.chat(
model="rax-4.0",
messages=[{"role": "user", "content": message}]
)
returnresponsefromflaskimportFlask, request, jsonifyfromrax_aiimportRaxAIapp=Flask(__name__)
rax=RaxAI(api_key=os.getenv("RAX_AI_API_KEY"))
@app.route("/chat", methods=["POST"])defchat():
message=request.json["message"]
response=rax.chat(
model="rax-4.0",
messages=[{"role": "user", "content": message}]
)
returnjsonify(response)# views.pyfromdjango.httpimportJsonResponsefromrax_aiimportRaxAIimportjsonrax=RaxAI(api_key=settings.RAX_AI_API_KEY)
defchat_view(request):
data=json.loads(request.body)
response=rax.chat(
model="rax-4.0",
messages=[{"role": "user", "content": data["message"]}]
)
returnJsonResponse(response)# Get current configconfig=rax.get_config()
print("Base URL:", config["base_url"])
print("Timeout:", config["timeout"])- 3 automatic retries for network errors
- Exponential backoff (1s, 2s, 4s delays)
- Smart error classification (no retry for 4xx errors)
- Timeout handling with configurable timeouts
- ✅ Python 3.7+
- ✅ Python 3.8+
- ✅ Python 3.9+
- ✅ Python 3.10+
- ✅ Python 3.11+
- ✅ Python 3.12+
- Visit ai.raxcore.dev
- Sign up or log in
- Navigate to API Keys
- Create a new key
- Start building!
Full type hints included for better IDE support:
fromrax_ai.typesimportChatMessage, ChatRequest, ChatResponsemessages: List[ChatMessage] = [
{"role": "user", "content": "Hello!"}
]
response: ChatResponse=rax.chat(
model="rax-4.0",
messages=messages
)Built for the Rax AI Platform at ai.raxcore.dev