ok
Browse files- src/rag/qa_engine.py +48 -1
- src/rag/retrieval_pipeline.py +23 -5
src/rag/qa_engine.py
CHANGED
|
@@ -59,8 +59,31 @@ def _safe_int(value: Any, default: int = 0) -> int:
|
|
| 59 |
|
| 60 |
|
| 61 |
def _doc_key_from_md(md: Dict[str, Any]) -> str:
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def _group_passages_for_llm(passages: List[Dict[str, Any]], max_units: int) -> List[Dict[str, Any]]:
|
| 66 |
grouped: "OrderedDict[str, Dict[str, Any]]" = OrderedDict()
|
|
@@ -210,3 +233,27 @@ def build_chat_response(
|
|
| 210 |
sources.append(src)
|
| 211 |
|
| 212 |
return ChatResponse(answer=answer, sources=sources)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
def _doc_key_from_md(md: Dict[str, Any]) -> str:
|
| 62 |
+
law_name = str(md.get("law_name") or "").strip().lower()
|
| 63 |
+
if law_name:
|
| 64 |
+
return law_name
|
| 65 |
|
| 66 |
+
document_id = str(md.get("document_id") or "").strip().lower()
|
| 67 |
+
if document_id:
|
| 68 |
+
return document_id
|
| 69 |
+
|
| 70 |
+
file_name = str(md.get("file_name") or md.get("filename") or "").strip().lower()
|
| 71 |
+
if file_name:
|
| 72 |
+
return file_name
|
| 73 |
+
|
| 74 |
+
chunk_id = str(md.get("chunk_id") or "").strip().lower()
|
| 75 |
+
if "::" in chunk_id:
|
| 76 |
+
return chunk_id.split("::")[0]
|
| 77 |
+
|
| 78 |
+
source_path = str(md.get("source_path") or md.get("file_path") or "").strip().lower()
|
| 79 |
+
if source_path:
|
| 80 |
+
return source_path
|
| 81 |
+
|
| 82 |
+
source = str(md.get("source") or "").strip().lower()
|
| 83 |
+
if source:
|
| 84 |
+
return source
|
| 85 |
+
|
| 86 |
+
return "unknown"
|
| 87 |
|
| 88 |
def _group_passages_for_llm(passages: List[Dict[str, Any]], max_units: int) -> List[Dict[str, Any]]:
|
| 89 |
grouped: "OrderedDict[str, Dict[str, Any]]" = OrderedDict()
|
|
|
|
| 233 |
sources.append(src)
|
| 234 |
|
| 235 |
return ChatResponse(answer=answer, sources=sources)
|
| 236 |
+
|
| 237 |
+
def answer_with_rag(
|
| 238 |
+
question: str,
|
| 239 |
+
retrieval_result: Dict[str, Any],
|
| 240 |
+
*,
|
| 241 |
+
response_mode: str | None = None,
|
| 242 |
+
) -> ChatResponse:
|
| 243 |
+
"""
|
| 244 |
+
Backward-compatible entrypoint used by the API layer.
|
| 245 |
+
|
| 246 |
+
The retrieval pipeline returns a dict that contains passages and optional
|
| 247 |
+
tuning parameters. This adapter extracts those fields and delegates to
|
| 248 |
+
``build_chat_response``.
|
| 249 |
+
"""
|
| 250 |
+
retrieval_result = retrieval_result or {}
|
| 251 |
+
passages = retrieval_result.get("passages", []) or []
|
| 252 |
+
|
| 253 |
+
return build_chat_response(
|
| 254 |
+
question=question,
|
| 255 |
+
passages=passages,
|
| 256 |
+
max_context_passages=retrieval_result.get("final_top_k"),
|
| 257 |
+
max_source_items=retrieval_result.get("final_top_k"),
|
| 258 |
+
response_mode=response_mode or retrieval_result.get("mode"),
|
| 259 |
+
)
|
src/rag/retrieval_pipeline.py
CHANGED
|
@@ -320,13 +320,31 @@ def _extract_node_id_from_payload(payload: Dict[str, Any]) -> Optional[str]:
|
|
| 320 |
|
| 321 |
|
| 322 |
def _candidate_doc_key(metadata: Dict[str, Any]) -> str:
|
| 323 |
-
source = str(metadata.get("source") or "").strip()
|
| 324 |
-
if source:
|
| 325 |
-
return source.lower()
|
| 326 |
law_name = str(metadata.get("law_name") or "").strip().lower()
|
| 327 |
-
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
|
|
|
| 330 |
|
| 331 |
def _build_runtime_retrieval_text(text: str, metadata: Dict[str, Any], payload: Optional[Dict[str, Any]] = None) -> str:
|
| 332 |
payload = payload or {}
|
|
|
|
| 320 |
|
| 321 |
|
| 322 |
def _candidate_doc_key(metadata: Dict[str, Any]) -> str:
|
|
|
|
|
|
|
|
|
|
| 323 |
law_name = str(metadata.get("law_name") or "").strip().lower()
|
| 324 |
+
if law_name:
|
| 325 |
+
return law_name
|
| 326 |
+
|
| 327 |
+
document_id = str(metadata.get("document_id") or "").strip().lower()
|
| 328 |
+
if document_id:
|
| 329 |
+
return document_id
|
| 330 |
+
|
| 331 |
+
file_name = str(metadata.get("file_name") or metadata.get("filename") or "").strip().lower()
|
| 332 |
+
if file_name:
|
| 333 |
+
return file_name
|
| 334 |
+
|
| 335 |
+
chunk_id = str(metadata.get("chunk_id") or "").strip().lower()
|
| 336 |
+
if "::" in chunk_id:
|
| 337 |
+
return chunk_id.split("::")[0]
|
| 338 |
+
|
| 339 |
+
source_path = str(metadata.get("source_path") or metadata.get("file_path") or "").strip().lower()
|
| 340 |
+
if source_path:
|
| 341 |
+
return source_path
|
| 342 |
+
|
| 343 |
+
source = str(metadata.get("source") or "").strip().lower()
|
| 344 |
+
if source:
|
| 345 |
+
return source
|
| 346 |
|
| 347 |
+
return "unknown"
|
| 348 |
|
| 349 |
def _build_runtime_retrieval_text(text: str, metadata: Dict[str, Any], payload: Optional[Dict[str, Any]] = None) -> str:
|
| 350 |
payload = payload or {}
|