agentic_huge_data_base / wiki
页面 Dify · 4 知识库与 RAG 系统·DeepWiki 中文全文译文

4 · 知识库与 RAG 系统(Knowledge Base and RAG System)

应用编排与外部知识接入 · 聚焦本章的模块关系、源码依据与实现要点。

项目Dify 章节4 状态全文译文 模块检索、召回与索引、模型调用与提供方适配、图谱与关系、文档对象与元数据
源码线索
  • api/controllers/console/app/annotation.py
  • api/controllers/console/datasets/data_source.py
  • api/controllers/console/datasets/datasets_document.py
  • api/controllers/console/datasets/datasets_segments.py
  • api/controllers/console/datasets/external.py
  • api/controllers/console/datasets/hit_testing.py
  • api/controllers/console/datasets/hit_testing_base.py
  • api/controllers/console/datasets/metadata.py
  • api/controllers/service_api/app/annotation.py
  • api/controllers/service_api/dataset/dataset.py
模块标签
  • 检索、召回与索引
  • 模型调用与提供方适配
  • 图谱与关系
  • 文档对象与元数据
  • 系统架构

章节正文

知识库与 RAG 系统

知识库与检索增强生成(RAG)系统

相关源文件

本章引用的主要源码文件:

  • api/controllers/console/app/annotation.py
  • api/controllers/console/datasets/data_source.py
  • api/controllers/console/datasets/datasets_document.py
  • api/controllers/console/datasets/datasets_segments.py
  • api/controllers/console/datasets/external.py
  • api/controllers/console/datasets/hit_testing.py
  • api/controllers/console/datasets/hit_testing_base.py
  • api/controllers/console/datasets/metadata.py
  • api/controllers/service_api/app/annotation.py
  • api/controllers/service_api/dataset/dataset.py
  • api/controllers/service_api/dataset/document.py
  • api/controllers/service_api/dataset/hit_testing.py
  • api/controllers/service_api/dataset/metadata.py
  • api/controllers/service_api/dataset/segment.py
  • api/core/callback_handler/index_tool_callback_handler.py
  • api/core/helper/moderation.py
  • api/core/helper/ssrf_proxy.py
  • api/core/indexing_runner.py
  • api/core/rag/datasource/retrieval_service.py
  • api/core/rag/extractor/pdf_extractor.py
  • api/core/rag/extractor/word_extractor.py
  • api/core/rag/index_processor/index_processor_base.py
  • api/core/rag/index_processor/processor/paragraph_index_processor.py
  • api/core/rag/index_processor/processor/parent_child_index_processor.py
  • api/core/rag/index_processor/processor/qa_index_processor.py
  • api/core/rag/retrieval/dataset_retrieval.py
  • api/core/tools/utils/dataset_retriever/dataset_multi_retriever_tool.py
  • api/core/tools/utils/dataset_retriever/dataset_retriever_tool.py
  • api/core/workflow/nodes/knowledge_retrieval/knowledge_retrieval_node.py
  • api/services/annotation_service.py
  • api/services/dataset_service.py
  • api/services/entities/knowledge_entities/knowledge_entities.py
  • api/services/hit_testing_service.py
  • api/services/knowledge_service.py
  • api/services/summary_index_service.py
  • api/tests/test_containers_integration_tests/models/test_conversation_status_count.py
  • api/tests/unit_tests/controllers/console/datasets/test_hit_testing_base.py
  • api/tests/unit_tests/controllers/service_api/app/test_annotation.py
  • api/tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py
  • api/tests/unit_tests/controllers/service_api/dataset/test_document.py
  • api/tests/unit_tests/controllers/service_api/dataset/test_hit_testing.py
  • api/tests/unit_tests/controllers/web/test_web_login.py
  • api/tests/unit_tests/core/helper/test_ssrf_proxy.py
  • api/tests/unit_tests/core/rag/datasource/test_datasource_retrieval.py
  • api/tests/unit_tests/core/rag/extractor/test_word_extractor.py
  • api/tests/unit_tests/core/rag/indexing/processor/test_paragraph_index_processor.py
  • api/tests/unit_tests/core/rag/indexing/processor/test_parent_child_index_processor.py
  • api/tests/unit_tests/core/rag/indexing/processor/test_qa_index_processor.py
  • api/tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py
  • api/tests/unit_tests/core/tools/test_signature.py
  • api/tests/unit_tests/models/test_app_models.py
  • api/tests/unit_tests/services/test_annotation_service.py
  • api/tests/unit_tests/services/test_batch_indexing_base.py
  • api/tests/unit_tests/services/test_hit_testing_service_dump_records.py
  • api/tests/unit_tests/services/test_knowledge_service.py
  • api/tests/unit_tests/services/test_metadata_bug_complete.py
  • api/tests/unit_tests/services/test_metadata_nullable_bug.py

本文档介绍 Dify 中的知识库与检索增强生成(RAG)系统,该系统支持对文档集合进行语义搜索。系统负责文档的入库、片段切分、向量化、存储和检索。关于 RAG 检索如何集成到工作流执行中,请参见工作流引擎与节点执行。关于 RAG 操作中使用的大语言模型(LLM)提供方配置,请参见大语言模型(LLM)集成与模型管理

RAG 系统包含以下组件:

数据集与文档数据模型

知识库系统将内容组织为层级结构:Dataset(数据集)→ Document(文档)→ DocumentSegment(文档片段)→(可选)ChildChunk(子块)。

核心实体关系
Dify · 核心实体关系 · 图 1
Dify · 核心实体关系 · 图 1

来源: api/models/dataset.py:38-54, api/services/dataset_service.py:39-54

数据集配置结构

Dataset 包含控制索引和检索行为的关键配置:

字段类型用途
indexing_technique"high_quality""economy"高质量模式使用向量嵌入向量;经济模式仅使用关键词 api/core/rag/index_processor/constant/index_type.py:24-25
embedding_model字符串用于向量生成的模型名称(例如 "text-embedding-3-small"
embedding_model_provider字符串提供方 ID(例如 "openai""anthropic"
retrieval_modelJSONB搜索方法、重排序配置、top-k、分数阈值 api/services/entities/knowledge_entities/knowledge_entities.py:73-75
provider"vendor""external"Dify 内部存储与外部 API api/services/dataset_service.py:50-51
doc_form字符串"text_model""qa_model""parent_child" api/core/rag/index_processor/constant/index_type.py:18-20

来源: api/models/dataset.py:38-54, api/services/entities/knowledge_entities/knowledge_entities.py:70-76

文档索引管线(ETL)

IndexingRunner 负责编排文档入库的提取-转换-加载(ETL)管线。该流程分为三个不同的阶段。详情请参见文档索引管线

索引管线流程
Dify · 索引管线流程 · 图 2
Dify · 索引管线流程 · 图 2

来源: api/core/indexing_runner.py:50-128, api/core/indexing_runner.py:93-119

索引处理器与分段策略

Dify 支持三种文档分段策略,每种策略都实现为 IndexProcessor。详情请参见文档索引管线

处理器架构
Dify · 处理器架构 · 图 3
Dify · 处理器架构 · 图 3

来源: api/core/rag/index_processor/index_processor_base.py:25-30, api/core/rag/index_processor/index_processor_factory.py:26-30

向量数据库集成

Dify 通过工厂模式抽象支持 23 种以上的向量数据库实现。详情请参见向量数据库集成架构

向量数据库工厂架构
Dify · 向量数据库工厂架构 · 图 4
Dify · 向量数据库工厂架构 · 图 4

来源: api/core/rag/datasource/vdb/vector_factory.py:15-20, api/core/rag/datasource/retrieval_service.py:15-23

检索服务与搜索方法

RetrievalService 提供统一接口,用于访问所有向量数据库中的多种搜索方法。详情请参见检索策略与元数据过滤

检索方法类型
Dify · 检索方法类型 · 图 5
Dify · 检索方法类型 · 图 5

来源: api/core/rag/datasource/retrieval_service.py:92-171, api/core/rag/retrieval/retrieval_methods.py:25-30

数据集检索与路由

对于包含多个数据集的应用,DatasetRetrieval 类负责编排检索和路由。详情请参见检索策略与元数据过滤

单数据集与多数据集检索
Dify · 单数据集与多数据集检索 · 图 6
Dify · 单数据集与多数据集检索 · 图 6

来源: api/core/rag/retrieval/dataset_retrieval.py:157-170, api/core/rag/retrieval/dataset_retrieval.py:101-118

元数据过滤

元数据过滤允许将检索限制为符合特定元数据条件的文档片段。详情请参见检索策略与元数据过滤

元数据过滤模式

支持三种过滤模式:

  1. disabled(禁用):不进行元数据过滤。
  2. manual(手动):用户通过界面/API 定义精确的过滤条件。
  3. automatic(自动):大语言模型(LLM)分析查询并自动生成过滤条件。

来源: api/core/rag/retrieval/dataset_retrieval.py:130-157, api/core/app/app_config/entities.py:15-20

摘要索引系统

摘要索引系统为文档片段生成简洁的摘要,支持基于摘要的检索。详情请参见摘要索引生成

来源: api/services/dataset_service.py:103, api/tasks/regenerate_summary_index_task.py:1-50

外部知识库集成

外部知识库允许 Dify 从第三方 API 检索内容,而无需在内部管理文档。详情请参见外部知识库集成

来源: api/services/external_knowledge_service.py:85-89, api/core/rag/datasource/retrieval_service.py:173-195

API 端点

知识库系统为控制台(管理端)和服务(应用端)使用提供了 API。详情请参见数据集服务与文档管理

来源: api/controllers/console/datasets/datasets_document.py:1-100, api/controllers/service_api/dataset/dataset.py:129-185, api/controllers/service_api/dataset/document.py:128-193

DatasetService 核心操作

DatasetService 类提供数据集管理的高级操作。详情请参见数据集服务与文档管理

来源: api/services/dataset_service.py:120-154