
Your First RAG App: A Student’s Step-by-Step Guide to Hybrid Search That Actually Works
A plain-language walkthrough of retrieval-augmented generation for students, with a working Python example of hybrid search and the mistakes beginners make most often.
When I started building NyayaGPT, a legal assistant over 34,000+ Indian
legal records, I thought RAG meant "connect a database to ChatGPT."
It is simpler than that, and also easier to get wrong. This guide is
what I wish I had read first.
What RAG means in one minute
Retrieval-augmented generation (RAG) has two steps:
- Retrieve: find the passages in your own documents that are most relevant to the question.
- Generate: give those passages to a language model and ask it to answer using only them.
The model stops guessing from memory and starts reading from your data.
That is why RAG reduces made-up answers and lets you show sources.
The pipeline
Documents -> Clean -> Chunk -> Embed -> Index
|
Question -> Embed -> Search the index -> Top passages -> LLM -> AnswerIndexing happens once. Everything below the line happens on every
question.
Step 1: Clean your documents
Remove page numbers, repeated headers and broken line breaks. If your
data has personal details, anonymize them now. It is far harder to
remove them after they reach the index.
Step 2: Chunk
Models cannot read a whole book per question, so you split documents
into chunks, usually a few sentences to a few paragraphs each.
- Too small: a chunk loses the context it needs to make sense.
- Too large: the chunk contains many topics and matches nothing well.
- Split on sentence or paragraph boundaries, not at a fixed character count. This matters even more for Indian scripts, where cutting mid-sentence badly hurts retrieval.
Step 3: Embed
An embedding model turns each chunk into a list of numbers, so that
chunks with similar meaning end up close together. If your users ask in
several languages, choose a multilingual embedding model. An English-only
model will fail quietly on Hindi or Tamil text.
Step 4: Search, and why one method is not enough
There are two main ways to search:
- Vector (semantic) search finds similar meaning. Good for questions phrased in everyday words.
- Keyword search (BM25) finds exact terms. Good for names, codes and numbers, like a section number.
Each one misses things the other finds. Hybrid search runs both and
merges the results. A simple, reliable way to merge is Reciprocal Rank
Fusion (RRF): each result earns a score based on its rank in each
list, and results that rank well in both rise to the top.
A working example
Install the libraries:
pip install rank_bm25 sentence-transformers numpyimport numpy as np
from rank_bm25 import BM25Okapi
from sentence_transformers import SentenceTransformer
docs = [
"Section 420 deals with cheating and dishonestly inducing delivery of property.",
"A consumer can file a complaint if a seller delivers a defective product.",
"Bail is the temporary release of an accused person awaiting trial.",
]
# Keyword index
bm25 = BM25Okapi([d.lower().split() for d in docs])
# Vector index
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")
doc_vecs = model.encode(docs, normalize_embeddings=True)
def hybrid_search(query, k=3, rrf_k=60):
# Rank by keyword score
kw_scores = bm25.get_scores(query.lower().split())
kw_rank = np.argsort(-kw_scores)
# Rank by vector similarity
q_vec = model.encode([query], normalize_embeddings=True)[0]
vec_rank = np.argsort(-(doc_vecs @ q_vec))
# Reciprocal Rank Fusion
fused = {}
for ranking in (kw_rank, vec_rank):
for position, doc_id in enumerate(ranking):
fused[doc_id] = fused.get(doc_id, 0) + 1 / (rrf_k + position + 1)
best = sorted(fused, key=fused.get, reverse=True)[:k]
return [docs[i] for i in best]
print(hybrid_search("what can I do if a shopkeeper cheated me"))The keyword side catches exact terms. The vector side catches the
meaning of "cheated." RRF combines them without needing to compare their
different score scales.
Step 5: Generate with citations
Pass the top passages to your model with an instruction like:
Answer using only the passages below. After each claim, cite the passage number. If the passages do not contain the answer, say you do not know.
That last sentence is the most important line in your whole system. It
turns "confident guess" into "honest refusal."
Mistakes beginners make
- Judging the model when retrieval is the problem. If the right passage never reaches the model, no model can answer correctly. Print your retrieved passages and read them.
- No evaluation. Write 20 real questions with known answers and check whether the right passage appears in the top results. Do this per language if you support more than one.
- Skipping the "I don't know" rule. Without it, the model fills gaps with invented text.
- Indexing sensitive data unprotected. Anonymize before indexing.
- Chunking blindly. Look at your chunks. If they read badly to you, they retrieve badly too.
Where to go next
- Try a re-ranking step that re-scores the top 20 results more carefully
- Add metadata filters, such as document type or year
- Build a small test set and track your retrieval accuracy over time
Final thought
Good RAG is mostly good retrieval. Spend your time on cleaning, chunking
and testing, and the generation step becomes the easy part.
If you build your first RAG app after reading this, share it in the
comments. I would love to see it.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article