diff --git a/bot.py b/bot.py index f505d0e..0d45735 100644 --- a/bot.py +++ b/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}") + parts.append(f"Category: {category}") if date: - parts.append(f" Date: {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.") + parts.append(f"Link: {link}") + 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,17 +489,12 @@ 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 - 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: - await self._auto_rename_room(room, user_message, reply) + # 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") + if gap_seconds > 300: + await self._auto_rename_room(room, user_message, reply) except Exception: logger.exception("LLM call failed") await self._send_text(room.room_id, "Sorry, I couldn't generate a response.")