Python & FastAPI Integration
Expose Setu completions within your own FastAPI microservices using asynchronous streaming and httpx.
FastAPI Streaming Proxy
python
import os
import httpx
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
app = FastAPI(title="Company AI Service")
SETU_API_KEY = os.environ.get("SUPREME_API_KEY")
SETU_BASE_URL = "https://supremeai.services/api/setu/v1"
class ChatRequest(BaseModel):
message: str
@app.post("/api/ask")
async def ask_agent(req: ChatRequest):
async def event_generator():
client = httpx.AsyncClient(timeout=60.0)
async with client.stream(
"POST",
f"{SETU_BASE_URL}/chat/completions",
headers={
"Authorization": f"Bearer {SETU_API_KEY}",
"Content-Type": "application/json"
},
json={
"model": "quantum-lite",
"messages": [{"role": "user", "content": req.message}],
"stream": True
}
) as response:
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail="Gateway error")
async for line in response.aiter_lines():
if line:
yield f"{line}\n\n"
return StreamingResponse(event_generator(), media_type="text/event-stream")