RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(service): keep citations and reference for a no-answer reply (#20862)
Caobaining1ce6d660923fb
diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.goindex d9e89660f1..877ebd0c48 100644--- a/internal/service/chat_pipeline.go+++ b/internal/service/chat_pipeline.go@@ -460,10 +460,6 @@ func (s *ChatPipelineService) AsyncChat(// once before any retrieval path can return early. The agentic dispatch// above resolves the same value through quoteEnabled.quote := quoteEnabled(kwargs, promptConfig)- // The dialog's configured no-answer line ("空回复"). An answer that only- // reports it is decorated as if quoting were off, so it carries neither- // citation markers nor a document reference (decorateQuote).- emptyResponse, _ := promptConfig["empty_response"].(string)fieldMap, fmErr := s.kbDAO.GetFieldMap(ctx, dao.DB, kbIDStrings(kbs))if fmErr != nil {common.Warn("get_field_map failed; proceeding without field_map", zap.Error(fmErr))@@ -880,7 +876,7 @@ func (s *ChatPipelineService) AsyncChat(if harnessAnswer != "" {common.Info("harness produced final cited answer; short-circuiting",zap.Int("answer_chars", len(harnessAnswer)))- final := s.decorateHarnessAnswer(harnessAnswer, kbinfos, slotCites, citeChunkIDs, decorateQuote(quote, harnessAnswer, emptyResponse))+ final := s.decorateHarnessAnswer(harnessAnswer, kbinfos, slotCites, citeChunkIDs, quote)final.Final = trueout <- finalreturn@@ -1507,7 +1503,7 @@ func (s *ChatPipelineService) AsyncChat(visibleAnswer := s.extractVisibleAnswer(thinkState.fullText)// Pass nil for ttsModel — audio was already produced per-delta.- final := s.decorateAnswer(ctx, visibleAnswer, kbinfos, prompt, questions, usedTokenCount, timer, embModel, chat.VectorSimilarityWeight, decorateQuote(quote, visibleAnswer, emptyResponse), nil, langfuseTraceID, llmModelConfig, chat.TenantID, kbTenantIDStrings(kbs), len(knowledges) > 0)+ final := s.decorateAnswer(ctx, visibleAnswer, kbinfos, prompt, questions, usedTokenCount, timer, embModel, chat.VectorSimilarityWeight, quote, nil, langfuseTraceID, llmModelConfig, chat.TenantID, kbTenantIDStrings(kbs), len(knowledges) > 0)final.Final = truefinal.AudioBinary = niltimer.Exit(common.PhaseGenerateAnswer)@@ -1546,7 +1542,7 @@ func (s *ChatPipelineService) AsyncChat(common.Debug("User: " + userContent + "|Assistant: " + answer)// Synthesize TTS for the full answer (non-stream, one-shot).- final := s.decorateAnswer(ctx, answer, kbinfos, prompt, questions, usedTokenCount, timer, embModel, chat.VectorSimilarityWeight, decorateQuote(quote, answer, emptyResponse), ttsModel, langfuseTraceID, llmModelConfig, chat.TenantID, kbTenantIDStrings(kbs), len(knowledges) > 0)+ final := s.decorateAnswer(ctx, answer, kbinfos, prompt, questions, usedTokenCount, timer, embModel, chat.VectorSimilarityWeight, quote, ttsModel, langfuseTraceID, llmModelConfig, chat.TenantID, kbTenantIDStrings(kbs), len(knowledges) > 0)final.Final = truetimer.Exit(common.PhaseGenerateAnswer)out <- finaldiff --git a/internal/service/citation.go b/internal/service/citation.goindex 0174f245dc..0c4a26429b 100644--- a/internal/service/citation.go+++ b/internal/service/citation.go@@ -592,62 +592,6 @@ func rawCitationMarkers(answer string, limit int) []int {return out}-// notFoundPhrases are the shipped system-prompt lines that instruct the model to-// answer "no answer in the knowledge base" verbatim (web/src/locales/zh.ts,-// systemInitialValue / emptyResponsePlaceholder). They are the fallback signal-// when the dialog configures no empty_response of its own.-var notFoundPhrases = []string{- "知识库中未找到您要的答案",- "在知识库中未找到您要寻找的答案",-}--// reportsNoAnswer reports whether the answer only announces that the knowledge-// base holds no answer. Such an answer cites nothing, so it must not carry-// citation markers or a document reference.-//-// The dialog's configured empty_response is the primary signal; when it is unset-// the shipped not-found lines above are the fallback. Both sides are stripped of-// citation markers and whitespace before matching, so a marker the model injected-// mid-sentence ("因 [ID:3]此") does not hide the phrase.-func reportsNoAnswer(answer, emptyResponse string) bool {- flat := flattenForMatch(stripCitations(answer))- if flat == "" {- return false- }- if phrase := flattenForMatch(emptyResponse); phrase != "" && strings.Contains(flat, phrase) {- return true- }- for _, phrase := range notFoundPhrases {- if strings.Contains(flat, phrase) {- return true- }- }- return false-}--// decorateQuote reports whether the answer should be decorated with its citation-// markers and document reference. An answer that only reports the knowledge base-// holds no answer is decorated as if quoting were off — otherwise the markers and-// the document list present sources for a reply that cited nothing.-func decorateQuote(quote bool, answer, emptyResponse string) bool {- return quote && !reportsNoAnswer(answer, emptyResponse)-}--// flattenForMatch drops all whitespace so a phrase match survives line breaks and-// the spaces a citation marker leaves behind when it is stripped.-func flattenForMatch(text string) string {- var b strings.Builder- b.Grow(len(text))- for _, r := range text {- switch r {- case ' ', '\t', '\n', '\r', '\v', '\f', '\u00a0', '\u3000':- default:- b.WriteRune(r)- }- }- return b.String()-}-// markerNumber parses a citation marker's digits (Arabic-Indic digits accepted)// into a position. ok is false for an empty or non-numeric capture.func markerNumber(digits string) (int, bool) {diff --git a/internal/service/citation_test.go b/internal/service/citation_test.goindex 090d1243ba..916702a4f2 100644--- a/internal/service/citation_test.go+++ b/internal/service/citation_test.go@@ -568,73 +568,11 @@ func TestCitationStreamFilter(t *testing.T) {}}-// TestReportsNoAnswer covers the three signals: the dialog's configured-// empty_response, the shipped not-found line, and a grounded answer that must-// keep its citations.-func TestReportsNoAnswer(t *testing.T) {- cases := []struct {- name string- answer string- emptyResponse string- want bool- }{- {- name: "configured empty response",- answer: "未在资料库中找到相关内容。",- emptyResponse: "未在资料库中找到相关内容。",- want: true,- },- {- name: "shipped phrase",- answer: "知识库中未找到您要的答案!",- want: true,- },- {- name: "shipped phrase with marker injected mid-sentence",- answer: "知识库中未找到您要的答案!因 [ID:3]此无法统计。",- want: true,- },- {- name: "shipped phrase split by line breaks",- answer: "知识库中\n未找到您要的\n答案!",- want: true,- },- {- name: "grounded answer keeps citations",- answer: "Texas and California agreed in 1924 [ID:0].",- emptyResponse: "知识库中未找到您要的答案!",- want: false,- },- }- for _, c := range cases {- t.Run(c.name, func(t *testing.T) {- if got := reportsNoAnswer(c.answer, c.emptyResponse); got != c.want {- t.Fatalf("reportsNoAnswer(%q, %q) = %v, want %v", c.answer, c.emptyResponse, got, c.want)- }- })- }-}--// TestDecorateQuote pins that a not-found answer is decorated with quoting off-// (no markers, no reference), while a grounded one keeps quoting on.-func TestDecorateQuote(t *testing.T) {- if decorateQuote(true, "知识库中未找到您要的答案!", "") {- t.Fatal("not-found answer must be decorated without quoting")- }- if !decorateQuote(true, "California voted for the same nominee [ID:0].", "知识库中未找到您要的答案!") {- t.Fatal("grounded answer must keep quoting")- }- if decorateQuote(false, "California voted for the same nominee [ID:0].", "") {- t.Fatal("quote=false must stay off")- }-}--// TestDecorateHarnessAnswerDropsCitationsForNotFoundAnswer is the end-to-end-// shape of the report: an answer that only says the knowledge base has no answer-// must come back without [ID:n] markers, even though the evidence pool and a-// resolvable marker are present. The evidence itself still ships — what the-// cite switch removes is the doc_aggs cards the markers would have opened.-func TestDecorateHarnessAnswerDropsCitationsForNotFoundAnswer(t *testing.T) {+// TestDecorateHarnessAnswerKeepsCitationsForNotFoundAnswer pins that an answer+// reporting no result is decorated like any other answer: its resolvable [ID:n]+// markers and the referenced doc_aggs still reach the client. The citation+// switch (quote) is the only thing that removes them.+func TestDecorateHarnessAnswerKeepsCitationsForNotFoundAnswer(t *testing.T) {kbinfos := map[string]interface{}{"chunks": []map[string]interface{}{{"chunk_id": "c0", "content_with_weight": "a", "doc_id": "d1", "docnm_kwd": "Doc One"},@@ -645,13 +583,13 @@ func TestDecorateHarnessAnswerDropsCitationsForNotFoundAnswer(t *testing.T) {}answer := "知识库中未找到您要的答案!因此无法统计得克萨斯州和加利福尼亚州 [ID:0] 选出同一候选人的次数。"s := &ChatPipelineService{}- res := s.decorateHarnessAnswer(answer, kbinfos, nil, nil, decorateQuote(true, answer, ""))+ res := s.decorateHarnessAnswer(answer, kbinfos, nil, nil, true)- if strings.Contains(res.Answer, "[ID:") {- t.Fatalf("not-found answer still carries citation markers: %q", res.Answer)+ if !strings.Contains(res.Answer, "[ID:0]") {+ t.Fatalf("answer lost its citation markers: %q", res.Answer)}- if _, has := res.Reference["doc_aggs"]; has {- t.Fatalf("not-found answer still carries doc_aggs: %#v", res.Reference)+ if _, has := res.Reference["doc_aggs"]; !has {+ t.Fatalf("answer lost its doc_aggs: %#v", res.Reference)}chunks, _ := res.Reference["chunks"].([]map[string]interface{})if len(chunks) != 1 || chunks[0]["id"] != "c0" {