multiapi / app /model_router.py
Juna190825's picture
Create app/model_router.py
5d286f3 verified
raw
history blame
713 Bytes
import random
import httpx
from .config import OPENROUTER_API_KEY, FREE_MODELS
async def query_model(prompt: str, model: str = None):
if not model:
model = random.choice(FREE_MODELS)
headers = {
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": model,
"messages": [
{"role": "user", "content": prompt}
]
}
async with httpx.AsyncClient() as client:
response = await client.post("https://openrouter.ai/api/v1/chat/completions", json=payload, headers=headers)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]