fix: include document content in RAG context + disable DM auto-rename

format_context() was only passing metadata (title, date, link) to the LLM,
so the bot could not answer content questions. Now passes full OCR text.
Also removes auto-rename for DMs (Element reuses single DM room per user pair).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Christian Gick
2026-02-16 13:43:04 +02:00
parent eacf4864d9
commit d392cda64d

47
bot.py
View File

@@ -99,24 +99,30 @@ class DocumentRAG:
def format_context(self, results: list[dict]) -> str: def format_context(self, results: list[dict]) -> str:
if not results: if not results:
return "" return ""
parts = ["The following documents were found in our document archive:"] parts = ["The following documents were found in our document archive:\n"]
for r in results: for i, r in enumerate(results, 1):
doc_id = r.get("id", "")
title = r.get("title", r.get("filename", "Untitled")) title = r.get("title", r.get("filename", "Untitled"))
filename = r.get("metadata", {}).get("original_filename", "") link = r.get("source_url") or r.get("metadata", {}).get("source_url", "")
category = r.get("category", "") category = r.get("category", "")
date = r.get("detected_date", "") date = r.get("detected_date", "")
link = r.get("source_url") or r.get("metadata", {}).get("source_url", "") content = r.get("content", "")
parts.append(f"- Title: {title}") summary = r.get("metadata", {}).get("summary", "")
if filename:
parts.append(f" Filename: {filename}") parts.append(f"--- Document {i}: {title} ---")
if category: if category:
parts.append(f" Category: {category}") parts.append(f"Category: {category}")
if date: if date:
parts.append(f" Date: {date}") parts.append(f"Date: {date}")
if link: if link:
parts.append(f" Link: {link}") parts.append(f"Link: {link}")
parts.append("\nUse this information to answer the user. Always include document links when referencing documents.") if summary:
parts.append(f"Summary: {summary}")
if content:
parts.append(f"Content:\n{content}")
parts.append("") # blank line between docs
parts.append("Use the document content above to answer the user's question. "
"Always include document links when referencing documents.")
return "\n".join(parts) return "\n".join(parts)
@@ -483,17 +489,12 @@ class Bot:
) )
reply = resp.choices[0].message.content reply = resp.choices[0].message.content
await self._send_text(room.room_id, reply) await self._send_text(room.room_id, reply)
# Auto-rename room after first AI response or after a conversation gap # Auto-rename: only for group rooms with explicit opt-in (not DMs)
# In DMs: always auto-rename. In groups: only if explicitly enabled. if room.room_id in self.auto_rename_rooms:
is_dm = room.member_count == 2 last_rename = self.renamed_rooms.get(room.room_id, 0)
last_rename = self.renamed_rooms.get(room.room_id, 0) gap_seconds = time.time() - last_rename if last_rename else float("inf")
gap_seconds = time.time() - last_rename if last_rename else float("inf") if gap_seconds > 300:
should_rename = ( await self._auto_rename_room(room, user_message, reply)
(is_dm or room.room_id in self.auto_rename_rooms)
and gap_seconds > 300 # Re-rename after 5min gap (new topic)
)
if should_rename:
await self._auto_rename_room(room, user_message, reply)
except Exception: except Exception:
logger.exception("LLM call failed") logger.exception("LLM call failed")
await self._send_text(room.room_id, "Sorry, I couldn't generate a response.") await self._send_text(room.room_id, "Sorry, I couldn't generate a response.")