Building a Private RAG System: From Model Inference to Document Parsing

    This article walks through the engineering pipeline required to deploy a production-grade private RAG system. Instead of focusing on a single tool, it follows the full chain across four layers: containerized model inference services, embedding and reranking models, deep parsing of complex documents, and orchestration with evaluation and operations. It also covers common deployment pitfalls and a recommended step-by-step rollout order.

    1. Why Private RAG Is Hard: The Bottleneck Is the Engineering Pipeline, Not the Model

    Once large model inference became widely accessible, the enterprise focus shifted from “can it run?” to “can it run reliably in production?” Retrieval-Augmented Generation (RAG) has become the preferred approach because it decouples knowledge updates from parameter updates: knowledge lives in an external knowledge base, while the model only handles understanding the question and organizing the answer. This reduces hallucinations, supports citation traceability, and meets compliance requirements that keep data inside the internal network.

    In real projects, however, a usable RAG system requires at least four layers of capability: a model inference service layer, an embedding and reranking layer, a document parsing and chunking layer, and a retrieval and generation orchestration layer. If any layer is missing or misconfigured, the end result is answers that miss the point, and the cost of troubleshooting is extremely high. For that reason, this article follows the pipeline in order rather than starting from a single point tool.

    2. Layer One: Providing a Unified Model Inference Service with Containers

    The most common approach for internal deployment is to package inference capability as a standalone service that exposes a standardized interface protocol upward and hides differences between model formats downward. The benefit is that the application side depends on a single calling convention, and swapping the underlying model requires no changes to business code.

    A typical startup approach looks like this (illustrative; refer to the documentation of the image you use for exact parameters):

    1. docker run -d \
    2. --name inference-service \
    3. -p 9997:9997 \
    4. -e INFERENCE_HOME=/data \
    5. -v $(pwd):/data \
    6. --gpus all \
    7. <inference-service-image>:latest \
    8. <service-start-command> --host 0.0.0.0

    Parameter by parameter: -d runs the container in the background; -p maps ports, exposing the in-container service port to the host; -e sets the model cache directory so models are not re-downloaded after the image is rebuilt; -v mounts a host directory to persist models and configuration; --gpus all passes all GPUs through to the container; and finally --host 0.0.0.0 is critical, because with the default loopback address, requests from outside the host will fail to connect outright.

    Common pitfalls at this layer cluster in four places:

    1. Driver and runtime version mismatch. The compute framework inside the container requires both the host GPU driver and the container runtime to meet version requirements; after a driver upgrade, old containers may fail to start.
    2. Unreachable model download source. Internal network environments usually need an explicitly configured model repository mirror or weights copied offline in advance, and you should confirm the cache directory is writable.
    3. Startup failure due to insufficient GPU memory. Context length, batch concurrency, and quantization precision together determine GPU memory usage; it is advisable to validate the pipeline with a small context first, then scale up gradually.
    4. Missing health checks. In production, the service readiness probe should be decoupled from business calls, so the service is not judged available while the model is still loading.

    3. Layer Two: The Division of Labor Between Embedding and Reranking Models

    Retrieval quality in RAG is determined jointly by two models. The embedding model maps text into a semantic space and determines whether recall is complete; the reranking model performs fine-grained relevance scoring on candidate chunks and determines whether the ordering is accurate. The two cannot replace each other: with vector recall alone, Top-K results often mix in chunks that are topically close but cannot answer the question; adding reranking without widening the recall set means the correct answer is never in the candidate pool to begin with.

    The recommended engineering approach is a two-stage strategy: the first stage uses hybrid recall combining vector retrieval with keyword retrieval to gather 20–50 candidates, and the second stage uses the reranking model to compress them down to 3–5 before handing them to the generation model. When deploying the reranking model, it is likewise advisable to register it as a separate inference service endpoint so it can be scaled independently.

    4. Layer Three: Deep Parsing for Complex Documents

    The real form of an enterprise knowledge base is a mix of PDFs, scanned documents, tables, diagrams, and rich text, not clean plain text. Parsing quality directly sets the ceiling on retrieval: table structure lost during parsing cannot be recovered by any later ranking strategy.

    A pragmatic parsing pipeline should include: layout analysis to identify heading hierarchy and body regions, table structure reconstruction into searchable row and column semantics, optical character recognition for scanned documents with coordinate information preserved, denoising of headers, footers, and repeated watermarks, and finally chunking along semantic boundaries rather than fixed character counts. A chunk size between 300 and 800 characters is a reasonable range for A/B comparison, and each chunk should retain metadata such as source file, page number, and section path, which is the foundation for later “evidence-backed citations.”

    5. Layer Four: Orchestration, Evaluation, and Operations

    The orchestration layer strings the above capabilities into a closed loop: query rewriting, hybrid retrieval, reranking, context assembly, generation, and citation backfilling. The most easily overlooked part here is context budget management and conflict handling: when multiple chunks reach contradictory conclusions, the model should be explicitly instructed to flag uncertainty rather than force a single answer.

    Evaluation must come first. It is advisable to build a regression set of 50–200 real question-answer pairs and measure recall hit rate, Top-3 relevance after reranking, answer accuracy, and citation accuracy separately. Without a regression set, any parameter tuning can only rely on subjective impressions.

    On the operations side, general cloud product categories can fill in the gaps: a container platform hosts inference and parsing services with elastic scaling, object storage holds original documents and parsing artifacts, a message queue smooths out spikes in batch ingestion tasks, and logging and monitoring with alerting track time-to-first-token, throughput, and failure rate. If you want to reduce the burden of self-hosting, you can also evaluate general model services and retrieval capabilities offered by vendors such as Baidu AI Cloud, deciding which stages to keep on-premises based on data sensitivity.

    Do not try to build the entire pipeline at once. The recommended path is: first get single-model inference working with one successful call, then add embedding and reranking, then validate parsing with 20 typical documents, and only then build the orchestration and evaluation system. At every step, keep a rollback-able configuration snapshot to drive the debugging cost as low as possible.