Spaces:
Sleeping
Sleeping
David Prince commited on
Commit ·
89ed5d6
1
Parent(s): 9348ecf
fix: agent/run uses direct Groq→Cerebras→OpenRouter fallback chain (no Databricks)
Browse files- app_routes_extension.py +41 -17
app_routes_extension.py
CHANGED
|
@@ -94,23 +94,47 @@ class AgentRunRequest(BaseModel):
|
|
| 94 |
|
| 95 |
@ext_router.post("/api/agent/run")
|
| 96 |
async def agent_run(body: AgentRunRequest) -> dict[str, Any]:
|
| 97 |
-
"""Route agent prompts through the LLM
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
def register(app) -> None:
|
|
|
|
| 94 |
|
| 95 |
@ext_router.post("/api/agent/run")
|
| 96 |
async def agent_run(body: AgentRunRequest) -> dict[str, Any]:
|
| 97 |
+
"""Route agent prompts through the production LLM fallback chain (Groq→Cerebras→OpenRouter)."""
|
| 98 |
+
import os, httpx, time as _time
|
| 99 |
+
t0 = _time.monotonic()
|
| 100 |
+
|
| 101 |
+
messages = [{"role": "user", "content": body.message}]
|
| 102 |
+
|
| 103 |
+
chain = []
|
| 104 |
+
if os.environ.get("GROQ_API_KEY"):
|
| 105 |
+
chain.append(("groq", "https://api.groq.com/openai/v1/chat/completions",
|
| 106 |
+
"llama-3.3-70b-versatile", f"Bearer {os.environ['GROQ_API_KEY']}"))
|
| 107 |
+
if os.environ.get("CEREBRAS_API_KEY"):
|
| 108 |
+
chain.append(("cerebras", "https://api.cerebras.ai/v1/chat/completions",
|
| 109 |
+
"llama3.1-70b", f"Bearer {os.environ['CEREBRAS_API_KEY']}"))
|
| 110 |
+
if os.environ.get("OPENROUTER_API_KEY"):
|
| 111 |
+
chain.append(("openrouter", "https://openrouter.ai/api/v1/chat/completions",
|
| 112 |
+
"meta-llama/llama-3.1-8b-instruct:free",
|
| 113 |
+
f"Bearer {os.environ['OPENROUTER_API_KEY']}"))
|
| 114 |
+
|
| 115 |
+
last_error = "No LLM providers configured"
|
| 116 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 117 |
+
for provider_name, url, model, auth in chain:
|
| 118 |
+
try:
|
| 119 |
+
resp = await client.post(url,
|
| 120 |
+
headers={"Authorization": auth, "Content-Type": "application/json"},
|
| 121 |
+
json={"model": model, "messages": messages, "max_tokens": 1024},
|
| 122 |
+
)
|
| 123 |
+
if resp.status_code == 200:
|
| 124 |
+
data = resp.json()
|
| 125 |
+
text = data["choices"][0]["message"]["content"]
|
| 126 |
+
return {
|
| 127 |
+
"response": text,
|
| 128 |
+
"provider": provider_name,
|
| 129 |
+
"model": model,
|
| 130 |
+
"latency_ms": round((_time.monotonic() - t0) * 1000),
|
| 131 |
+
}
|
| 132 |
+
last_error = f"{provider_name} returned {resp.status_code}: {resp.text[:200]}"
|
| 133 |
+
except Exception as exc:
|
| 134 |
+
last_error = f"{provider_name} error: {exc}"
|
| 135 |
+
continue
|
| 136 |
+
|
| 137 |
+
return {"error": last_error, "latency_ms": round((_time.monotonic() - t0) * 1000)}
|
| 138 |
|
| 139 |
|
| 140 |
def register(app) -> None:
|