Files
chat-proxy/backend/services/history_manager.py
T
2025-09-03 16:19:06 +00:00

54 lines
1.9 KiB
Python

# services/history_manager.py
import httpx
from config import settings
from services import redis_service
MAX_HISTORY_TURNS = 10 # ultimi turni da mantenere
SUMMARY_TRIGGER_TURNS = 20 # soglia per fare summarization
async def summarize_messages(messages):
"""
Usa LM Studio per riassumere i messaggi in forma compatta (condensed history).
"""
prompt = [
{"role": "system", "content": "Riassumi la seguente conversazione in forma di elenco puntato, mantenendo solo i fatti chiave e le informazioni rilevanti per continuare il dialogo."},
*messages
]
async with httpx.AsyncClient(timeout=settings.REQUEST_TIMEOUT) as client:
resp = await client.post(
settings.LM_STUDIO_URL,
json={"model": settings.MODEL_NAME, "messages": prompt}
)
resp.raise_for_status()
data = resp.json()
return data["choices"][0]["message"]["content"]
async def prepare_history(user_id: str, session_id: str):
"""
Recupera la history da Redis, applica windowing e summarization se necessario,
e restituisce la lista di messaggi da passare al modello.
"""
full_history = redis_service.get_chat(user_id, session_id, limit=1000)
if len(full_history) > SUMMARY_TRIGGER_TURNS:
old_messages = full_history[:-MAX_HISTORY_TURNS]
last_messages = full_history[-MAX_HISTORY_TURNS:]
summary_text = await summarize_messages(old_messages)
condensed_history = [{"role": "system", "content": f"Riassunto conversazione: {summary_text}"}]
condensed_history.extend(last_messages)
# Salva la nuova history condensata
redis_service.clear_chat(user_id, session_id)
for msg in condensed_history:
redis_service.save_chat(user_id, session_id, msg)
return condensed_history
else:
# Solo windowing
return full_history[-MAX_HISTORY_TURNS:]