Repository navigation
fix(memory): score Chinese recall on word boundaries - #4850
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4846
Why
MEMORY RECALL 的词法召回按字符处理中文:
tokenize()逐字切分,bigrams()只配相邻双字。如 #4846 所分析,「连接池」这类三字术语从不作为词级单元参与匹配,而「池配」「的默」这类跨词边界的碎片反而计入得分;共享常见单字的无关记忆也可能过线注入上下文。记忆召回是自动注入场景,弱相关条目的代价比普通检索更高。Root cause
internal/application/service/memory/lexical.go的scoreItems()只有两级特征:单字(权重 1)与相邻字符对(权重 2)。字符对直接取自相邻单字,没有任何词边界信息,也无词级单元。What changed
复用项目已有的分词能力
types.Jieba(faq_snippet、query_expansion、qdrant retriever 均已使用),为评分增加词级特征,字符特征保留为 fallback:hanSegments():把每段中文按 jieba 词边界切成多字词;分不出多字词的整段原样保留(未登录词的字符对 fallback 不丢);单字词(的了是…)丢弃——unigram 已覆盖wordPairs():字符对只在词内部产生,不再跨词拼接(「连接池配置」不会再产出「池配」)minRecallScore(0.15)、重要性平铺(0.01×Importance)、tokenize()本身(consolidate 的去重路径行为不变)Side effect analysis
selectRecallItems/lexicalRanking签名不变,search.go与recall_trace.go无需改动Validation
在 golang:1.26-bookworm(与 CI 构建镜像同版本)容器中:
go test -count=1 ./internal/application/service/memory/→ ok,8 个既有用例 + 6 个新用例全部通过 ✓TestWordBoundariesFilterSharedCharacters在改动前的lexical.go上运行确实失败、改动后通过(证明测试守住了修复)✓go build ./...→ OK ✓go vet ./internal/application/service/memory/→ clean ✓gofmt -l→ clean;golangci-lint v2.12.2--new-from-merge-base=upstream/main→ 0 issues ✓新用例覆盖 #4846 提到的全部场景:专业术语(连接池)、共享单字但语义无关(改名 vs 改选/名人堂)、中文标点边界(「改名,备案」不产出「名备」类碎片)、中英混合(K8s 集群)、未登录词(自造词「星澜」)。
AI assistance
Tool(s) used: ZCode (GLM)
How: AI 完成 issue 分析、方案设计、实现与回归测试;所有测试均在本地容器实际运行验证(含改动前失败对照)。我逐行审阅并核实了上述每一条行为声明。