Update rag/engine.py
Browse files- rag/engine.py +12 -0
rag/engine.py
CHANGED
|
@@ -117,6 +117,12 @@ async def agent_stream(message: str, machine_id: str, history: list[dict]):
|
|
| 117 |
except Exception as e:
|
| 118 |
print(f"Error in context expansion: {e}")
|
| 119 |
expanded_chunks = top_reranked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# Yield retrieved chunks metadata for frontend citation panel (use expanded_chunks so all cited pages are present)
|
| 122 |
retrieved_event_data = []
|
|
@@ -280,6 +286,12 @@ async def agent_chat(message: str, machine_id: str, history: list[dict]) -> dict
|
|
| 280 |
except Exception as e:
|
| 281 |
print(f"[RAG Diagnostics] ERROR in context expansion: {e}", flush=True)
|
| 282 |
expanded_chunks = top_reranked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
# Format sources and searches
|
| 285 |
sources = []
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
print(f"Error in context expansion: {e}")
|
| 119 |
expanded_chunks = top_reranked
|
| 120 |
+
|
| 121 |
+
# Lock context to the top document to prevent cross-document answer mixing
|
| 122 |
+
top_filename = top_reranked[0]["filename"] if top_reranked else None
|
| 123 |
+
if top_filename:
|
| 124 |
+
expanded_chunks = [c for c in expanded_chunks if c["filename"] == top_filename]
|
| 125 |
+
print(f"RAG v2: Context locked to '{top_filename}' ({len(expanded_chunks)} chunks)", flush=True)
|
| 126 |
|
| 127 |
# Yield retrieved chunks metadata for frontend citation panel (use expanded_chunks so all cited pages are present)
|
| 128 |
retrieved_event_data = []
|
|
|
|
| 286 |
except Exception as e:
|
| 287 |
print(f"[RAG Diagnostics] ERROR in context expansion: {e}", flush=True)
|
| 288 |
expanded_chunks = top_reranked
|
| 289 |
+
|
| 290 |
+
# Lock context to the top document to prevent cross-document answer mixing
|
| 291 |
+
top_filename = top_reranked[0]["filename"] if top_reranked else None
|
| 292 |
+
if top_filename:
|
| 293 |
+
expanded_chunks = [c for c in expanded_chunks if c["filename"] == top_filename]
|
| 294 |
+
print(f"[RAG Diagnostics] Context locked to '{top_filename}' ({len(expanded_chunks)} chunks).", flush=True)
|
| 295 |
|
| 296 |
# Format sources and searches
|
| 297 |
sources = []
|