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): drop citations and reference when the answer reports no… (#20111)
Caobaining1ed12d999bcf5
diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.goindex 0bd78e723d..33a3eeb906 100644--- a/internal/service/chat_pipeline.go+++ b/internal/service/chat_pipeline.go@@ -418,6 +418,10 @@ func (s *ChatPipelineService) AsyncChat(if promptConfigQuote, ok := promptConfig["quote"].(bool); ok {quote = quote && promptConfigQuote}+ // 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))@@ -858,7 +862,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, quote)+ final := s.decorateHarnessAnswer(harnessAnswer, kbinfos, slotCites, citeChunkIDs, decorateQuote(quote, harnessAnswer, emptyResponse))final.Final = trueout <- finalreturn@@ -1474,7 +1478,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, quote, nil, langfuseTraceID, llmModelConfig, chat.TenantID, kbTenantIDStrings(kbs), len(knowledges) > 0)+ 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.Final = truefinal.AudioBinary = niltimer.Exit(common.PhaseGenerateAnswer)@@ -1515,7 +1519,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, quote, ttsModel, langfuseTraceID, llmModelConfig, chat.TenantID, kbTenantIDStrings(kbs), len(knowledges) > 0)+ 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.Final = truetimer.Exit(common.PhaseGenerateAnswer)out <- finaldiff --git a/internal/service/citation.go b/internal/service/citation.goindex 0c4a26429b..0174f245dc 100644--- a/internal/service/citation.go+++ b/internal/service/citation.go@@ -592,6 +592,62 @@ 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 30e4a87945..e4bdef6952 100644--- a/internal/service/citation_test.go+++ b/internal/service/citation_test.go@@ -529,3 +529,89 @@ func TestCitationStreamFilter(t *testing.T) {t.Fatalf("filtered answer=%q, want %q", got, want)}}++// 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 with neither [ID:n] markers nor a reference, even though the+// evidence pool and a resolvable marker are present.+func TestDecorateHarnessAnswerDropsCitationsForNotFoundAnswer(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"},+ },+ "doc_aggs": []interface{}{+ map[string]interface{}{"doc_id": "d1", "doc_name": "Doc One"},+ },+ }+ answer := "知识库中未找到您要的答案!因此无法统计得克萨斯州和加利福尼亚州 [ID:0] 选出同一候选人的次数。"+ s := &ChatPipelineService{}+ res := s.decorateHarnessAnswer(answer, kbinfos, nil, nil, decorateQuote(true, answer, ""))++ if strings.Contains(res.Answer, "[ID:") {+ t.Fatalf("not-found answer still carries citation markers: %q", res.Answer)+ }+ if len(res.Reference) != 0 {+ t.Fatalf("not-found answer still carries a reference: %#v", res.Reference)+ }+}