fix: connect Peg to local AI runtime
This commit is contained in:
+15
-1
@@ -48,7 +48,8 @@ async def chat_with_peg(data: CoachChatIn, cfg: Settings) -> CoachChatOut:
|
||||
response = await client.post(
|
||||
f"{cfg.ai_base_url.rstrip('/')}/chat/completions",
|
||||
headers=headers,
|
||||
json={"model": cfg.ai_model, "messages": messages, "temperature": 0.4, "max_tokens": 350},
|
||||
# Keep answers useful but short so a local CPU model responds reliably.
|
||||
json={"model": cfg.ai_model, "messages": messages, "temperature": 0.4, "max_tokens": 160},
|
||||
)
|
||||
response.raise_for_status()
|
||||
content = response.json()["choices"][0]["message"]["content"].strip()
|
||||
@@ -60,3 +61,16 @@ async def chat_with_peg(data: CoachChatIn, cfg: Settings) -> CoachChatOut:
|
||||
return CoachChatOut(message=content, source="model")
|
||||
except (httpx.HTTPError, KeyError, IndexError, TypeError, ValueError):
|
||||
return CoachChatOut(message="Je suis momentanément indisponible. En attendant, choisis une petite action sans risque : boire un verre d’eau, marcher quelques minutes si tu te sens bien, ou noter ton prochain repas. Pour une question médicale, contacte un professionnel de santé.", source="fallback")
|
||||
|
||||
|
||||
async def peg_provider_health(cfg: Settings) -> dict[str, str | bool]:
|
||||
"""Check the configured AI provider without leaking credentials or prompts."""
|
||||
base_url = cfg.ai_base_url.rstrip("/")
|
||||
headers = {"Authorization": f"Bearer {cfg.ai_api_key}"} if cfg.ai_api_key else {}
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=min(cfg.ai_timeout_seconds, 5.0)) as client:
|
||||
response = await client.get(f"{base_url}/models", headers=headers)
|
||||
response.raise_for_status()
|
||||
return {"status": "ok", "reachable": True, "model": cfg.ai_model}
|
||||
except httpx.HTTPError:
|
||||
return {"status": "unavailable", "reachable": False, "model": cfg.ai_model}
|
||||
|
||||
Reference in New Issue
Block a user