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(knowledge): strip think blocks from generated questions and summaries (#4691)

stanley24e409bea1c8
diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go
index e0217a17d..b675cdfc4 100644
--- a/internal/application/service/knowledge_process.go
+++ b/internal/application/service/knowledge_process.go
@@ -12,6 +12,7 @@ import (
"strings"
"time"
+ "github.com/Tencent/WeKnora/internal/agent/tools"
"github.com/Tencent/WeKnora/internal/application/access"
"github.com/Tencent/WeKnora/internal/application/repository"
chatpipeline "github.com/Tencent/WeKnora/internal/application/service/chat_pipeline"
@@ -1101,7 +1102,7 @@ func validateSummaryOutput(response *types.ChatResponse) (string, error) {
if response == nil {
return "", errEmptySummaryOutput
}
- content := strings.TrimSpace(response.Content)
+ content := strings.TrimSpace(tools.StripThinkBlocks(response.Content))
if content == "" {
return "", errEmptySummaryOutput
}
@@ -2632,8 +2633,14 @@ func (s *knowledgeService) generateQuestionsWithContext(ctx context.Context,
return nil, fmt.Errorf("failed to generate questions: %w", err)
}
- // Parse response
- lines := strings.Split(response.Content, "\n")
+ return parseGeneratedQuestions(response.Content, questionCount), nil
+}
+
+// parseGeneratedQuestions drops inline <think> reasoning first: servers that
+// ignore Thinking=false still emit it in content, and each reasoning line
+// would otherwise be indexed as a question.
+func parseGeneratedQuestions(content string, questionCount int) []string {
+ lines := strings.Split(tools.StripThinkBlocks(content), "\n")
questions := make([]string, 0, questionCount)
for _, line := range lines {
line = strings.TrimSpace(line)
@@ -2649,8 +2656,7 @@ func (s *knowledgeService) generateQuestionsWithContext(ctx context.Context,
}
}
}
-
- return questions, nil
+ return questions
}
// RegenerateChunkQuestions synchronously refreshes Doc2Query-style auxiliary
diff --git a/internal/application/service/knowledge_process_questions_test.go b/internal/application/service/knowledge_process_questions_test.go
new file mode 100644
index 000000000..c00492d6d
--- /dev/null
+++ b/internal/application/service/knowledge_process_questions_test.go
@@ -0,0 +1,50 @@
+package service
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestParseGeneratedQuestions(t *testing.T) {
+ tests := []struct {
+ name string
+ content string
+ count int
+ want []string
+ }{
+ {
+ name: "numbered list",
+ content: "1. What is WeKnora?\n2) How are chunks indexed?\n- ok\n",
+ count: 3,
+ want: []string{"What is WeKnora?", "How are chunks indexed?"},
+ },
+ {
+ name: "limited to question count",
+ content: "First question?\nSecond question?\nThird question?",
+ count: 2,
+ want: []string{"First question?", "Second question?"},
+ },
+ {
+ name: "inline think block is not parsed as questions",
+ content: "<think>\nThe chunk talks about indexing.\nI should ask about it.\n</think>\n" +
+ "What is WeKnora?\nHow are chunks indexed?",
+ count: 3,
+ want: []string{"What is WeKnora?", "How are chunks indexed?"},
+ },
+ {
+ name: "unterminated think block yields nothing",
+ content: "<think>\nThe chunk talks about indexing.\nI should ask about it.",
+ count: 3,
+ want: []string{},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := parseGeneratedQuestions(tt.content, tt.count)
+ if !reflect.DeepEqual(got, tt.want) {
+ t.Fatalf("questions = %q, want %q", got, tt.want)
+ }
+ })
+ }
+}
diff --git a/internal/application/service/knowledge_summary_test.go b/internal/application/service/knowledge_summary_test.go
index 2eca03bf2..4805a98e3 100644
--- a/internal/application/service/knowledge_summary_test.go
+++ b/internal/application/service/knowledge_summary_test.go
@@ -130,6 +130,16 @@ func TestValidateSummaryOutput(t *testing.T) {
response: &types.ChatResponse{Content: " useful summary \n"},
want: "useful summary",
},
+ {
+ name: "think-only response rejected",
+ response: &types.ChatResponse{Content: "<think>\nreasoning\n</think>\n"},
+ wantError: true,
+ },
+ {
+ name: "inline think block is stripped",
+ response: &types.ChatResponse{Content: "<think>\nreasoning\n</think>\nuseful summary"},
+ want: "useful summary",
+ },
{
name: "a normal stop is accepted",
response: &types.ChatResponse{Content: "useful summary", FinishReason: "stop"},