Implementation:Infiniflow Ragflow Dealer Insert Citations
Appearance
| Knowledge Sources | |
|---|---|
| Domains | RAG, Information_Retrieval |
| Last Updated | 2026-02-12 06:00 GMT |
Overview
Concrete tool for inserting citation markers into LLM answers by matching to retrieved chunks provided by RAGFlow's Dealer class.
Description
Dealer.insert_citations matches segments of the LLM-generated answer to retrieved chunks using hybrid keyword+vector similarity. It inserts [ID:N] markers in the answer text and returns the set of matched chunk indices. Called from decorate_answer in DialogService when the quote option is enabled.
Usage
Called automatically during chat response post-processing.
Code Reference
Source Location
- Repository: ragflow
- File: api/db/services/dialog_service.py (L630-638 call site), rag/nlp/search.py (Dealer.insert_citations)
Signature
class Dealer:
def insert_citations(
self,
answer: str,
content_ltks: list[str],
vectors: list,
embd_mdl,
tkweight: float,
vtweight: float
) -> tuple[str, set]:
"""Insert citation markers into answer text.
Args:
answer: str - LLM-generated answer.
content_ltks: list[str] - Tokenized content of retrieved chunks.
vectors: list - Embedding vectors of retrieved chunks.
embd_mdl: EmbeddingModel - For encoding answer segments.
tkweight: float - Keyword similarity weight (1 - vector_similarity_weight).
vtweight: float - Vector similarity weight.
Returns:
tuple[str, set] - (answer with [ID:N] markers, set of matched indices).
"""
Import
from rag.nlp.search import Dealer
# Or via settings.retriever
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| answer | str | Yes | LLM-generated answer text |
| content_ltks | list[str] | Yes | Tokenized chunk contents |
| vectors | list | Yes | Chunk embedding vectors |
| embd_mdl | EmbeddingModel | Yes | For encoding answer segments |
| tkweight | float | Yes | Keyword weight |
| vtweight | float | Yes | Vector weight |
Outputs
| Name | Type | Description |
|---|---|---|
| answer | str | Answer with [ID:0], [ID:1], etc. markers |
| indices | set | Set of matched chunk indices |
Usage Examples
# Internal usage in decorate_answer
answer, idx = retriever.insert_citations(
answer,
[ck["content_ltks"] for ck in kbinfos["chunks"]],
[ck["vector"] for ck in kbinfos["chunks"]],
embd_mdl,
tkweight=1 - dialog.vector_similarity_weight,
vtweight=dialog.vector_similarity_weight,
)
# answer now contains [ID:0], [ID:1] markers
Related Pages
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment