WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

Ranking core (compositeScore / applyMMR) is implemented twice — chat pipeline vs agent knowledge_search — and the two copies have diverged

Open vicfei opened this issue 2 months ago • 3 comments

Affected Component

Backend — retrieval ranking shared by both Q&A surfaces:

  • internal/application/service/chat_pipeline/rerank.go (chat / knowledge-chat pipeline)
  • internal/agent/tools/knowledge_search.go (ReAct agent knowledge_search tool)

Problem

The ranking core — composite scoring (0.6*modelScore + 0.3*baseScore + 0.1*sourceWeight, clamped to [0,1]) and MMR selection with Jaccard token-set redundancy — exists as two independent implementations, and they have already drifted:

chat pipeline agent tool
compositeScore chat_pipeline/rerank.go:439 knowledge_search.go:1210
applyMMR chat_pipeline/rerank.go:458 knowledge_search.go:1249
position prior not applied applied (positionPrior ±0.05 from StartAt/EndAt)
MMR structure naive per-candidate redundancy scan, ParallelMap tokenization incremental maxRedundancy cache (comment asserts identical selection output)

The practical consequence of the drift: the same retrieved chunk can rank differently depending on whether the answer is produced through a chat session or an agent session, and every future ranking change (weights, normalization, source weighting) has to be made twice or silently diverge further.

Why consolidation is straightforward

  • Both copies already sit on the same shared primitives: the agent's tokenizeSimple (knowledge_search.go:1330) is a pure delegate to searchutil.TokenizeSimple, clampFloat delegates to searchutil.ClampFloat, and both MMRs use searchutil.Jaccard.
  • The agent's searchResultWithMeta (knowledge_search.go:110) embeds *types.SearchResult, so a shared implementation can operate on the common type.

Proposal

  1. Extract compositeScore / applyMMR into the existing shared package (e.g. searchutil or a small ranking helper package).
  2. Keep path-specific concerns (pipeline event emission vs logger, parallel tokenization) as parameters/options.
  3. The one semantic decision for maintainers: should the agent's position prior be unified into both paths or stay agent-only? Either way, making the choice once in shared code is the point.

Pure refactor with no behavior change per path (the agent MMR's own comment states its output matches the naive form, including tie-breaking). I'm happy to submit the PR if the approach sounds good.

WeKnora Version

upstream/main @ 647848f3 (line numbers above verified on this revision)

vicfei avatar Sep 07 '26 16:09 vicfei

The refactor PR implementing this proposal is up as #3150 — zero behavior change per path (the agent still multiplies in the position prior before its single clamp), with randomized differential tests pinning the incremental MMR selection against the naive form. The position-prior unification decision remains open for maintainers; with the core now in one place it's a one-line change either way.

vicfei avatar Sep 10 '26 02:09 vicfei

重做版已提交:#3533(对 #3381 后的新文件布局重锚,方案与首轮 #3150 一致)。

  • searchutil.CompositeScoreRaw(0.6/0.3/0.1+web_search 0.95,不 clamp——clamp 留在调用侧,因为 agent 要先乘位置先验)+ PositionPrior(两侧唯一行为差异,集中暴露)+ 泛型 ApplyMMR(增量形态,token sets 由调用方注入,返回选择+平均冗余供 chat 日志);
  • chat 保留并行分词与 pipeline 观测,agent 保留串行富化分词,三个一行委托与未用参数删除;
  • 位级对齐有测试钉死:两套旧公式作参考预言机的全矩阵 parity + 300 轮随机差分(选择序+平均冗余)+ agent 包既有朴素预言机测试改跑共享核心后原样通过;
  • λ、权重、日志零变更。感谢关闭 #3150 时给的指针。

vicfei avatar Sep 22 '26 02:09 vicfei

Fixed by #3675: rerank, composite scoring and MMR now live in one shared package (internal/reranking), used by both the chat pipeline and the agent knowledge_search tool. The duplicated copies are gone.

lyingbug avatar Sep 25 '26 06:09 lyingbug