mirror of
https://github.com/Alkatrazz24/Funk-lab.git
synced 2026-07-10 18:34:42 +02:00
feat(stt-server): contextes présélectionnables + sources live + contexte assemblé (visualiseur)
Asa n'est plus bloqué sur le seul contexte « doc cluster grounding-strict ». Le client choisit un contexte par requête ; le serveur change le system prompt ET injecte les données live du domaine, puis renvoie le contexte assemblé pour le visualiseur du HUD. - contexts.py : profils funk / ghostfolio / grafana / alerting / cluster (system prompt + sources) + assemble() (prompt final + structure de visualisation). - sources.py : fetchers live best-effort (Ghostfolio auth+details, Alertmanager alerts hors Watchdog, Prometheus cluster/metrics), env-config, dégradation propre. - brain.py : ask() reçoit le system prompt déjà assemblé (assemblage remonté). - app.py : /v1/ask accepte `context`, renvoie context_id + le contexte assemblé ; nouveau GET /v1/contexts ; RAG doc conditionné au profil. - config.py : URLs sources + STT_GHOSTFOLIO_TOKEN + STT_DEFAULT_CONTEXT. - deployment : env in-cluster (Prometheus/Alertmanager monitoring, Ghostfolio ai), jeton via secret optionnel stt-server-secrets/ghostfolio-token. - bump 0.3.1 → 0.4.0. Validé en local : assemblage (blocs+RAG+mémoire), parsing des sources (mock), endpoints /v1/contexts et /v1/ask (LLM mocké) — context_id, visualiseur, fallback contexte inconnu → funk. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
3b6099ffa0
commit
da0dc3f5f6
8 changed files with 388 additions and 27 deletions
|
|
@ -19,9 +19,11 @@ from stt_server import __version__
|
|||
from stt_server import brain
|
||||
from stt_server.brain import ask as brain_ask
|
||||
from stt_server.config import settings
|
||||
from stt_server.contexts import CONTEXTS, assemble, get_context
|
||||
from stt_server.knowledge import Knowledge
|
||||
from stt_server.longterm import LongTermMemory
|
||||
from stt_server.memory import SessionStore
|
||||
from stt_server.sources import fetch_blocks
|
||||
|
||||
log = logging.getLogger("stt_server")
|
||||
# uvicorn ne configure que ses propres loggers : on attache notre handler en INFO
|
||||
|
|
@ -54,13 +56,16 @@ app = FastAPI(title="STT-server", version=__version__, lifespan=lifespan)
|
|||
|
||||
class AskRequest(BaseModel):
|
||||
text: str
|
||||
model: str | None = None # alias LiteLLM ; défaut serveur si absent
|
||||
model: str | None = None # alias LiteLLM ; défaut serveur si absent
|
||||
session_id: str | None = None # mémoire court-terme : fil de conversation
|
||||
context: str | None = None # contexte présélectionné (funk/ghostfolio/…)
|
||||
|
||||
|
||||
class AskReply(BaseModel):
|
||||
reply: str
|
||||
model: str
|
||||
context_id: str # contexte effectivement utilisé
|
||||
context: dict | None = None # contexte assemblé (visualiseur HUD)
|
||||
|
||||
|
||||
@app.get("/healthz")
|
||||
|
|
@ -73,6 +78,18 @@ async def v1_models() -> dict:
|
|||
return {"default": settings.model, "available": settings.allowed_models}
|
||||
|
||||
|
||||
@app.get("/v1/contexts")
|
||||
async def v1_contexts() -> dict:
|
||||
"""Contextes présélectionnables (pour le sélecteur du HUD)."""
|
||||
return {
|
||||
"default": settings.default_context,
|
||||
"contexts": [
|
||||
{"id": c.id, "label": c.label, "icon": c.icon, "description": c.description}
|
||||
for c in CONTEXTS.values()
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@app.get("/v1/memory/health")
|
||||
async def v1_memory_health() -> dict:
|
||||
"""État de la mémoire long-terme (embeddings + Qdrant + collection), erreurs exposées."""
|
||||
|
|
@ -99,15 +116,25 @@ async def v1_ask(req: AskRequest, background: BackgroundTasks) -> AskReply:
|
|||
status_code=400,
|
||||
detail=f"modèle '{model}' non autorisé ; dispo : {settings.allowed_models}",
|
||||
)
|
||||
ctx = get_context(req.context)
|
||||
t0 = time.perf_counter()
|
||||
history = sessions.history(req.session_id) if req.session_id else None
|
||||
# recall : timeout serré, dégrade vite ; renvoie aussi le vecteur (réutilisé ci-dessous)
|
||||
memories, qvec = await longterm.recall(text) if longterm else ([], None)
|
||||
# RAG doc : on réutilise qvec (même embedder nomic) → ancre la réponse dans funk-docs
|
||||
docs = await knowledge.search(text, qvec) if knowledge else []
|
||||
# RAG doc : seulement si le contexte le demande (réutilise qvec, même embedder nomic)
|
||||
docs = []
|
||||
if "docs" in ctx.sources and knowledge:
|
||||
docs = await knowledge.search(text, qvec)
|
||||
# Sources live du contexte (Ghostfolio / Prometheus / Alertmanager) — best-effort
|
||||
blocks: list = []
|
||||
live = tuple(s for s in ctx.sources if s != "docs")
|
||||
if live:
|
||||
async with httpx.AsyncClient() as c:
|
||||
blocks = await fetch_blocks(c, live)
|
||||
t_recall = time.perf_counter()
|
||||
system, ctx_debug = assemble(ctx, blocks=blocks, docs=docs, memories=memories)
|
||||
try:
|
||||
reply = await brain_ask(text, model, history, memories, docs)
|
||||
reply = await brain_ask(text, system, model, history)
|
||||
except httpx.HTTPError as e:
|
||||
raise HTTPException(status_code=502, detail=f"upstream LiteLLM : {e}") from e
|
||||
t_gen = time.perf_counter()
|
||||
|
|
@ -118,15 +145,14 @@ async def v1_ask(req: AskRequest, background: BackgroundTasks) -> AskReply:
|
|||
if longterm:
|
||||
background.add_task(longterm.store, req.session_id or "anon", text, qvec)
|
||||
log.info(
|
||||
"ask model=%s recall=%.0fms gen=%.0fms total=%.0fms mem=%d docs=%d",
|
||||
model,
|
||||
"ask ctx=%s model=%s recall+src=%.0fms gen=%.0fms total=%.0fms mem=%d docs=%d blocks=%d",
|
||||
ctx.id, model,
|
||||
(t_recall - t0) * 1000,
|
||||
(t_gen - t_recall) * 1000,
|
||||
(t_gen - t0) * 1000,
|
||||
len(memories),
|
||||
len(docs),
|
||||
len(memories), len(docs), len(blocks),
|
||||
)
|
||||
return AskReply(reply=reply, model=model)
|
||||
return AskReply(reply=reply, model=model, context_id=ctx.id, context=ctx_debug)
|
||||
|
||||
|
||||
def run() -> None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue