import os, requests, json
r = requests.post("https://ninjachat.ai/api/v1/chat",
headers={"Authorization": f"Bearer {os.environ['NINJACHAT_API_KEY']}"},
json={
"model": "auto",
"messages": [{"role": "user", "content": "Write a haiku about coding"}],
"stream": True,
"include_routing": True,
},
stream=True
)
for line in r.iter_lines():
if not line:
continue
text = line.decode("utf-8")
if text.startswith("data: ") and text != "data: [DONE]":
chunk = json.loads(text[6:])
if "routing" in chunk:
print(f"Routed to: {chunk['routing']['resolved']}", flush=True)
token = chunk.get("choices", [{}])[0].get("delta", {}).get("content", "")
print(token, end="", flush=True)
print()