Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(suggestion): avoid echoing the current query (#2582)
HJX|翻斗花园二蛋2f2bcc7aa7e8
diff --git a/internal/application/service/message_suggestion.go b/internal/application/service/message_suggestion.goindex 85e15d46f..6b66d51a2 100644--- a/internal/application/service/message_suggestion.go+++ b/internal/application/service/message_suggestion.go@@ -379,6 +379,11 @@ func (s *messageSuggestionService) generateWithModel(return nil, types.TokenUsage{}, err}items, err := parseGeneratedSuggestions(response.Content, config.Categories, count)+ if err == nil {+ // The system prompt already asks the model not to repeat prior user+ // questions, but that is guidance the model does not always honour.+ items = filterSuggestionItemsAgainstQuery(items, generationContext.CurrentQuery)+ }return items, response.Usage, err}@@ -469,7 +474,18 @@ func (s *messageSuggestionService) generateFromKnowledge(items := make(types.SuggestionItems, 0, len(candidates))for _, candidate := range candidates {text := strings.TrimSpace(candidate.Question)- if text == "" {+ // The knowledge path never reaches the model, so the "do not repeat+ // prior user questions" system-prompt rule cannot apply here. Worse,+ // rankKnowledgeSuggestions ranks candidates against a context that+ // starts with CurrentQuery and IsContentContained adds +1, so a+ // candidate identical to the question just asked is actively promoted+ // to the top of the knowledge pool.+ // Typical trigger: the user asks "介绍一下X" while X has an+ // entity/summary wiki page, which wikiSuggestionFromPage turns into+ // the very same "介绍一下X".+ // Skip via continue rather than filtering afterwards so that later+ // candidates can backfill and the configured count is preserved.+ if text == "" || suggestionMatchesQuery(text, generationContext.CurrentQuery) {continue}item := types.SuggestionItem{@@ -916,6 +932,36 @@ func mergeHybridSuggestionItems(model, knowledge types.SuggestionItems, limit inreturn result}+// filterSuggestionItemsAgainstQuery drops suggestions that are, after+// normalization, exactly the question the user just asked.+// Only exact matches are removed, deliberately: a similarity threshold would+// also kill legitimate follow-ups about the same entity, which is precisely+// what the feature exists for.+func filterSuggestionItemsAgainstQuery(+ items types.SuggestionItems,+ currentQuery string,+) types.SuggestionItems {+ if normalizeSuggestionText(currentQuery) == "" {+ return items+ }+ filtered := make(types.SuggestionItems, 0, len(items))+ for _, item := range items {+ if suggestionMatchesQuery(item.Text, currentQuery) {+ continue+ }+ filtered = append(filtered, item)+ }+ return filtered+}++// suggestionMatchesQuery reports whether a candidate is the current question.+// It reuses normalizeSuggestionText so punctuation, whitespace and case+// variants ("Tell me about Foo?" vs "tell me about foo") are caught too.+func suggestionMatchesQuery(value, currentQuery string) bool {+ queryKey := normalizeSuggestionText(currentQuery)+ return queryKey != "" && normalizeSuggestionText(value) == queryKey+}+func normalizeSuggestionText(value string) string {return strings.Map(func(r rune) rune {if unicode.IsSpace(r) || strings.ContainsRune("??!!,,.。::;;\"'", r) {diff --git a/internal/application/service/message_suggestion_test.go b/internal/application/service/message_suggestion_test.goindex 17cb27087..93ed261fa 100644--- a/internal/application/service/message_suggestion_test.go+++ b/internal/application/service/message_suggestion_test.go@@ -33,6 +33,78 @@ func TestParseGeneratedSuggestionsFiltersAndDeduplicates(t *testing.T) {}}+func TestFilterSuggestionItemsAgainstQueryDropsNormalizedEchoes(t *testing.T) {+ const currentQuery = "介绍一下手冲咖啡"+ items := types.SuggestionItems{+ {ID: "model-echo", Text: "介绍一下 手冲咖啡?", Source: "model"},+ {ID: "wiki-echo", Text: "介绍一下手冲咖啡。", Source: "wiki"},+ {ID: "keep", Text: "手冲咖啡适合用什么水温?", Source: "model"},+ }+ got := filterSuggestionItemsAgainstQuery(items, currentQuery)+ if len(got) != 1 || got[0].ID != "keep" {+ t.Fatalf("filterSuggestionItemsAgainstQuery() = %#v", got)+ }+}++func TestFilterSuggestionItemsAgainstQueryKeepsAllWhenQueryEmpty(t *testing.T) {+ items := types.SuggestionItems{+ {ID: "1", Text: "A?", Source: "model"},+ {ID: "2", Text: "B?", Source: "wiki"},+ }+ for _, query := range []string{"", " ", "??"} {+ got := filterSuggestionItemsAgainstQuery(items, query)+ if len(got) != 2 {+ t.Fatalf("query %q: len = %d, want 2", query, len(got))+ }+ }+}++func TestSuggestionMatchesQueryIgnoresPunctuationAndCase(t *testing.T) {+ cases := []struct {+ value, query string+ want bool+ }{+ {"Tell me about Foo?", "tell me about foo", true},+ {"介绍一下X", "介绍一下X?", true},+ {"介绍一下 X", "介绍一下X", true},+ {"什么是X?", "介绍一下X", false},+ {"介绍一下XY", "介绍一下X", false},+ {"介绍一下X", "", false},+ }+ for _, c := range cases {+ if got := suggestionMatchesQuery(c.value, c.query); got != c.want {+ t.Fatalf("suggestionMatchesQuery(%q, %q) = %v, want %v", c.value, c.query, got, c.want)+ }+ }+}++// Removing the echo must not collapse the hybrid layout: the knowledge slot+// should be filled by the next knowledge candidate, not stolen by the model.+func TestMergeHybridKeepsLayoutAfterEchoRemoved(t *testing.T) {+ const currentQuery = "介绍一下手冲咖啡"+ model := types.SuggestionItems{+ {ID: "m1", Text: "手冲咖啡适合用什么水温?", Source: "model"},+ {ID: "m2", Text: "如何选择咖啡豆?", Source: "model"},+ }+ knowledge := filterSuggestionItemsAgainstQuery(types.SuggestionItems{+ {ID: "k-echo", Text: currentQuery, Source: "wiki"},+ {ID: "k2", Text: "咖啡豆应该如何保存?", Source: "wiki"},+ }, currentQuery)++ got := mergeHybridSuggestionItems(model, knowledge, 3)+ if len(got) != 3 {+ t.Fatalf("len = %d, want 3: %#v", len(got), got)+ }+ if got[0].ID != "m1" || got[1].ID != "m2" || got[2].ID != "k2" {+ t.Fatalf("mergeHybridSuggestionItems() = %#v", got)+ }+ for _, item := range got {+ if item.Text == currentQuery {+ t.Fatalf("follow-up still echoes the current question: %#v", got)+ }+ }+}+func TestMergeSuggestionItemsPreservesPriorityAndLimit(t *testing.T) {primary := types.SuggestionItems{{ID: "1", Text: "A?", Source: "model"}}fallback := types.SuggestionItems{