From b925786867c9293e7c6c9f5b08927a9f4acf36fc Mon Sep 17 00:00:00 2001 From: Christian Gick Date: Thu, 5 Mar 2026 15:42:05 +0200 Subject: [PATCH] fix: Move doc_context after history to prevent history pattern override Two changes: 1. Reorder messages: doc_context now placed RIGHT BEFORE the user message (after chat history), so fresh search results override historical patterns where the bot repeatedly said "only one passport" 2. Strengthen doc_context instructions: explicitly tell LLM that fresh search results override chat history, and to list ALL matching documents Co-Authored-By: Claude Opus 4.6 --- bot.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index 918478d..bc85b7c 100644 --- a/bot.py +++ b/bot.py @@ -425,12 +425,15 @@ class DocumentRAG: parts.append("") # blank line between docs parts.append("IMPORTANT INSTRUCTIONS FOR DOCUMENT RESPONSES:\n" - "1. Answer the user's question using the document content above.\n" - "2. You MUST include a source link for EVERY document you reference.\n" - "3. Format links as markdown: [Document Title](url)\n" - "4. Place the link right after mentioning or quoting the document.\n" - "5. If a document has no link, skip the link but still reference the title.\n" - "6. Never show raw URLs without markdown formatting.") + "1. Answer the user's question using ALL the document content above.\n" + "2. These are FRESH search results — they override anything from chat history.\n" + " If previous messages said 'only one passport' but documents show more, trust the documents.\n" + "3. You MUST include a source link for EVERY document you reference.\n" + "4. Format links as markdown: [Document Title](url)\n" + "5. Place the link right after mentioning or quoting the document.\n" + "6. If a document has no link, skip the link but still reference the title.\n" + "7. Never show raw URLs without markdown formatting.\n" + "8. List ALL matching documents, not just the first one.") return "\n".join(parts) @@ -2106,16 +2109,17 @@ class Bot: ) # Build conversation context + # Order: system → memory → chunks → history → doc_context (closest to user message wins) messages = [{"role": "system", "content": SYSTEM_PROMPT}] if memory_context: messages.append({"role": "system", "content": memory_context}) if chunk_context: messages.append({"role": "system", "content": chunk_context}) - if doc_context: - messages.append({"role": "system", "content": doc_context}) if room_doc_context: messages.append({"role": "system", "content": room_doc_context}) messages.extend(history) + if doc_context: + messages.append({"role": "system", "content": doc_context}) # Add current user message (multimodal if image provided) if image_data: