fix: connect Peg to local AI runtime
MetaBloom CI / app (push) Canceled after 0s
MetaBloom CI / api (push) Canceled after 0s

This commit is contained in:
AI Company
2026-07-21 22:06:38 +00:00
parent 02e24cbc92
commit 0c2430ad13
7 changed files with 50 additions and 8 deletions
+15 -1
View File
@@ -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 deau, 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}