Juna190825 commited on
Commit
5d286f3
·
verified ·
1 Parent(s): 2312d81

Create app/model_router.py

Browse files
Files changed (1) hide show
  1. app/model_router.py +24 -0
app/model_router.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import httpx
3
+ from .config import OPENROUTER_API_KEY, FREE_MODELS
4
+
5
+ async def query_model(prompt: str, model: str = None):
6
+ if not model:
7
+ model = random.choice(FREE_MODELS)
8
+
9
+ headers = {
10
+ "Authorization": f"Bearer {OPENROUTER_API_KEY}",
11
+ "Content-Type": "application/json"
12
+ }
13
+
14
+ payload = {
15
+ "model": model,
16
+ "messages": [
17
+ {"role": "user", "content": prompt}
18
+ ]
19
+ }
20
+
21
+ async with httpx.AsyncClient() as client:
22
+ response = await client.post("https://openrouter.ai/api/v1/chat/completions", json=payload, headers=headers)
23
+ response.raise_for_status()
24
+ return response.json()["choices"][0]["message"]["content"]