31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
import asyncio, httpx, os, json
|
|
from dotenv import load_dotenv
|
|
load_dotenv()
|
|
|
|
KEY = os.getenv("ROUTER_KEY")
|
|
URL = "https://openrouter.ai/api/v1/chat/completions"
|
|
|
|
async def test():
|
|
# Minimal test
|
|
payload = {
|
|
"model": "google/gemini-2.5-flash",
|
|
"messages": [{"role": "user", "content": "Say hi"}],
|
|
"stream": True,
|
|
}
|
|
headers = {"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"}
|
|
async with httpx.AsyncClient(timeout=30) as c:
|
|
async with c.stream("POST", URL, headers=headers, json=payload) as r:
|
|
print("status:", r.status_code)
|
|
async for line in r.aiter_lines():
|
|
if line.startswith("data: ") and line[6:] != "[DONE]":
|
|
d = json.loads(line[6:])
|
|
content = d.get("choices", [{}])[0].get("delta", {}).get("content", "")
|
|
if content:
|
|
print("chunk:", repr(content))
|
|
return
|
|
if d.get("error"):
|
|
print("ERROR:", d["error"])
|
|
return
|
|
|
|
asyncio.run(test())
|