External Knowledge Base Retrieval in Dify: A Practical Guide to Better RAG Accuracy

    This guide explains why built-in knowledge bases in visual application orchestration platforms often return inaccurate retrieval results, and how moving retrieval to an external service makes quality measurable and tunable. It covers the three-stage architecture, deployment considerations for retrieval services and their dependencies, parsing and vectorization strategies, building a small evaluation set, configuring external knowledge bases and generation parameters in Dify, and a troubleshooting checklist for common issues such as vague answers, irrelevant chunks, and broken table data.

    1. The Problem: Why Built-In Knowledge Bases Retrieve Poorly

    When teams build question-answering applications on visual application orchestration platforms, they often run into the same issue: documents are uploaded and parsing reports success, yet the model’s answers clearly drift from the original meaning of the documents, or only generic, peripheral fragments are retrieved. This is rarely a failure of a single component. It is usually the combined effect of multiple factors along the retrieval chain.

    The common causes fall into four categories:

    • Chunking strategy does not fit the document structure: Fixed-length chunking tends to cut through tables, clauses, and heading hierarchies, leaving individual chunks semantically incomplete.
    • Embedding model does not match the domain of the corpus: General-purpose embedding models lack discriminative power for specialized terms, abbreviations, and internal code names, so vectors end up close in distance but unrelated in meaning.
    • No reranking stage: Vector recall only looks at similarity, with no second-pass judgment of candidate chunks against the query intent, so noise slips into the Top-K results.
    • Loose generation parameters and prompt constraints: A high temperature and the absence of a hard “answer only from the retrieved content” constraint in the prompt encourage the model to improvise.

    A more engineering-oriented solution is therefore to separate retrieval from the orchestration platform and hand it to an external retrieval service that can be tuned independently. Retrieval quality can then be measured and iterated on its own, without being limited by the invisible details of the platform’s built-in pipeline.

    2. Overall Architecture

    The pipeline has three stages:

    1. Knowledge processing: Document upload → parsing (layout, tables, OCR) → chunking → embedding → writing to the vector index and full-text index.
    2. Retrieval service: Exposes a unified retrieval API that accepts a query and retrieval parameters and returns candidate chunks with their scores.
    3. Application orchestration: The orchestration platform calls the retrieval service through the external knowledge base interface, assembles the recalled chunks into a prompt, and lets the large language model generate the answer.

    The benefit of this layering is clear separation of responsibilities: knowledge processing and retrieval are owned by the external service, while the orchestration platform only handles “call + assemble + generate.”

    3. Deploying the External Retrieval Service

    A complete retrieval service typically depends on several base components working together, commonly including a full-text search engine, a relational database, object storage, a cache service, and the retrieval service itself. Keep the following points in mind during deployment.

    1. Ports and exposure scope

    A retrieval service usually exposes multiple ports for the web UI, encrypted access, and API calls. For integration with the orchestration platform, only the API port matters, and the orchestration side must be able to reach it over the network. It is recommended to open that port only on the internal network rather than exposing it directly to the public internet.

    1. # Check containers and port usage
    2. docker ps --format "table {{.Names}}\t{{.Ports}}"
    3. # Verify retrieval API connectivity (illustrative)
    4. curl -s http://<retrieval-service-host>:<api-port>/api/v1/health

    2. Resource planning for dependent components

    The full-text search engine is memory-sensitive, while the relational database and object storage are sensitive to disk throughput. In production, deploy the components on separate nodes and plan index capacity in advance: the number of chunks produced after parsing is usually several times the number of paragraphs in the source text, so storage grows quickly.

    3. Configuring the embedding and inference models

    Two types of models must be connected after deployment: the embedding model converts text into vectors, and the inference model produces the final answer. They should be configured separately. Once the embedding model changes, all historical vectors must be rebuilt in full, so the choice has to be right from the start; the inference model, by contrast, can be swapped at any time.

    4. Building the Knowledge Base and Parsing Strategy

    1. Choosing a parsing method

    The parsing method determines the quality of converting unstructured documents into structured text. For PDFs with complex layouts and reports with many tables, prefer a parsing mode with layout analysis and table reconstruction capabilities. For plain text and Markdown-like corpora, general parsing is enough. A wrong parsing choice cannot be compensated for by any later stage.

    2. Upload and vectorization

    Uploading a document triggers a parsing task, and once parsing finishes the system automatically performs chunking and vectorization. In practice:

    • Test parsing on a small batch of single files first, and confirm the chunk granularity is reasonable before processing in bulk.
    • Add metadata such as title, section, and last-updated time to documents so retrieval can filter on them.
    • Set up a refresh mechanism for time-sensitive content to prevent stale chunks from being recalled continuously.

    3. Building an evaluation set

    Before going live, collect 30–50 real business questions with their correct chunks to form a small evaluation set. Run a regression after each change to chunking, embedding, or Top-K, using recall hit rate instead of the subjective feeling that results are “not accurate.”

    5. Integrating on the Orchestration Platform Side

    1. Configuring the external knowledge base

    Add an external knowledge base in the orchestration platform, then fill in the retrieval service’s API address, call credentials, and knowledge base identifier, and set the retrieval parameters:

    1. {
    2. "knowledge_id": "kb_demo_001",
    3. "query": "retrieval query text",
    4. "top_k": 6,
    5. "score_threshold": 0.35,
    6. "rerank": false
    7. }

    top_k determines the number of candidates: too small misses recall, too large introduces noise. score_threshold filters out low-relevance chunks and needs repeated calibration against the evaluation set.

    2. Prompt and generation parameters

    The prompt should include an explicit constraint, for example: “Answer only from the provided material, and state clearly when the material does not cover the question.” Keep the temperature in a low range so answers stay stable and reproducible. If the retrieval service does not have a reranking model enabled, leave it off at first and evaluate introducing it once recall quality is stable.

    3. Attributing performance differences correctly

    When the same question behaves differently in the retrieval service’s own chat UI and in the orchestration platform, in most cases retrieval is not at fault. The two sides usually differ in prompt templates, how context is assembled, the inference model, and the temperature setting. The recommended troubleshooting order is: first align prompts and parameters, then compare whether the recalled chunks are identical, and only then suspect retrieval itself.

    6. Common Issues and Tuning Checklist

    • Vague answers: increase top_k and lower the score threshold; check whether a too-high threshold filtered everything out.
    • Irrelevant recalled chunks: switch to an embedding model that fits the domain better and rebuild the index.
    • Garbled table data: switch to a parsing method with table parsing capability and re-vectorize.
    • Answers that go beyond the material: strengthen prompt constraints and lower the temperature.
    • Answers unchanged after a document update: confirm parsing and vectorization tasks have finished, and clear the old index if necessary.

    7. Summary

    Externalizing retrieval turns “uncontrollable black-box recall” into a measurable, iterable engineering module. As long as you calibrate layer by layer in the order of parsing strategy → embedding choice → retrieval parameters → prompt constraints, and keep running regressions with a small evaluation set, poor recall from a built-in knowledge base can be improved systematically. To reduce the burden of self-hosting and operations, you can also evaluate general retrieval and vector storage capabilities from cloud vendors such as Baidu AI Cloud, and focus your effort on business corpus governance and evaluation practices.