A Practical Guide to Building a Private RAG System
This article walks through the practical steps of building a private, on-premises RAG system: the four-layer architecture, containerized inference deployment, embedding and reranker models, knowledge base construction, retrieval tuning with citation traceability, and filling infrastructure gaps in a private environment. It also highlights the common pitfalls around version pinning, document parsing quality, retrieval evaluation, and access governance.
1. Why Private RAG Has Become a Business Necessity
Large language model capabilities can be obtained through API calls, but enterprise knowledge is usually scattered across internal materials such as contracts, support tickets, manuals, and reports. This content is not suitable for external sharing, and general-purpose models struggle to interpret it directly. Retrieval-Augmented Generation (RAG) addresses this by replacing “model memory” with externally retrievable evidence, so answers are grounded in sources while sensitive data stays inside your own network.
One common misconception needs clearing up: RAG is not simply chunking documents and stuffing them into a prompt. It is an engineering pipeline with eight stages: document parsing, chunking, vectorization, indexing, retrieval, reranking, generation, and citation backfilling. Weakness at any stage shows up as irrelevant answers or fabricated content.
2. Layered Architecture
A deployable private setup typically has four layers:
- Compute and inference layer: Runs the LLM inference service in containers and exposes an endpoint that speaks a common API protocol for upstream applications.
- Model serving layer: Besides the chat model, you need separately deployed embedding and reranker models. The three have different responsibilities, and mixing them up noticeably degrades retrieval quality.
- Knowledge processing layer: Handles multi-format document parsing, layout reconstruction, chunking strategy, metadata extraction, and index building.
- Application and governance layer: Q&A orchestration, permission filtering, log auditing, monitoring and alerting, and quality evaluation.
3. Containerizing the Inference Service
The mainstream approach is to bring up inference services through a container platform. The logic of a typical startup command breaks down into a few elements, and understanding the elements matters more than memorizing the command:
docker run -d --name inference \-p 9997:9997 \-e HOME_DIR=/data \-v $(pwd):/data \--gpus all \<inference image> \<startup command> -H 0.0.0.0
-druns the container in the background so the service survives terminal disconnects.-pmaps the port; the host port must match what the application side is configured to use.-vmounts the data directory. Model weights and configuration must be persisted, otherwise a rebuilt container has to re-download tens of gigabytes of weights.--gpus allgrants GPU access. With a newer GPU model, confirm that the driver and container runtime versions are compatible.-H 0.0.0.0allows external access. If only the loopback address is specified, callers outside the host will fail to connect.
Frequent pitfalls: slow or interrupted image pulls, GPU driver incompatibility with the container runtime, inference processes being killed due to insufficient shared memory, and random failures caused by multiple models competing for VRAM. A practical troubleshooting order: check container logs first, then GPU utilization and remaining VRAM, and finally verify ports and network policies.
4. Embedding and Reranker Models
The ceiling on retrieval quality is set by the embedding model. Three things matter when choosing one: Chinese semantic matching performance, maximum input length, and the cost implied by the vector dimension and index size. When deploying, prefer a domestic mirror source to shorten download time, and pin the model version. Otherwise an upgrade can shift the vector space for the same set of documents and invalidate existing indexes.
The reranker performs fine-grained ordering over the initially retrieved candidates. A common engineering pattern is two-stage: retrieve dozens of candidates in the coarse pass, then keep the top few after reranking. If retrieved content is relevant but poorly ordered, first verify that the reranker is actually taking effect rather than repeatedly adjusting chunk granularity.
5. Building the Knowledge Base
Using the deployment flow of an open-source RAG engine as an example, the general steps are:
git clone <repository URL>cd <project directory>/dockergit checkout -f <pinned version tag># Then edit the environment variable file to set the model service address, ports, and database credentials# Finally start the orchestration services
The version must be pinned. Following the default branch for upgrades often leads to image and configuration mismatches that prevent startup. The model addresses, vector dimension, and database credentials in the environment variable file should all be verified before launch.
Document parsing is the dividing line for output quality. Scanned PDFs, complex tables, and multi-level headings that are not correctly reconstructed cannot be compensated for by a better model downstream. Sample the parsing results for manual verification, and keep a mapping between original files and parsed text so problems are easy to trace.
6. Retrieval Tuning and Citation Traceability
- Hybrid retrieval: Vector search handles semantic similarity, keyword search handles proper nouns and identifiers; combining them covers more scenarios.
- Metadata filtering: Pre-filter by department, classification level, and time range. This improves precision and is also where access control is enforced.
- Chunking strategy: Split along semantic paragraphs and keep the heading path as context so sentences are not cut in half.
- Citation backfilling: Annotate the source document and paragraph position in the answer; this is key to acceptance in enterprise settings.
- Evaluation loop: Build a fixed Q&A set and continuously track retrieval hit rate and answer accuracy instead of tuning parameters by intuition.
7. Filling Capability Gaps in the Cloud
Private environments often lack supporting infrastructure, which can be filled by general category: object storage for original documents and parsing artifacts, a container platform to host inference and orchestration services, a logging service to collect the call chain, and monitoring and alerting to cover VRAM, latency, and failure rates. Baidu AI Cloud offers corresponding capabilities in these general categories, which can be combined as needed to close the operations loop while keeping data inside the internal network.
8. Conclusion
The hard part of putting RAG into production is not starting a service. It is sustained investment in four things: version pinning, parsing quality, retrieval evaluation, and permission governance. Get the pipeline working end to end with observable data first, then optimize step by step. That is the cost-controlled path.
