Building a Private RAG System: Inference Service and Document Engine Practices

    This article explains how to build a private Retrieval-Augmented Generation (RAG) system using a two-stack architecture: a model inference layer and a document understanding and retrieval layer. It covers the key configuration of containerized inference deployment, the selection logic for reranking models, the workflow of a deep document understanding engine, and a checklist for integration and tuning. Readers will learn how the two layers divide responsibilities, how to troubleshoot quality and performance issues by layer, and how to balance effectiveness, cost, and compliance in a private deployment.

    1. Why Private RAG Needs a Two-Stack Architecture: Inference Plus Retrieval

    The core value of Retrieval-Augmented Generation (RAG) is decoupling parametric knowledge from non-parametric knowledge: the large language model handles language organization and reasoning, while the external knowledge base supplies facts. This decoupling has a direct consequence—the system naturally splits into two layers with completely different responsibilities.

    The first is the model inference layer, which loads and invokes large language models, embedding models, and reranking models. It focuses on GPU memory usage, batch throughput, context length, and concurrent scheduling. The second is the document understanding and retrieval layer, which handles document parsing, chunking, vectorization and indexing, hybrid recall, and reranking. It focuses on parsing accuracy, index update latency, and citation traceability.

    The two components described in the original text map exactly to these layers: the former is a platform for hosting inference capabilities of multiple open-source models under one runtime, and the latter is a RAG engine that emphasizes deep document understanding. Understanding this matters more than memorizing any single command, because it determines the troubleshooting path: if answer quality is poor, inspect the retrieval layer first; if responses are slow or GPU memory overflows, inspect the inference layer first.

    2. Inference Service Layer: Key Configuration for Containerized Deployment

    Delivering the inference service in a container is a common industry practice. Its value is packaging the driver, runtime, and model directory into a reproducible environment. A typical startup command contains the following elements, and understanding each item is more useful than copying the whole command:

    1. docker run -d \
    2. --name inference-svc \
    3. -p 9997:9997 \
    4. -e RUNTIME_HOME=/data \
    5. -v $(pwd):/data \
    6. --gpus all \
    7. <inference-image>:latest \
    8. <server-cmd> -H 0.0.0.0
    • Background execution and naming: -d and --name make the service lifecycle manageable, which eases later log inspection and restarts.
    • Port mapping: -p 9997:9997 exposes the in-container service port on the host. Once deployed, the management interface is reachable via the host address plus that port.
    • Externalized data directory: an environment variable points the runtime home to a mount point inside the container, and the host’s current directory is mounted there, so model files, configuration, and logs persist. When the container is rebuilt, models do not need to be downloaded again—the most important detail in long-term operations.
    • GPU passthrough: --gpus all lets the container use all host GPUs. If the GPUs must be shared with other services, switch to assigning specific devices to avoid resource contention.
    • Listen address: -H 0.0.0.0 accepts connections from any source. Note that this is a convenience setting; in shared networks or public-facing environments it must be combined with access control, a reverse proxy, and authentication, otherwise the model endpoint is effectively fully open.

    3. Model Loading: Selection Logic Using a Reranking Model as an Example

    The value of an inference platform is “one runtime, multiple models.” In RAG scenarios, besides the generation model and the embedding model, a reranking model is often the most cost-effective component: the recall stage pursues coverage and can pull dozens of candidates from millions of documents; the rerank stage pursues precision, using a cross-encoder to score each query-document pair, pushing the truly relevant content to the top before taking the first several chunks into the generation model.

    For model sources, prefer a model community or mirror repository with good connectivity in your network environment, which can significantly shorten the first download. With limited GPU memory, choose a smaller reranking model, or load it at a different time from the generation model. After loading, fill in that model’s endpoint and model identifier on the RAG engine side; keeping the parameters consistent on both sides is the most common source of errors during integration.

    4. Retrieval Layer: The Difference Deep Document Understanding Makes

    The traditional approach treats a document as a plain text stream and hard-cuts it at a fixed length. Misaligned tables, headers and footers mixed in, and multi-column layouts read in the wrong order keep polluting recall results. A RAG engine that emphasizes deep document understanding typically includes these steps:

    1. Layout and structure parsing: identify heading levels, paragraphs, tables, and images, and preserve the section path so that chunk boundaries follow semantics.
    2. Semantic chunking and metadata tagging: split by structure rather than character count, and attach metadata such as source file, page number, and section to support citation traceability.
    3. Hybrid recall: vector retrieval is good at semantic similarity, while keyword retrieval is good at proper nouns and identifiers; fusing the two noticeably improves hit rates for long-tail queries.
    4. Reranking and context assembly: rerank candidate chunks, then truncate them to control the context size sent to the model, balancing quality against cost.
    5. Citation generation: marking sources in the answer is a key indicator of whether RAG is trustworthy, and a threshold for adoption in enterprise scenarios.

    For deployment, clone the source code, switch to a designated stable version, and then enter the orchestration directory. This is the safer approach: the main branch may contain unreleased changes, and pinning a version number keeps the environment reproducible. Then bring up the service through container orchestration and complete, in order, the model service endpoint configuration, knowledge base creation, and document ingestion.

    5. Integration and Tuning Checklist

    • Connectivity first: confirm the retrieval layer can reach the inference layer’s endpoint before investigating quality issues.
    • Layered diagnosis: for inaccurate answers, check recall and reranking; for timeouts or GPU memory overflow, check batch size, context length, and concurrency limits.
    • Observability: connect log collection and monitoring alerts for the inference service and the retrieval service separately, watching request latency, failure rate, and GPU memory levels.
    • Data boundaries: the core benefit of private deployment is that data stays inside the internal network; model weights, vector indexes, and original documents should be stored in controlled storage with regular backups.
    • Elastic scaling: as concurrency rises, horizontally scale the stateless retrieval service on a container platform, scale the inference layer according to GPU resources, or use a message queue to smooth peaks.

    6. Conclusion

    The difficulty of putting private RAG into production lies not in any single command but in the coordination of the two layers: the inference layer provides stable, concurrent model capability, and the retrieval layer provides clearly structured, traceable knowledge supply. Decoupling the two, observing them separately, and scaling each on its own is what makes a sustainable balance among quality, cost, and compliance possible. If a cloud-based form is used, common capabilities such as container platforms, object storage, log services, and monitoring alerts can be reused directly, further shortening the distance from prototype to production.