上传文档,构建向量知识库,用户提问时检索精确答案并附引用来源。

适用场景

Legal teams reviewing contracts, HR departments with policy documents, technical teams with API documentation.

前提条件

Dify account, documents to upload, OpenAI API key for embeddings

工作流图

用 Dify 搭建 RAG 文档问答系统

Create Knowledge Base
Configure Retrieval
Create Q&A App
Add Citation Display
Deploy and Monitor

分步指南

  1. 1

    Create Knowledge Base

    In Dify, create a new Knowledge Base. Upload your documents (PDF, DOCX, TXT, MD). Set chunking to 300 tokens with 50 token overlap. Choose the embedding model (OpenAI text-embedding-3-small is cost-effective).

  2. 2

    Configure Retrieval

    Set retrieval mode to 'semantic search' with top-K=5. Enable reranking if available (Cohere reranker improves accuracy significantly). Set a similarity threshold of 0.7 to filter irrelevant chunks.

  3. 3

    Create Q&A App

    Create a new Completion App. Connect your knowledge base. Set the prompt: 'Answer the user's question using ONLY the provided context. If the answer is not in the context, say I cannot find this information. Always cite the source document and page number.'

  4. 4

    Add Citation Display

    Enable Dify's citation feature. When the model generates an answer, it will show which chunks were used. Configure the UI to display source filenames and relevant passages.

  5. 5

    Deploy and Monitor

    Deploy as a web app or API. Set up logging to track: query volume, answer accuracy (user feedback), and unanswered questions (to identify knowledge gaps). Add new documents based on gaps found.

标签

#rag#document-qa#knowledge-base#dify#retrieval