Vineet Ganti
Finding the right text: embeddings, reranking, chunking and HyDE
All posts

AI & LLMs/Agentic AI · Part 3 of 14

·24 min read

Finding the right text: embeddings, reranking, chunking and HyDE

Before an agent can answer from your documents it has to find the right few sentences. Word vectors, sentence embeddings and cosine similarity, a fast search followed by a careful reranker, three ways to cut a document into chunks, and HyDE, all re-run on my laptop with the model calls on Groq and on Claude.

Agentic AIRAGEmbeddingsLangChainAI Engineering

An agent that answers from your own documents never reads the whole pile. It searches, takes the few pieces that look most relevant, and puts only those in front of the model. If the search brings back the wrong pieces, the best model in the world answers the wrong question. This post is about that search step: how text becomes numbers, how those numbers are compared, how a second, slower model fixes the first one's ranking, how a document is cut into pieces, and a trick called HyDE that searches with a made-up answer.

#TL;DR

  • Embeddings turn text into lists of numbers whose direction carries meaning. Cosine similarity compares the directions: "fruit and nutrition" scored 0.71 against "Apples and oranges are healthy fruits", 0.47 against a banana sentence that shares no word with it, and 0.10 against cricket. ✅
  • Search in two stages. A bi-encoder embeds every document once and finds candidates in milliseconds; a cross-encoder reads the question and each candidate together and reorders the short list. The reranker moved the sentence that actually lists COVID-19 symptoms from 4th to 1st. ✅
  • A reranker judges relevance, not truth. A false sentence saying COVID-19 causes memory loss rose to 2nd after reranking. ✅
  • Chunk size decides what can be found. At 50 characters the best hit was a fragment, at 150 a whole sentence, at 400 half the article. Splitting at headings keeps a heading with its text and records where each chunk came from. ✅
  • HyDE searches with a made-up answer. The plain query matched one product at 1.59; with a model's fake product titles added, Groq found three running shoes (top 8.09). Two Claude runs wrote different fake titles and got different top threes, and one of them pulled a casual shoe in. ✅
  • Semantic chunking depends on the embedding model. With the local model I used, it cut the sample into two chunks, not one per topic. ✅

#Where the code came from, and how I ran it

The folder is Bootcamp_14th & 15th Nov, with a 1_GIVE_FIRST folder of exercises with TODOs and a 2_SHARE_AFTER folder of solutions. I ran the solutions. Module 1 needs no model API at all: GloVe word vectors, and two small models from Hugging Face that run on the CPU. Module 2 makes one model call (the HyDE step) and uses an embedding model for semantic chunking.

The course code calls OpenAI (gpt-6-luna and text-embedding-3-small). I have no OpenAI credits, so every lab in this series now goes through one small file, llm.py, with a switch: LLM_PROVIDER=groq uses openai/gpt-oss-120b on Groq, and LLM_PROVIDER=claude uses Claude Haiku 5.5 through my Claude subscription. Neither has an embeddings endpoint, so both use the local all-MiniLM-L6-v2 model, which is the same model module 1 already uses. The tools post explains the switch and what it had to fake.

LabelMeaning
✅I ran it on 2026-11-18; the output is from my run
📘From Shivank's explainer or the library docs; not re-run by me

The modules ran in a Python 3.12 virtual environment. Timings from my run log: the similarity app took 22 s and the rerank app 53 s on their first run, nearly all of it downloading models; the chunking notebook took 32 s on Groq and 36 s on Claude. ✅

#Part 1: words as numbers

Data

Computers compare numbers, not meanings. An embedding is a list of numbers produced by a model trained on a lot of text, arranged so that texts used in similar ways end up with similar numbers. No single number means "royal" or "fruit"; the pattern across all of them does.

#GloVe: one vector per word

The notebook loads GloVe vectors through gensim: 400,000 words, 50 numbers each, learned from Wikipedia and news text.

python1_word_embeddings.ipynb (solution)
model = api.load("glove-wiki-gigaword-50")
v_king = model["king"]                       # word in, vector out, like a dictionary
model.most_similar("king", topn=5)
model.most_similar(positive=["king", "woman"], negative=["man"], topn=1)
output
Shape of the 'king' vector: (50,)
First 5 numbers: [ 0.50451   0.68607  -0.59517  -0.022801  0.60046 ]
prince 0.824 · queen 0.784 · ii 0.775 · emperor 0.774 · son 0.767
king - man + woman = [('queen', 0.8523604273796082)]
paris - france + india = [('delhi', 0.8888585567474365)]

✅ The nearest words to king are other royals, and word maths works: start at king, remove the man direction, add the woman direction, and the closest word is queen. The model learned a "capital of" direction too, from reading alone.

Two things in the same notebook keep that in proportion:

  • ✅ Doing the maths by hand returns the starting word. king − man + woman computed with numpy and searched directly gives king 0.886, then queen 0.861. The vector barely moved; most_similar(positive=…, negative=…) quietly drops the input words from its answers, which is why it shows queen.
  • ✅ Not every subtraction means something. A cell that searches india − bengali returns hopes, bid, korea, united, ahead. Word maths finds directions that the training text happened to create, and many of them are noise.

#Cosine similarity, written by hand

python1_word_embeddings.ipynb (solution)
def cosine_similarity(a, b):
    return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)))
output
   king vs queen     -> my answer: 0.784 | gensim: 0.784
cricket vs football  -> my answer: 0.696 | gensim: 0.696
   king vs banana    -> my answer: 0.221 | gensim: 0.221

The dot product is large when two lists rise and fall together; dividing by both lengths removes size, so only direction is compared. The result runs from −1 to 1. Direction is what matters because a long and a short sentence about the same thing point the same way but have very different lengths. ✅

#Sentence embeddings: the similarity app

Single words are not enough for search, so the next app embeds whole sentences with all-MiniLM-L6-v2, which turns any sentence into 384 numbers and runs on a laptop CPU.

python2_similarity_search_app.py (solution, the search itself)
doc_embeddings = model.encode(docs)                       # one row of 384 numbers per line
query_embedding = model.encode([query.strip()])[0]
scores = [(doc, cosine_similarity(query_embedding, emb)) for doc, emb in zip(docs, doc_embeddings)]
scores.sort(key=lambda pair: pair[1], reverse=True)

The app is a Gradio page, so I called its retrieve_top_k function directly with the page's own sample documents instead of clicking:

QueryTop results (score)
fruit and nutrition (k=3)Apples and oranges are healthy fruits (0.71) · Bananas are a great source of potassium (0.47) · Virat Kohli scored a century in the final (0.10)
stock market news (k=2)The stock market crashed today (0.61) · Sensex fell 800 points after the RBI announcement (0.25)
cricket (k=1)Virat Kohli scored a century in the final (0.40)

✅ The banana sentence comes second although it shares no word with "fruit and nutrition". Keyword search could not do that. The Sensex sentence shares no word with "stock market news" either and still beats the unrelated sentences. Notice also that k=3 always returns three results, however weak the third is: the cricket sentence at 0.10 is not an answer. A real system needs a score threshold, not just a count.

#Part 2: retrieve fast, then rerank carefully

Data

The model in Part 1 is a bi-encoder: it embeds the question and each document separately. That is what makes it fast, because the document vectors can be computed once and stored. It is also its weakness: it never sees the question and the document together.

A cross-encoder reads the pair as one input and outputs a relevance score. Every word of the question can be matched against every word of the document, so it is far more accurate, but nothing can be precomputed: one full model run per pair, per question. So it only ever sees the bi-encoder's short list.

python3_reranking_app.py (solution)
bi_encoder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
cross_encoder = CrossEncoder("cross-encoder/ms-marco-MiniLM-L6-v2")   # trained on real web searches

def rerank(query, documents):
    scores = cross_encoder.predict([(query, doc) for doc in documents])
    return sorted(zip(documents, scores), key=lambda pair: pair[1], reverse=True)

The app ships with eight sentences built to trip a vector search: several that merely repeat the query words, two real descriptions, one about dementia, and one false claim that COVID-19 causes memory loss. Retrieving all eight and reranking them:

SentenceBi-encoder (cosine)Cross-encoder
COVID-19 has many symptoms.0.92 (1st)7.17 (5th)
COVID-19 symptoms are not nice0.87 (2nd)6.83 (7th)
COVID-19 symptoms are bad.0.86 (3rd)7.66 (4th)
COVID-19 symptoms can include: a high temperature… a new, continuous cough; a loss or change to your sense of smell…0.86 (4th)10.42 (1st)
COVID-19 is a disease caused by a virus. The most common symptoms are fever, chills…0.83 (5th)9.81 (3rd)
COVID-19 symptoms are really bad.0.82 (6th)7.08 (6th)
COVID-19 has the following symptom: memory loss, poor judgment, and confusion.0.66 (7th)9.84 (2nd)
Dementia has the following symptom: memory loss, poor judgment, and confusion.0.28 (8th)−9.93 (8th)

✅ Three things in that table:

  1. Repeating the query words is enough to win a vector search. "COVID-19 has many symptoms." tells you nothing and came first.
  2. The reranker fixed that. The sentence that lists symptoms moved from 4th to 1st; the empty ones dropped.
  3. The reranker promoted a falsehood. The memory-loss sentence went from 7th to 2nd. It has the right topic and the shape of an answer, and that is exactly what relevance means. The explainer predicted it would "usually not sink to the bottom"; on my run it did worse than not sinking. Checking whether something is true needs trusted sources in the knowledge base, not a better ranker.

Cross-encoder scores are not between 0 and 1 and can be negative; only the order matters. And the reranker can only reorder what retrieval gives it: with k=2 here it could never have found the best sentence, which retrieval put 4th. In practice you retrieve 20 to 50 and rerank down to 3 to 5. 📘

#Part 3: cutting documents into chunks

A retrieval system never sends a whole document to the model; it sends the few chunks that match. Chunks are the units of knowledge your agent can find, so where you cut decides what can be found.

#Size

pythonchunking_strategies.ipynb (solution, part 2.1)
text_splitter = RecursiveCharacterTextSplitter(chunk_size=size, chunk_overlap=int(size * 0.1))
chunks = text_splitter.split_documents(documents)
db = Chroma.from_documents(chunks, embeddings, collection_name=f"heart_chunks_{size}",
                           ids=[f"{size}-{i}" for i in range(len(chunks))])

RecursiveCharacterTextSplitter tries paragraph breaks first, then line breaks, then spaces, and only cuts inside a word as a last resort. Each size gets its own Chroma collection, and the explicit ids mean running the cell twice updates the same chunks instead of storing duplicates. The 10% overlap repeats the edge text, so a sentence cut at a boundary still appears whole in one chunk. ✅

SizeChunksTop result for "What is the primary function of the heart…?"
5016"the body and removes metabolic waste. The heart"
1506"The human heart is a muscular organ that pumps blood through the circulatory system."
4002four sentences of the article in one block

✅ Identical on both runs, because nothing in this step calls a chat model. There is no universal best size: the explainer suggests starting around 500 to 1,000 characters with 10 to 20% overlap for real documents, then testing with real questions. 📘

#Headings

A pure fixed-length splitter (separator="") cuts every 100 characters, even inside words: ✅ my run printed …that enable / s computers… and ## Types of M / achine Learning. Allowing cuts only at line breaks fixes the words, not the structure:

pythonchunking_strategies.ipynb (solution, part 2.2)
fixed_length_splitter = CharacterTextSplitter(chunk_size=200, chunk_overlap=20, separator="\n")

✅ Five chunks of 184, 174, 139, 198 and 106 characters, and chunk 3 ends with the heading ### Unsupervised Learning while its text starts chunk 4. A search for "what is clustering?" would find chunk 4 with no heading to say what it is about.

Splitting at the headings themselves gives one chunk per section and keeps its address:

pythonchunking_strategies.ipynb (solution, part 2.4)
markdown_splitter = MarkdownHeaderTextSplitter(headers_to_split_on=headers_to_split_on)
md_header_splits = markdown_splitter.split_text(text)
output
Chunk 3:
Unsupervised learning finds hidden patterns in data without labeled examples: …
Metadata: {'Header 1': 'Machine Learning Fundamentals', 'Header 2': 'Types of Machine Learning', 'Header 3': 'Unsupervised Learning'}

✅ The metadata can be shown as the answer's source ("Types of Machine Learning → Unsupervised Learning"), used as a filter, or prepended to the chunk before embedding so the vector knows the topic. For a long policy PDF with headings, the explainer's advice is structure first, then a recursive splitter for any section that is still too long. 📘

#Semantic chunking, and a result that differed from the course

Semantic chunking cuts where the meaning jumps: embed every sentence, measure the distance between neighbours, and start a new chunk where a gap is unusually large (by default, larger than 95% of all gaps).

pythonchunking_strategies.ipynb (solution, part 2.3, as ported)
semantic_splitter = SemanticChunker(provider.make_embeddings())   # the course used OpenAI text-embedding-3-small
docs = semantic_splitter.create_documents([text])

✅ With the local all-MiniLM-L6-v2 model, my run produced two chunks: the title and first sentence, then everything else (supervised, unsupervised and reinforcement learning in one block). The explainer, with OpenAI's larger embedding model, describes "chunks of different lengths, each about one topic". 📘 So the cut points are a property of the embedding model, not just the text: a smaller model sees the three learning types as one topic. That is worth knowing before switching embedding models under an existing index.

The notebook also printed a deprecation warning: langchain-experimental is being sunset. The method is four steps and easy to write yourself if the package goes away.

#Part 4: HyDE, searching with a made-up answer

Request or acknowledgmentData

Questions and documents are written in different words. A shopper types "I want running shoes"; the catalogue says trainers, Pegasus, Ultraboost. HyDE (Hypothetical Document Embeddings) asks a model to write a fake answer in the documents' own style, then searches with that. The notebook uses BM25 keyword scoring so the effect is easy to see.

pythonchunking_strategies.ipynb (solution, part 2.5)
hyde_prompt = (
    "You write product titles for an online shoe store.\n"
    f"A shopper searched for: '{shopper_query}'.\n"
    "Write 4 short product titles (brand, model, key feature) that would perfectly match this search. "
    "One title per line, no numbering."
)
hyde_answer = llm.invoke(hyde_prompt).text
scores_hyde = bm25.get_scores((shopper_query + " " + hyde_answer).lower().split())

Without HyDE, ✅ one product matched: Adidas Ultraboost sneakers for men, Marathon Shoes (1.59), and only because its title contains Shoes. With HyDE, every run wrote different fake listings and got a different top three. I ran Claude twice (the first run was before an unrelated fix to my adapter, described in the tools post; it doesn't touch this prompt):

RunMade-up titlesTop three (BM25 score)
GroqNike Air Zoom Pegasus · Adidas Ultraboost 22 · Brooks Ghost 15 · ASICS Gel-Kayano 30Nike Air Zoom Pegasus trainers (8.09) · Adidas Ultraboost (6.32) · Asics Gel-Kayano (4.45)
Claude, run 1Nike Pegasus 41 · ASICS Gel-Kayano 31 · Brooks Ghost 16 · Hoka Clifton 9 "…for Everyday Comfort"Adidas Ultraboost (11.06) · Asics Gel-Kayano (6.67) · Puma casual footwear for everyday wear (5.49)
Claude, run 2Brooks Ghost 16 · ASICS Gel-Kayano 31 · Nike Pegasus 41 "…for All-Day Comfort" · Hoka Clifton 9Asics Gel-Kayano (6.67) · Nike Air Zoom Pegasus (4.04) · Adidas Ultraboost (2.37)

✅ Every run found real running shoes the plain query missed. But Claude's titles were long and descriptive, and in run 1 the word everyday matched a casual Puma shoe into third place; in run 2 the same kind of title said All-Day, which matches nothing, and all three were running shoes. The search result depends on which words a model happens to write. The fake answer is never shown to the user, so being wrong is fine; being wordy is not, because every word is a search term. The original paper embeds the fake answer and runs a vector search with it; the same code shape works with db.similarity_search(hyde_answer). 📘 HyDE costs one extra model call per question, so it earns its place on short or vaguely worded questions.

#Reproduce this yourself

  1. ✅ Create a Python 3.12 virtual environment and install gensim, sentence-transformers, scikit-learn, gradio, langchain, langchain-text-splitters, langchain-chroma, langchain-huggingface, langchain-experimental, chromadb, rank_bm25 and python-dotenv. The first run downloads GloVe (66 MB) and the two small models.
  2. ✅ Module 1 needs no API key. Run 1_word_embeddings.ipynb top to bottom, then python 2_similarity_search_app.py and python 3_reranking_app.py and open the printed address (Gradio defaults to port 7860; pass server_port=7861 if something already uses it).
  3. ✅ Module 2 needs one chat model for HyDE. With the course code, that is an OpenAI key in .env. With my port, put GROQ_API_KEY in .env, or sign in to Claude Code and set LLM_PROVIDER=claude.
  4. ✅ To compare providers, run the notebook twice with jupyter nbconvert --to notebook --execute and a different LLM_PROVIDER each time; only the HyDE cells change.
  5. 📘 For your own documents, start with LangChain's text splitters and test chunk sizes against real questions before tuning anything else.

#Things that can trip you up

  • Top-k always returns k results. The third "match" for fruit was a cricket sentence at 0.10. Add a minimum score.
  • Word overlap wins vector searches too. Sentences that repeat the query words float to the top; a reranker fixes most of that.
  • Relevance is not truth. The reranker ranked a false medical claim second. Curate what goes into the knowledge base.
  • A heading can end up in the wrong chunk with fixed-length splitting. Split at headings when the document has them.
  • Changing the embedding model changes your chunks if you chunk semantically, and invalidates every stored vector either way.
  • Storing chunks without ids duplicates them on every re-run, and the same chunk then fills your top three.
  • HyDE's fake answer is a search query. Wordy fake answers pull in wrong neighbours; ask for short titles.
  • On Windows, Hugging Face prints a warning about symlinks on first download. It is harmless; the cache just uses more disk.

#What I learned

The step before the model is where most answers are won or lost. Three of my results came out differently from the course's descriptions, and each one taught something: the reranker promoted a falsehood rather than merely keeping it, semantic chunking collapsed to two chunks with a smaller embedding model, and HyDE's top three changed from run to run with the words the model happened to choose.

#What I'd do differently

Measure retrieval on its own before wiring in a model: a handful of questions with the chunk each should find, and a count of how often it is in the top three. Every number in this post came from eyeballing one query; the evaluation post builds exactly that kind of test as Recall@k.

#Summary

Retrieval turns text into vectors and compares their directions. A fast bi-encoder narrows millions of chunks to a short list and a cross-encoder reorders it, but neither knows what is true. How a document is cut decides what can be found at all: too small splits ideas, too large buries them, and headings make the best cut points. HyDE helps when questions and documents use different words, at the price of one model call and some risk of wrong neighbours.

#Concepts at a glance

TermIn one line
EmbeddingA list of numbers for a piece of text, arranged so similar meanings point the same way
Cosine similarityCompares the direction of two vectors; 1 is the same meaning, 0 unrelated
Bi-encoderEmbeds query and documents separately; fast, precomputable
Cross-encoderScores a (query, document) pair together; accurate, slow, used to rerank
ChunkOne piece of a document, the unit a retriever returns
OverlapText repeated at chunk edges so a cut sentence still appears whole
Semantic chunkingCutting where the meaning between neighbouring sentences jumps
BM25Classic keyword scoring, counting rare shared words
HyDESearching with a model-written fake answer instead of the question

#Test yourself

Why does searching with the raw vector for king − man + woman return king?

The arithmetic moves the vector only a little, so it is still nearest to king. most_similar(positive, negative) removes the input words from its answers; a plain nearest-neighbour search doesn't.

Why not run the cross-encoder over every document?

It runs the full model once per (question, document) pair, every time someone asks. A million documents would be a million model runs per question. The bi-encoder's document vectors are computed once and reused.

The reranker put "COVID-19 causes memory loss" second. What would actually keep it out of answers?

Not a better ranker: it really is relevant to the query. Keep false or unreviewed text out of the knowledge base, or check claims against trusted sources.

You have a 300-page HR policy with headings. How would you chunk it?

Split at the headings first, keeping them as metadata, then split any section that is still too long with a recursive splitter. The headings also make answers easy to cite.

HyDE's fake answer is often wrong. Why is that acceptable, and what still matters?

It is never shown to anyone; it only supplies search words. What matters is that its words match the real documents and not their neighbours: short, catalogue-style titles worked better than long descriptive ones.

#Sources