Introduction
A model’s training data is not necessarily public-only, and its full contents may not be known. The practical issue is that your private or current documents may not be reliably reflected in its answers. RAG retrieves relevant documents at question time and supplies them to a model, but this does not guarantee a correct answer.
Local processing and confidential data
This example demonstrates local inference and storage. Check initial model downloads, logs, backups, network exposure, and telemetry; do not enter confidential documents without organizational approval. RAG also does not guarantee answer accuracy.
1. Overall Architecture of RAG
RAG is not a single AI model, but a system architecture where multiple components work together. It broadly consists of two phases: the “Ingestion (Data Loading) Phase” and the “Retrieval & Generation Phase”.
The Mermaid diagram below shows the overall picture of a RAG system.
graph TD
subgraph "Ingestion Phase (Preparation)"
Doc["Custom Documents (PDF, TXT, etc.)"] --> Loader["Document Loader"]
Loader --> Splitter["Text Splitting (Chunking)"]
Splitter --> EmbedModel1["Embedding Model"]
EmbedModel1 --> VectorDB["Vector Database"]
end
subgraph "Inference Phase (User Query)"
User["User Question (Query)"] --> EmbedModel2["Embedding Model"]
EmbedModel2 --> QueryVector["Query Vector"]
QueryVector --> Search["Similarity Search (Vector Search)"]
VectorDB --> Search
Search --> Context["Relevant Chunk Extraction (Context)"]
User --> PromptBuilder["Prompt Builder"]
Context --> PromptBuilder
PromptBuilder --> LocalLLM["Local LLM"]
LocalLLM --> Answer["Final Answer Generation"]
end
Ingestion Phase (Preparation)
- Document Loading: Loads unstructured data such as PDFs, Word documents, and text files.
- Chunking (Text Splitting): Splits long texts into meaningful chunks to fit within the LLM’s input limit (context window) and to improve search accuracy.
- Embedding (Vectorization): Inputs the split chunks into an Embedding Model and converts them into an array of numerical values (vectors) with hundreds to thousands of dimensions.
- Saving to Database: Saves the converted vectors and their associated original text data into a Vector Database (Vector DB).
Inference Phase (Runtime)
- Query Vectorization: Vectorizes the user’s question text using the same embedding model used in the preparation phase.
- Similarity Search: Performs a similarity calculation between the query vector and the document vectors in the database, and retrieves the top few semantic (highly relevant) text chunks.
- Prompt Building: Combines the retrieved relevant text as “context (background knowledge)” with the user’s question text to create an input prompt for the LLM.
- Answer Generation: The LLM, receiving the augmented prompt, generates an answer based on the provided context information.
2. Deep Understanding of Vector Search and Embeddings
At the core of RAG is “Vector Search (Semantic Search)”. While traditional keyword search (like BM25) is based on exact word matching and frequency, vector search is based on “semantic similarity”. For example, even if the words are different, like “dog” and “puppy”, or “PC” and “computer”, they will be hit in the search if their meanings are close.
What is an Embedding Model?
Vector dimensions and input limits vary by model. multilingual-e5-large produces 1,024 dimensions and accepts up to 512 tokens. Prefix documents with passage: and questions with query: .
In this multi-dimensional space (latent space), the model is trained so that sentences with similar meanings are closer in distance in the coordinate space.
Mathematical Background of Similarity Calculation: Cosine Similarity
When a vector database searches for relevant documents, the most commonly used distance metric is Cosine Similarity. Unlike Euclidean distance (absolute spatial distance), cosine similarity focuses on the “angle between two vectors”. Since it is less affected by the length of the sentence (the norm of the vector), it is highly suitable for calculating text similarity.
Expressed mathematically, the cosine similarity between vectors \(\mathbf{A}\) and \(\mathbf{B}\) is as follows:
\[ \text{Cosine Similarity}(\mathbf{A}, \mathbf{B}) = \cos(\theta) = \frac{\mathbf{A} \cdot \mathbf{B}}{\|\mathbf{A}\| \|\mathbf{B}\|} = \frac{\sum_{i=1}^{n} A_i B_i}{\sqrt{\sum_{i=1}^{n} A_i^2} \sqrt{\sum_{i=1}^{n} B_i^2}} \]- \(\mathbf{A} \cdot \mathbf{B}\) represents the Dot Product.
- \(\|\mathbf{A}\|\) represents the L2 norm (length) of vector \(\mathbf{A}\).
- \(n\) is the number of dimensions of the vector.
Cosine similarity takes a value from -1 to 1. A value near 0 does not necessarily mean semantic unrelatedness, and a value near -1 does not necessarily mean opposite meaning.
- 1: vectors point in the same direction
- 0: vectors are orthogonal
- -1: vectors point in opposite directions
Chroma uses HNSW in its single-node configuration, but vector databases do not all use the same index. Millions of records are not guaranteed to be searchable in milliseconds.
3. Technology Stack for Building Local RAG
To build a fully local RAG that does not rely on the cloud, we leverage the open-source ecosystem. The recommended technology stack is introduced below.
- Large Language Model (LLM)
- Tools:
OllamaorLlama.cpp - Models: Lightweight, high-performance open models like
Llama-3-8B-Instruct,Gemma-2-9B-It,Qwen2-7B-Instruct. For Japanese tasks, Japanese-tuned models likeLlama-3-ELYZA-JP-8Bare suitable.
- Tools:
- Embedding Model (Embedding)
- Models:
intfloat/multilingual-e5-largeorBAAI/bge-m3. When running locally, it is common to download them from Hugging Face and run them with Sentence-Transformers.
- Models:
- Vector Database (Vector DB)
ChromaDB: Python-based and extremely easy to set up. Ideal for local development.FAISS: A fast vector search library developed by Meta.Qdrant/Milvus: For larger scale and production environments.
- Orchestration Framework
LangChain: The de facto standard for chaining components together.LlamaIndex: A data connection framework specifically specialized for RAG.
This tutorial calls the libraries directly and does not use LangChain. If you choose a framework, check its current official guide and package versions.
4. Implementation Tutorial: Building a Full Local RAG with Python
From here, we will build a local RAG while actually writing Python code. Please install Ollama on your PC in advance and have it running in the background. Also, pull a model on Ollama (e.g., ollama pull qwen3:8b).
Step 1: Installing Required Libraries
| |
Step 2: Complete Implementation Code
Below is the complete Python script to load a PDF file, vectorize it, and have the local LLM answer questions.
| |
Implementation notes
The previous example relied on the older LangChain RetrievalQA chain and Ollama wrapper, so it now uses the current Chroma, Sentence Transformers, and Ollama Python APIs directly. It applies E5’s query: / passage: prefixes, stores page numbers, uses a local database, and removes old chunks when re-indexing the same PDF. Answers show source locations, but you must check that the sources support them. A 400-character chunk is not the same as 512 tokens; tune it for your documents.
5. Advanced RAG Techniques
The basic RAG system (Naive RAG) built in the above tutorial works, but if high answer accuracy is required in a production environment, the introduction of advanced techniques like the following becomes necessary.
Their effects depend on the data and questions. Measure recall, precision, and latency on representative queries before and after adoption.
5.1 Hybrid Search
While vector search is good at capturing “meaning”, it can be poor at strict keyword searches like “specific proper nouns”, “product model numbers”, and “employee IDs”. Therefore, by running semantic search via vector search and keyword search using algorithms like BM25 in parallel, and integrating both results by scoring them (using techniques like Reciprocal Rank Fusion; RRF), you can drastically reduce search misses.
5.2 Re-ranking
Vector search is fast, but it does not necessarily evaluate the exact contextual relevance of the context. A general pipeline for improving search accuracy is as follows:
- First-stage Retrieval: Retrieve a broad and shallow range of relevant chunks (about 20-30) from the Vector DB.
- Re-ranking: Use another heavier machine learning model called a Cross-Encoder (e.g.,
bge-reranker) to input pairs of the user’s query and the retrieved chunks, and recalculate their semantic relevance scores. - Selection: Pass only the top 3-5 with the highest scores as the final context to the LLM prompt.
Reranking can reorder candidates, but it does not guarantee more accurate answers. Evaluate it on your data and queries.
graph LR
Query["Query"] --> VSearch["Vector Search (Top 20)"]
VSearch --> Reranker["Re-ranker Model (Cross-Encoder)"]
Query --> Reranker
Reranker --> TopK["High Precision Top 3"]
TopK --> LLM["LLM Generation"]
5.3 Semantic Chunking and Parent Document Retrieval
Instead of mechanically splitting text by a fixed number of characters, there is a technique called “Semantic Chunking” that uses AI to detect shifts in meaning and splits the text accordingly. Also, in the “Parent Document Retriever” technique, you vectorize in very small units (like sentences) for high-precision search, but when passing it to the LLM, you provide the “original large paragraph (parent document)” containing that sentence, thus providing sufficient context to the LLM.
6. Challenges and Countermeasures when Operating Local RAG
There are unique hurdles when building and operating RAG in a local environment.
- Memory: An 8B model in FP16 uses about 16 GB for weights alone, with additional runtime and KV-cache memory. Quantized speed and quality vary; an 8 GB GPU is not guaranteed to be fast enough.
- Context Window Limits: If the amount of retrieved context is too large, it may exceed the LLM’s input limit (token limit), or the model might forget the middle part of the information (Lost in the middle phenomenon). Adjusting the number of chunks to extract and carefully selecting them through the aforementioned re-ranking techniques are essential.
- Data Freshness Management: When a source document is updated, the corresponding document’s vector in the vector database also needs to be updated or deleted (CRUD operations). Since ChromaDB supports updates based on document IDs, it is practical to manage file hash values and set up a batch process to sync only the differences.
Conclusion
RAG supplies retrieved passages to a model; it does not guarantee correct answers or privacy. Check citations, logs, backups, first-time downloads, outbound connections, and access permissions before use.
![Featured image of post [RAG Implementation Guide] How to Load Your Own Documents into Local AI](/p/rag-local-ai-implementation-guide/img/eyecatch_hu11218043209250547443.jpg)