Hausmeister: Fix History-Poisoning + Message-Logging-Reihenfolge + Einzelmessage-Fallback
This commit is contained in:
parent
339a087ac4
commit
5eea29f284
3 changed files with 22 additions and 11 deletions
|
|
@ -391,15 +391,14 @@ def ask_with_tools(question: str, tool_handlers: dict, session_id: str = None) -
|
||||||
if session_id:
|
if session_id:
|
||||||
try:
|
try:
|
||||||
import memory_client
|
import memory_client
|
||||||
history = memory_client.get_session_messages(session_id, limit=20)
|
history = memory_client.get_session_messages(session_id, limit=10)
|
||||||
for msg in history:
|
for msg in history:
|
||||||
if msg.get("role") in ("user", "assistant") and msg.get("content"):
|
if msg.get("role") in ("user", "assistant") and msg.get("content"):
|
||||||
messages.append({"role": msg["role"], "content": msg["content"]})
|
messages.append({"role": msg["role"], "content": msg["content"]})
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not any(m.get("content") == question for m in messages):
|
messages.append({"role": "user", "content": question})
|
||||||
messages.append({"role": "user", "content": question})
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for _round in range(MAX_TOOL_ROUNDS):
|
for _round in range(MAX_TOOL_ROUNDS):
|
||||||
|
|
|
||||||
|
|
@ -212,25 +212,38 @@ def get_session_summary(session_id: str, limit: int = 20, topic: str = None) ->
|
||||||
return "Keine Themen in dieser Session."
|
return "Keine Themen in dieser Session."
|
||||||
|
|
||||||
if topic:
|
if topic:
|
||||||
matching = []
|
matching_pairs = []
|
||||||
|
matching_singles = []
|
||||||
other_topics = []
|
other_topics = []
|
||||||
for q, a in exchanges:
|
for q, a in exchanges:
|
||||||
combined = q + " " + (a or "")
|
combined = q + " " + (a or "")
|
||||||
if _topic_matches(combined, topic):
|
if _topic_matches(combined, topic):
|
||||||
matching.append((q, a))
|
matching_pairs.append((q, a))
|
||||||
else:
|
else:
|
||||||
other_topics.append(q[:80])
|
other_topics.append(q[:80])
|
||||||
|
|
||||||
|
# Fallback: einzelne Messages pruefen (falls Pairing unvollstaendig)
|
||||||
|
if not matching_pairs:
|
||||||
|
for msg in messages:
|
||||||
|
content = (msg.get("content") or "").strip()
|
||||||
|
if content and _topic_matches(content, topic):
|
||||||
|
role = msg.get("role", "?")
|
||||||
|
matching_singles.append((role, content[:200]))
|
||||||
|
|
||||||
lines = []
|
lines = []
|
||||||
if matching:
|
if matching_pairs:
|
||||||
lines.append("Zum Thema '" + topic + "' (" + str(len(matching)) + " Treffer):")
|
lines.append("Zum Thema '" + topic + "' (" + str(len(matching_pairs)) + " Treffer):")
|
||||||
for i, (q, a) in enumerate(matching, 1):
|
for i, (q, a) in enumerate(matching_pairs, 1):
|
||||||
line = str(i) + ". Frage: " + q
|
line = str(i) + ". Frage: " + q
|
||||||
if a:
|
if a:
|
||||||
line += "\n Antwort: " + a
|
line += "\n Antwort: " + a
|
||||||
lines.append(line)
|
lines.append(line)
|
||||||
|
elif matching_singles:
|
||||||
|
lines.append("Zum Thema '" + topic + "' (" + str(len(matching_singles)) + " relevante Messages):")
|
||||||
|
for role, content in matching_singles:
|
||||||
|
lines.append(" [" + role + "] " + content)
|
||||||
else:
|
else:
|
||||||
lines.append("Zum Thema '" + topic + "' wurde in dieser Session nichts direkt besprochen.")
|
lines.append("Zum Thema '" + topic + "' wurde in dieser Session nichts besprochen.")
|
||||||
|
|
||||||
if other_topics:
|
if other_topics:
|
||||||
lines.append("\nSonstige Themen der Session: " + ", ".join(other_topics))
|
lines.append("\nSonstige Themen der Session: " + ", ".join(other_topics))
|
||||||
|
|
|
||||||
|
|
@ -308,14 +308,13 @@ async def handle_message(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
||||||
|
|
||||||
channel_key = str(update.effective_chat.id)
|
channel_key = str(update.effective_chat.id)
|
||||||
session_id = memory_client.get_or_create_session(channel_key, source="telegram")
|
session_id = memory_client.get_or_create_session(channel_key, source="telegram")
|
||||||
if session_id:
|
|
||||||
memory_client.log_message(session_id, "user", text)
|
|
||||||
|
|
||||||
await update.message.reply_text("🤔 Denke nach...")
|
await update.message.reply_text("🤔 Denke nach...")
|
||||||
try:
|
try:
|
||||||
handlers = context.get_tool_handlers(session_id=session_id)
|
handlers = context.get_tool_handlers(session_id=session_id)
|
||||||
answer = llm.ask_with_tools(text, handlers, session_id=session_id)
|
answer = llm.ask_with_tools(text, handlers, session_id=session_id)
|
||||||
if session_id:
|
if session_id:
|
||||||
|
memory_client.log_message(session_id, "user", text)
|
||||||
memory_client.log_message(session_id, "assistant", answer)
|
memory_client.log_message(session_id, "assistant", answer)
|
||||||
await update.message.reply_text(answer[:4000], reply_markup=KEYBOARD)
|
await update.message.reply_text(answer[:4000], reply_markup=KEYBOARD)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue