Building a Local RAG System: Hybrid Retrieval Approach

Listen to this Post

Featured Image

Source:

Shantanu Ladhwe demonstrates how to build a fully local RAG (Retrieval-Augmented Generation) system without cloud dependencies. The system leverages hybrid retrieval (BM25 + semantic search) for efficient document processing and question-answering.

Key Components

1. Streamlit App

  • Simple UI for file uploads, queries, and responses.
    pip install streamlit 
    streamlit run app.py 
    

2. OCR (PyTesseract)

  • Converts PDFs/images to text.
    sudo apt install tesseract-ocr  Linux 
    pip install pytesseract pillow 
    

3. Ingestion Pipeline

  • Cleans, chunks, and enriches text with embeddings.
    from langchain.text_splitter import RecursiveCharacterTextSplitter 
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) 
    

4. Vector Store (OpenSearch)

  • Stores embeddings for hybrid search.
    docker run -p 9200:9200 -e "discovery.type=single-node" opensearchproject/opensearch 
    

5. Hybrid Search (BM25 + Semantic)

  • Combines keyword and vector search.
    from opensearchpy import OpenSearch 
    client = OpenSearch("http://localhost:9200") 
    

6. Prompt Template

  • Structures LLM inputs for better responses.
    template = """Answer based on context: {context}\nQuestion: {question}""" 
    

7. Local LLM (Ollama)

  • Runs models like LLaMA, Mistral locally.
    ollama pull llama3 
    ollama run llama3 
    

You Should Know:

1. Setting Up OpenSearch for Hybrid Search

 Install & run OpenSearch 
docker pull opensearchproject/opensearch 
docker run -d -p 9200:9200 -e "discovery.type=single-node" opensearchproject/opensearch 

Verify:

curl -X GET "http://localhost:9200" 

2. Running Ollama for Local LLM

 Install Ollama 
curl -fsSL https://ollama.com/install.sh | sh 
 Run a model 
ollama pull mistral 
ollama run mistral 

3. Hybrid Search Query Example

from opensearchpy import OpenSearch 
client = OpenSearch("http://localhost:9200")

query = { 
"query": { 
"hybrid": { 
"queries": [ 
{"match": {"text": "RAG systems"}},  BM25 
{"knn": {"embedding": {"vector": [0.1, 0.2, ...], "k": 5}}}  Semantic 
] 
} 
} 
} 
response = client.search(index="documents", body=query) 

4. Streamlit UI for RAG

import streamlit as st 
st.title("Local RAG System") 
uploaded_file = st.file_uploader("Upload a PDF") 
if uploaded_file: 
text = extract_text(uploaded_file) 
st.write("Extracted Text:", text[:500]) 

What Undercode Say:

Hybrid RAG systems offer cost efficiency, privacy, and control by avoiding cloud dependencies. Key takeaways:
– Start small (local LLMs + OpenSearch).
– Optimize retrieval (BM25 + embeddings).
– Experiment with models (Mistral, LLaMA, Phi).

Expected Output:

A fully functional RAG system running locally with hybrid search, OCR support, and a Streamlit UI.

Prediction:

Hybrid RAG will dominate enterprise AI due to its balance of accuracy and cost, reducing reliance on expensive agentic frameworks.

References:

Reported By: Shantanuladhwe 90 – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram