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 <noreply@anthropic.com>
This commit is contained in:
Christian Gick
2026-03-05 15:42:05 +02:00
parent aa175b8fb9
commit b925786867

20
bot.py
View File

@@ -425,12 +425,15 @@ class DocumentRAG:
parts.append("") # blank line between docs parts.append("") # blank line between docs
parts.append("IMPORTANT INSTRUCTIONS FOR DOCUMENT RESPONSES:\n" parts.append("IMPORTANT INSTRUCTIONS FOR DOCUMENT RESPONSES:\n"
"1. Answer the user's question using the document content above.\n" "1. Answer the user's question using ALL the document content above.\n"
"2. You MUST include a source link for EVERY document you reference.\n" "2. These are FRESH search results — they override anything from chat history.\n"
"3. Format links as markdown: [Document Title](url)\n" " If previous messages said 'only one passport' but documents show more, trust the documents.\n"
"4. Place the link right after mentioning or quoting the document.\n" "3. You MUST include a source link for EVERY document you reference.\n"
"5. If a document has no link, skip the link but still reference the title.\n" "4. Format links as markdown: [Document Title](url)\n"
"6. Never show raw URLs without markdown formatting.") "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) return "\n".join(parts)
@@ -2106,16 +2109,17 @@ class Bot:
) )
# Build conversation context # Build conversation context
# Order: system → memory → chunks → history → doc_context (closest to user message wins)
messages = [{"role": "system", "content": SYSTEM_PROMPT}] messages = [{"role": "system", "content": SYSTEM_PROMPT}]
if memory_context: if memory_context:
messages.append({"role": "system", "content": memory_context}) messages.append({"role": "system", "content": memory_context})
if chunk_context: if chunk_context:
messages.append({"role": "system", "content": chunk_context}) messages.append({"role": "system", "content": chunk_context})
if doc_context:
messages.append({"role": "system", "content": doc_context})
if room_doc_context: if room_doc_context:
messages.append({"role": "system", "content": room_doc_context}) messages.append({"role": "system", "content": room_doc_context})
messages.extend(history) messages.extend(history)
if doc_context:
messages.append({"role": "system", "content": doc_context})
# Add current user message (multimodal if image provided) # Add current user message (multimodal if image provided)
if image_data: if image_data: