Local LLM + Local Knowledge Base: A Practical Guide to Building a Fully Offline RAG Environment

    This guide walks through building a completely offline RAG (Retrieval-Augmented Generation) setup, where model inference, embedding, and knowledge base storage and retrieval all run locally with no data leaving your network. It covers environment preparation, installing a containerized knowledge base application, selecting and pulling text generation and embedding models, wiring the services together, validating retrieval quality, and extending the local API to power AI coding assistants. Readers will learn a practical, privacy-first architecture suitable for consumer hardware and sensitive codebases.

    1. Background and Overall Approach

    As large language models become mainstream, more individual developers and enterprises want to run them locally—especially for knowledge Q&A involving private data and internal documents. Sending data to cloud services carries compliance and leakage risks even when security guarantees are promised. A “fully local” approach is therefore a pragmatic choice: model inference runs locally, vectorization runs locally, and knowledge base storage and retrieval also run locally, so data never leaves the intranet across the entire pipeline.

    This solution consists of two core components:

    1. A local LLM runtime: loads and runs both the text generation model and the embedding model, exposing a unified inference API. Popular solutions of this kind support Windows, macOS, and Linux, and let you download and switch models on demand—friendly for hardware experimentation.
    2. A local knowledge base application: handles document import, chunking, vectorization, knowledge retrieval, and Q&A orchestration. These applications are typically deployed as Docker containers and rely on the local models for embedding generation and answer generation, forming a closed loop.

    Once the two are connected via API, you get: upload local documents → local vectorization → local retrieval → local LLM generates the answer, with no external network dependency at any step (aside from the initial model download).

    2. Environment Preparation

    2.1 Install a Container Runtime

    Knowledge base applications are usually distributed as container images, so install Docker first—download the appropriate version from the official website. After installation, run two checks:

    1. docker --version
    2. docker compose version

    If docker compose works, orchestration capability is ready. On Windows, confirm that the WSL2 backend is enabled; otherwise container performance will degrade noticeably.

    2.2 Install the Local Model Runtime

    Download the installer for your platform from the official website. Windows users should watch out for a common pitfall: the default installation path and model storage path are both on the C drive, while model files can range from several GB to tens of GB. Plan ahead:

    Change the installation directory: open a terminal in the installer’s directory and specify the install path via a command-line parameter:

    1. InstallerName.exe /DIR="D:\soft\llm-runtime"

    Change the model storage location: after installation, set the model directory environment variable to point to a partition with plenty of free space, e.g., D:\models. Restart the service for the change to take effect.

    Before pulling models, assess your hardware:

    • A 7B-class model needs roughly 4–8 GB of VRAM or RAM after quantization;
    • 14B and larger models should have at least 16 GB of VRAM, or you’ll need to accept slower CPU inference;
    • Embedding models are much smaller (a few hundred MB) and place little strain on hardware.

    2.3 Deploy the Local Knowledge Base Application

    Check the official documentation of the open-source project for system requirements (typically a Docker environment plus sufficient free memory). A typical deployment uses the project’s compose file for one-command startup:

    1. curl -sSL <compose-file-url> -o docker-compose.yml
    2. docker compose -f docker-compose.yml up -d

    Once it finishes, run docker ps to confirm all service containers are running. Then open the locally mapped port in your browser (defined in the compose file—3080 is a common example) to access the knowledge base management UI.

    3. Model Preparation and Service Integration

    3.1 Pull the Embedding Model

    The embedding model converts text into semantic vectors and is critical to RAG retrieval quality. A small, multilingual model is sufficient:

    1. ollama pull bge-m3 # Example: multilingual embedding model

    Verify it after pulling:

    1. curl http://localhost:11434/api/embeddings -d '{"model":"bge-m3","prompt":"test text"}'

    A returned vector array means the service is working.

    3.2 Pull the Text Generation Model

    Pick a chat model based on your hardware; a 7B quantized version is the sweet spot for most consumer machines:

    1. ollama pull qwen2.5:7b # Example: adjust size to your hardware

    Test generation quality and response speed interactively in the terminal before deciding whether to switch to a larger or smaller model.

    3.3 Configure Local Models in the Knowledge Base

    Go to the knowledge base application’s settings page and configure the model service address. Because the knowledge base runs inside a Docker container, it must reach the model service on the host using the container-accessible hostname (such as host.docker.internal) rather than localhost:

    • Model service URL: http://host.docker.internal:11434
    • Select the pulled models from the “embedding model” and “generation model” dropdowns, then save.

    After configuration, use the built-in chat test feature to verify connectivity before proceeding with the actual knowledge base workflow: create a knowledge base → upload documents → wait for chunking and vectorization to finish → run Q&A tests.

    4. Validation and Tuning

    Upload a few documents with concrete factual content (such as internal specifications or technical manuals) and evaluate along these dimensions:

    1. Retrieval accuracy: does the answer cite the correct document passages?
    2. Refusal capability: for questions outside the knowledge base, does it correctly answer “no relevant information found” rather than hallucinating?
    3. Response latency: record time-to-first-token and total generation time to judge whether the hardware is adequate.

    If retrieval is inaccurate, adjust chunk size and overlap. If the generated answers are off-topic, try a larger model or tweak the prompt template.

    5. Going Further: Powering Coding Assistants via the Local API

    The local model runtime exposes a standard-compatible inference API, so it can serve not just the knowledge base but also various development tools.

    5.1 Calling the API Directly

    1. curl http://localhost:11434/api/generate -d '{
    2. "model": "qwen2.5:7b",
    3. "prompt": "Write quicksort in Python",
    4. "stream": false
    5. }'

    5.2 Connecting to AI Coding Tools

    Many CLI-based coding assistants support custom model endpoints. As an example with a typical AI coding CLI, point the model base URL to your local service via an environment variable and set the model name to the locally pulled model:

    1. export OPENAI_BASE_URL=http://localhost:11434/v1

    Once configured, the coding assistant’s code generation, explanation, and refactoring requests are all handled by the local model. For classified codebases or any scenario where code must not leave the network, this is a low-cost way to get started. That said, local small models trail flagship cloud models in coding ability—they’re fine for routine tasks, while complex architecture design still needs human leadership.

    6. Summary

    This article walked through a fully offline RAG pipeline built on a local model runtime plus a containerized knowledge base: from dependency installation and path planning, to model selection, service integration, and validation, to powering a coding assistant via the API. The core value of this setup is complete data sovereignty, with hardware requirements flexibly adjusted through model quantization. A natural next step is exploring knowledge-graph-enhanced retrieval to improve accuracy on multi-hop questions.