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:
35
bot.py
35
bot.py
@@ -99,24 +99,30 @@ class DocumentRAG:
|
||||
def format_context(self, results: list[dict]) -> str:
|
||||
if not results:
|
||||
return ""
|
||||
parts = ["The following documents were found in our document archive:"]
|
||||
for r in results:
|
||||
doc_id = r.get("id", "")
|
||||
parts = ["The following documents were found in our document archive:\n"]
|
||||
for i, r in enumerate(results, 1):
|
||||
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", "")
|
||||
date = r.get("detected_date", "")
|
||||
link = r.get("source_url") or r.get("metadata", {}).get("source_url", "")
|
||||
parts.append(f"- Title: {title}")
|
||||
if filename:
|
||||
parts.append(f" Filename: {filename}")
|
||||
content = r.get("content", "")
|
||||
summary = r.get("metadata", {}).get("summary", "")
|
||||
|
||||
parts.append(f"--- Document {i}: {title} ---")
|
||||
if category:
|
||||
parts.append(f"Category: {category}")
|
||||
if date:
|
||||
parts.append(f"Date: {date}")
|
||||
if 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)
|
||||
|
||||
|
||||
@@ -483,16 +489,11 @@ class Bot:
|
||||
)
|
||||
reply = resp.choices[0].message.content
|
||||
await self._send_text(room.room_id, reply)
|
||||
# Auto-rename room after first AI response or after a conversation gap
|
||||
# In DMs: always auto-rename. In groups: only if explicitly enabled.
|
||||
is_dm = room.member_count == 2
|
||||
# Auto-rename: only for group rooms with explicit opt-in (not DMs)
|
||||
if room.room_id in self.auto_rename_rooms:
|
||||
last_rename = self.renamed_rooms.get(room.room_id, 0)
|
||||
gap_seconds = time.time() - last_rename if last_rename else float("inf")
|
||||
should_rename = (
|
||||
(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:
|
||||
if gap_seconds > 300:
|
||||
await self._auto_rename_room(room, user_message, reply)
|
||||
except Exception:
|
||||
logger.exception("LLM call failed")
|
||||
|
||||
Reference in New Issue
Block a user