Spaces:
Running
Running
Rachit-Mangawa commited on
Commit ·
83674d5
1
Parent(s): be5289d
fix(api): refactor OpenRouter call with model hierarchy and mandatory headers
Browse files
app.py
CHANGED
|
@@ -62,18 +62,42 @@ async def call_llm_local(prompt: str, system: str = "") -> Optional[Dict]:
|
|
| 62 |
|
| 63 |
async def call_llm_api(prompt: str, system: str = "") -> Optional[Dict]:
|
| 64 |
if not OPENROUTER_KEYS: return None
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
}
|
|
|
|
| 72 |
async with httpx.AsyncClient() as client:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def prune_message(text: str) -> str:
|
| 79 |
text = text.replace("\n", " ").strip()
|
|
|
|
| 62 |
|
| 63 |
async def call_llm_api(prompt: str, system: str = "") -> Optional[Dict]:
|
| 64 |
if not OPENROUTER_KEYS: return None
|
| 65 |
+
|
| 66 |
+
# Tier 1 Hierarchy: Primary -> Secondary -> Backup
|
| 67 |
+
models = ["poolside/laguna-m.1:free", "tencent/hy3:free", "google/gemini-2.0-flash-exp:free"]
|
| 68 |
+
|
| 69 |
+
key = OPENROUTER_KEYS[0]
|
| 70 |
+
headers = {
|
| 71 |
+
"Authorization": f"Bearer {key}",
|
| 72 |
+
"Content-Type": "application/json",
|
| 73 |
+
"HTTP-Referer": "https://huggingface.co/spaces",
|
| 74 |
+
"X-Title": "Vera Engine Platinum"
|
| 75 |
}
|
| 76 |
+
|
| 77 |
async with httpx.AsyncClient() as client:
|
| 78 |
+
for model in models:
|
| 79 |
+
payload = {
|
| 80 |
+
"model": model,
|
| 81 |
+
"messages": [
|
| 82 |
+
{"role": "system", "content": system + "\n\nRETURN JSON ONLY. NO MARKDOWN. NO FABRICATION."},
|
| 83 |
+
{"role": "user", "content": prompt}
|
| 84 |
+
],
|
| 85 |
+
"response_format": {"type": "json_object"}
|
| 86 |
+
}
|
| 87 |
+
try:
|
| 88 |
+
res = await client.post(
|
| 89 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 90 |
+
headers=headers,
|
| 91 |
+
json=payload,
|
| 92 |
+
timeout=10.0
|
| 93 |
+
)
|
| 94 |
+
if res.status_code == 200:
|
| 95 |
+
return json.loads(res.json()["choices"][0]["message"]["content"])
|
| 96 |
+
logger.warning(f"OpenRouter model {model} failed with status {res.status_code}")
|
| 97 |
+
except Exception as e:
|
| 98 |
+
logger.warning(f"OpenRouter error with model {model}: {e}")
|
| 99 |
+
continue
|
| 100 |
+
return None
|
| 101 |
|
| 102 |
def prune_message(text: str) -> str:
|
| 103 |
text = text.replace("\n", " ").strip()
|