Files
qinling0210 c5ff2bced5 Refine agentic RAG phase logging (#18228)
### Summary

Refine agentic RAG phase logging

example:
```
2026-08-13 17:25:06,733 INFO     2434554 [Agentic RAG] LLM usage by phase:                                                             phase            llm_calls prompt_tok   output_tok  total_tok    time(s)
  formalize              1        389          151        540        1.7
  route                  1        287          234        521        2.7
  planner                1      11857          832      12689        6.8
  orchestrator round 1       0          0            0          0      171.5
    claim_research (2)       8      82519        23146     105665      131.0
    sufficiency            2      10122         1709      11831       15.0
    grounded               1       5846         3232       9078       24.7
  finalize               2       9945         2228      12173       19.9
  total: 16 LLM calls, 152497 tokens
```
2026-08-13 20:15:24 +08:00

52 lines
1.6 KiB
Python

"""Low mode: direct single-pass search."""
import logging
from rag.advanced_rag.harness.stats import in_phase
from rag.advanced_rag.harness.tools.search import hybrid_search
_LOG = logging.getLogger(__name__)
@in_phase("direct")
async def direct_search(state: dict, tools) -> dict:
"""Single hybrid search → merge into kbinfos."""
question = state.get("question", "")
keywords = state.get("keywords", "")
_LOG.info('[Direct search] Looking up the knowledge base for: "%s" (keywords: %s)', question, keywords)
result = await hybrid_search(tools, query=question, keywords=keywords, use_compiled=True)
_merge_kbinfos(tools, result)
if not _has_chunks(tools):
_LOG.info("[Direct search] Found no matching passages.")
return {"empty_result": True, "kbinfos": tools.kbinfos}
return {"kbinfos": tools.kbinfos}
def _merge_kbinfos(tools, result: dict):
if not result or not result.get("chunks"):
return
seen = {_chunk_key(c) for c in tools.kbinfos.get("chunks", [])}
for c in result.get("chunks", []):
k = _chunk_key(c)
if k in seen:
continue
seen.add(k)
tools.kbinfos.setdefault("chunks", []).append(c)
dseen = {d.get("doc_id") for d in tools.kbinfos.get("doc_aggs", [])}
for d in result.get("doc_aggs", []):
if d.get("doc_id") in dseen:
continue
dseen.add(d.get("doc_id"))
tools.kbinfos.setdefault("doc_aggs", []).append(d)
def _chunk_key(ck: dict) -> str:
return ck.get("chunk_id") or ck.get("id") or str(id(ck))
def _has_chunks(tools) -> bool:
return bool(tools.kbinfos.get("chunks"))