stram response, like ais

This commit is contained in:
Stefano Zini
2026-11-05 08:50:19 +01:00
parent d3c6816181
commit 19de4654ff
6 changed files with 72 additions and 18 deletions
+9 -6
View File
@@ -19,7 +19,7 @@ SYS_M = ('Hai un argomento della riunione attuale ed estratti numerati di riunio
'"confermato": la riunione attuale conferma una decisione precedente; "cambiato": la contraddice o la modifica (riporta prima e dopo); "aperto": domanda aperta o azione precedente che la riunione attuale non ha chiuso; "contesto": informazione precedente pertinente non ripresa oggi. '
'Massimo 4 voci, solo estratti davvero pertinenti allo stesso argomento, altrimenti lista vuota. In italiano, forma impersonale, senza etichette "Speaker N". Non inventare.')
# Prompt for ask(): answer from numbered excerpts only, cite as [n].
SYS_ASK = 'Rispondi alla domanda usando SOLO gli estratti numerati di riunioni passate. Rispondi SOLO JSON: {"answer":str}. Cita gli estratti usati con il numero tra parentesi quadre, es. [2]. Se gli estratti non bastano, dillo. In italiano, conciso.'
SYS_ASK = 'Rispondi alla domanda usando SOLO gli estratti numerati di riunioni passate. Rispondi in testo semplice (niente JSON). Cita gli estratti usati con il numero tra parentesi quadre, es. [2]. Se gli estratti non bastano, dillo. In italiano, conciso.'
# Valid link statuses; anything else from the LLM is dropped.
STATUSES = {"confermato", "cambiato", "aperto", "contesto"}
@@ -152,15 +152,18 @@ def run(name, link=True):
# Q&A across all indexed meetings: top-K excerpts -> LLM answer citing [n].
def ask(q):
# Generator: first {"sources": [...]}, then {"t": text delta} events as the model writes.
def ask_stream(q):
# Cosine search over the normalized matrix; keep the best K_ASK excerpts.
rows, M = _rows(lambda r: True)
if not rows: return {"answer": "Nessuna riunione indicizzata.", "sources": []}
if not rows: yield {"sources": []}; yield {"t": "Nessuna riunione indicizzata."}; return
top = np.argsort(-(M @ embed([q])[0]))[:K_ASK]
rs = [rows[j] for j in top]
r = report.chat(SYS_ASK, f"DOMANDA: {q}\n\nESTRATTI:\n" + "\n".join(_prev_line(n, x) for n, x in enumerate(rs, 1)), 1500) or {}
return {"answer": str(r.get("answer") or "Risposta non disponibile."),
"sources": [{"n": n, **{k: x[k] for k in ("name", "date", "meeting", "topic", "anchor", "kind", "text", "t")}} for n, x in enumerate(rs, 1)]}
yield {"sources": [{"n": n, **{k: x[k] for k in ("name", "date", "meeting", "topic", "anchor", "kind", "text", "t")}} for n, x in enumerate(rs, 1)]}
body = {"temperature": 0.2, "max_tokens": 1500, "chat_template_kwargs": {"enable_thinking": False},
"messages": [{"role": "system", "content": SYS_ASK},
{"role": "user", "content": f"DOMANDA: {q}\n\nESTRATTI:\n" + "\n".join(_prev_line(n, x) for n, x in enumerate(rs, 1))}]}
for d in llama.stream(body, 1800): yield {"t": d}
if __name__ == "__main__":