Build a local RAG pipeline in 30 lines of Python (no Docker, no API keys)
Containers

Build a local RAG pipeline in 30 lines of Python (no Docker, no API keys)

📅 March 30, 2026 ⏱ 3 min read 📡 dev.to

Build a Local RAG Pipeline in 30 Lines of Python (No Docker, No API Keys)

Stop sending your sensitive documents to ChatGPT. Build a completely local Retrieval-Augmented Generation (RAG) system that runs on your laptop, processes your private data securely, and costs zero dollars per query.

Why This Matters

Privacy-conscious developers and organizations need AI capabilities without data leaving their infrastructure. Traditional RAG solutions require expensive API calls to OpenAI, complex Docker setups, or cloud dependencies that leak sensitive information. A local RAG pipeline gives you:

How To Build It

This implementation uses Ollama for the language model and basic Python libraries for document processing and vector search. No GPU required, though it helps with speed.

Step 1: Install Dependencies


# Install Ollama (handles local LLM inference)
curl -fsSL https://ollama.ai/install.sh | sh

# Pull a lightweight model (3.8GB download)
ollama pull llama3.2:3b

# Install Python dependencies  
pip install sentence-transformers faiss-cpu PyPDF2

Step 2: The Complete RAG Pipeline (30 lines)


import faiss
import numpy as np
from sentence_transformers import SentenceTransformer
import requests
import json
import PyPDF2
import os

class LocalRAG:
    def __init__(self):
        self.encoder = SentenceTransformer('all-MiniLM-L6-v2')
        self.index = faiss.IndexFlatIP(384)  # 384 is embedding dimension
        self.chunks = []
    
    def add_document(self, filepath):
        with open(filepath, 'rb') as f:
            reader = PyPDF2.PdfReader(f)
            text = ''.join(page.extract_text() for page in reader.pages)
        
        # Split into chunks (simple sentence splitting)
        chunks = [s.strip() for s in text.split('.') if len(s.strip()) > 20]
        embeddings = self.encoder.encode(chunks)
        
        self.index.add(embeddings.astype('float32'))
        self.chunks.extend(chunks)
    
    def query(self, question, k=3):
        question_embedding = self.encoder.encode([question])
        scores, indices = self.index.search(question_embedding.astype('float32'), k)
        
        context = '\n'.join([self.chunks[i] for i in indices[0]])
        prompt = f"Context: {context}\n\nQuestion: {question}\nAnswer:"
        
        response = requests.post('http://localhost:11434/api/generate', 
                               json={'model': 'llama3.2:3b', 'prompt': prompt, 'stream': False})
        return json.loads(response.text)['response']

# Usage
rag = LocalRAG()
rag.add_document('your_document.pdf')
answer = rag.query('What is the main topic of this document?')
print(answer)

Step 3: Test Your Pipeline


# Start Ollama server (if not already running)
ollama serve &

# Run your RAG script
python rag_pipeline.py

Best Practices

Common Pitfalls

Memory issues with large documents — The simple chunking strategy loads entire PDFs into memory. For large document sets, implement streaming chunk processing or use document databases like ChromaDB.

Poor retrieval relevance — Sentence-splitting creates uneven chunk sizes and breaks context. Use libraries like langchain text splitters or implement semantic chunking based on topic boundaries.

Model hallucination — Even with context, local models may generate plausible-sounding but incorrect answers. Always implement confidence scoring and source citation in production systems.

Ollama connection errors — Ensure Ollama server is running on port 11434. Check with curl http://localhost:11434/api/tags before running your RAG pipeline.

Related DevToolKit Tools

Enhance your RAG development workflow with these complementary tools:

This local RAG setup gives you complete control over your AI pipeline while maintaining privacy and avoiding vendor lock-in. Start with this foundation and iterate based on your specific use case requirements.

Try These Tools for Free

54 developer & DevOps tools — right in your browser. No sign-up needed.

Explore All Tools →

Related DevToolKit Tools