David Prince commited on
Commit
9b613a9
·
1 Parent(s): 89ed5d6

fix: agent/run direct provider chain; enable Supabase PostgreSQL via DATABASE_URL

Browse files
Files changed (2) hide show
  1. app_part1.py +5 -0
  2. app_routes_extension.py +20 -31
app_part1.py CHANGED
@@ -61,6 +61,11 @@ IN_MEMORY_BUILDS = {}
61
  IN_MEMORY_ASSETS = []
62
 
63
  async def init_db_pool() -> asyncpg.Pool:
 
 
 
 
 
64
  pool = await asyncpg.create_pool(
65
  host=POSTGRES_SERVER,
66
  port=int(POSTGRES_PORT),
 
61
  IN_MEMORY_ASSETS = []
62
 
63
  async def init_db_pool() -> asyncpg.Pool:
64
+ database_url = os.environ.get("DATABASE_URL", "").strip()
65
+ if database_url:
66
+ pool = await asyncpg.create_pool(dsn=database_url, min_size=1, max_size=5,
67
+ ssl="require" if os.environ.get("POSTGRES_SSL","").lower() in ("1","true","yes") else None)
68
+ return pool
69
  pool = await asyncpg.create_pool(
70
  host=POSTGRES_SERVER,
71
  port=int(POSTGRES_PORT),
app_routes_extension.py CHANGED
@@ -94,47 +94,36 @@ 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 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:
 
94
 
95
  @ext_router.post("/api/agent/run")
96
  async def agent_run(body: AgentRunRequest) -> dict[str, Any]:
97
+ """Direct Groq→Cerebras→OpenRouter fallback — no Databricks."""
98
+ import os, httpx, time as _t
99
+ t0 = _t.monotonic()
 
100
  messages = [{"role": "user", "content": body.message}]
 
101
  chain = []
102
  if os.environ.get("GROQ_API_KEY"):
103
  chain.append(("groq", "https://api.groq.com/openai/v1/chat/completions",
104
+ "llama-3.3-70b-versatile", os.environ["GROQ_API_KEY"]))
105
  if os.environ.get("CEREBRAS_API_KEY"):
106
  chain.append(("cerebras", "https://api.cerebras.ai/v1/chat/completions",
107
+ "llama3.1-70b", os.environ["CEREBRAS_API_KEY"]))
108
  if os.environ.get("OPENROUTER_API_KEY"):
109
  chain.append(("openrouter", "https://openrouter.ai/api/v1/chat/completions",
110
+ "meta-llama/llama-3.1-8b-instruct:free", os.environ["OPENROUTER_API_KEY"]))
111
+ last_err = "No providers configured"
 
 
112
  async with httpx.AsyncClient(timeout=30.0) as client:
113
+ for name, url, model, key in chain:
114
  try:
115
+ r = await client.post(url,
116
+ headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"},
117
+ json={"model": model, "messages": messages, "max_tokens": 1024})
118
+ if r.status_code == 200:
119
+ text = r.json()["choices"][0]["message"]["content"]
120
+ return {"response": text, "provider": name, "model": model,
121
+ "latency_ms": round((_t.monotonic()-t0)*1000)}
122
+ last_err = f"{name} {r.status_code}: {r.text[:150]}"
123
+ except Exception as e:
124
+ last_err = f"{name}: {e}"
125
+ return {"error": last_err, "latency_ms": round((_t.monotonic()-t0)*1000)}
126
+
 
 
 
 
 
 
 
127
 
128
 
129
  def register(app) -> None: