Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(rerank): preserve code and math passages

xiaowang60028cbe0ecf
diff --git a/internal/application/service/chat_pipeline/rerank.go b/internal/application/service/chat_pipeline/rerank.go
index 665069dee..659a0c659 100644
--- a/internal/application/service/chat_pipeline/rerank.go
+++ b/internal/application/service/chat_pipeline/rerank.go
@@ -559,10 +559,10 @@ var (
reMarkdownLink = regexp.MustCompile(`\[([^\]]+)\]\([^()\s]*(?:\([^)]*\)[^()\s]*)*\)`)
// reRawURL matches standalone http(s) URLs.
reRawURL = regexp.MustCompile(`https?://[^\s)\]>]+`)
- // reCodeBlock matches fenced code blocks (```...```).
- reCodeBlock = regexp.MustCompile("(?s)```(?:\\w*)\n?.*?```")
- // reLatexBlock matches block-level LaTeX ($$...$$).
- reLatexBlock = regexp.MustCompile(`(?s)\$\$.*?\$\$`)
+ // reCodeBlock captures the semantic body of fenced code blocks.
+ reCodeBlock = regexp.MustCompile("(?s)```[^\\r\\n]*\\r?\\n(.*?)\\r?\\n?```")
+ // reLatexBlock captures the semantic body of block-level LaTeX ($$...$$).
+ reLatexBlock = regexp.MustCompile(`(?s)\$\$(.*?)\$\$`)
// reTableSep matches table separator rows like |---|---|.
// Uses [ \t] instead of \s to avoid consuming newlines across rows.
reTableSep = regexp.MustCompile(`(?m)^[ \t]*\|[ \t:|-]+\|[ \t]*$`)
@@ -589,13 +589,13 @@ var (
// cleanPassageForRerank strips markdown/structural noise from text to produce
// a clean semantic passage for the rerank model. The cleaning is designed to
-// preserve all meaningful natural-language content while removing formatting
+// preserve all meaningful semantic content while removing formatting
// that would confuse text-similarity scoring.
func cleanPassageForRerank(text string) string {
- // 1. Remove code blocks (before other patterns to avoid partial matches)
- text = reCodeBlock.ReplaceAllString(text, "")
- // 2. Remove LaTeX block math
- text = reLatexBlock.ReplaceAllString(text, "")
+ // 1. Unwrap code blocks so code-only candidates remain rerankable.
+ text = reCodeBlock.ReplaceAllString(text, "$1")
+ // 2. Unwrap LaTeX blocks so formula-only candidates remain rerankable.
+ text = reLatexBlock.ReplaceAllString(text, "$1")
// 3. Remove HTML tags
text = reHTMLTag.ReplaceAllString(text, "")
// 3.5. Unwrap nested [![alt](img_url)](link_url) → ![alt](img_url)
diff --git a/internal/application/service/chat_pipeline/rerank_clean_test.go b/internal/application/service/chat_pipeline/rerank_clean_test.go
index 5ca0cc588..0fccdc151 100644
--- a/internal/application/service/chat_pipeline/rerank_clean_test.go
+++ b/internal/application/service/chat_pipeline/rerank_clean_test.go
@@ -29,6 +29,37 @@ func TestGetEnrichedPassageKeepsQuestionsFromEarlierRevision(t *testing.T) {
}
}
+func TestGetEnrichedPassageKeepsCodeAndMathCandidates(t *testing.T) {
+ tests := []struct {
+ name string
+ content string
+ want string
+ }{
+ {
+ name: "code only",
+ content: "```go\nfunc answer() int { return 42 }\n```",
+ want: "func answer() int { return 42 }",
+ },
+ {
+ name: "math only",
+ content: "$$\nE = mc^2\n$$",
+ want: "E = mc^2",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ passage := getEnrichedPassage(context.Background(), &types.SearchResult{Content: tt.content})
+ if strings.TrimSpace(passage) == "" {
+ t.Fatal("semantic-only candidate was removed from the rerank passage")
+ }
+ if !strings.Contains(passage, tt.want) {
+ t.Fatalf("rerank passage %q does not preserve %q", passage, tt.want)
+ }
+ })
+ }
+}
+
func TestCleanPassageForRerank(t *testing.T) {
tests := []struct {
name string
@@ -56,14 +87,14 @@ func TestCleanPassageForRerank(t *testing.T) {
expect: "访问 获取更多信息",
},
{
- name: "remove code blocks",
+ name: "unwrap code blocks",
input: "示例代码:\n```python\nprint('hello')\n```\n以上是示例",
- expect: "示例代码:\n\n以上是示例",
+ expect: "示例代码:\nprint('hello')\n以上是示例",
},
{
- name: "remove LaTeX blocks",
+ name: "unwrap LaTeX blocks",
input: "公式如下 $$E=mc^2$$ 其中E是能量",
- expect: "公式如下 其中E是能量",
+ expect: "公式如下 E=mc^2 其中E是能量",
},
{
name: "remove table separator rows and convert data rows",
@@ -114,7 +145,7 @@ func TestCleanPassageForRerank(t *testing.T) {
- 功能二
` + "```json\n{\"key\": \"value\"}\n```",
- expect: "产品介绍\n\n这是一个 重要的 产品。详见 产品页面。\n\n用户评价:非常好用\n\n功能一\n功能二",
+ expect: "产品介绍\n\n这是一个 重要的 产品。详见 产品页面。\n\n用户评价:非常好用\n\n功能一\n功能二\n\n{\"key\": \"value\"}",
},
{
name: "convert table data rows to plain text",