Generative AIby Dhruv Narang

What the Vector Database Stores

A normal database stores text and numbers. A vector database also stores the vector, so we can search by meaning. But the vector alone is not enough, so each chunk is kept as one row with four parts.

ID
chunk_001 A name for the row, so we can point to it.
Vector
[-0.0069, -0.053, 0.04 …] The numbers from the embedding model. We search with this.
Chunk text
“RAG combines a retriever with a generator …” The words. We never search with them, but this is what we give to the LLM.
Metadata
source: rag_paper.pdf, page: 3 Where the chunk came from. Used to filter, and to show sources.
IDVectorChunk textMetadata
chunk_001[-0.0069, -0.053, 0.04 …]RAG combines a retriever with a generator …source: rag_paper.pdf, page: 3
chunk_002[-0.0026, -0.053, 0.04 …]The main advantage is access to fresh knowledge …source: rag_paper.pdf, page: 4
chunk_003[-0.0089, -0.043, 0.04 …]Retrieval reduces hallucination because …source: rag_paper.pdf, page: 4
The database holds many rows like these. Example rows.

Two Jobs: Store Once, Search Every Time

Using a vector database is two separate jobs. Storing happens once, when we load the documents. Searching happens every time a user asks a question.

Job 1: Store (once, when we load the documents)
Chunk text
RAG combines a retriever with a generator …
→
Embedding model
turns the text into a vector
→
Vector
[-0.0069, -0.053, 0.04 …]
→
Saved as one row
ID + vector + text + metadata
Job 2: Search (every time a question arrives)
The question
what is main advantage of Retrieval-Augmented Generation?
→
Embedding model
the same one as before
→
Query vector
[-0.0039, -0.077, 0.066 …]
→
Compare
with the stored vectors
→
Result
the top-k chunk texts
Both jobs use the same embedding model. Only then do the question and the chunks live in the same vector space, where we can compare them.

Searching, Step by Step

Let us follow one question from the start to the end.

1. Embed
The question goes to the same embedding model. Out comes the query vector.
2. Score
Give every stored vector a similarity score against the query vector. How the score is worked out is the next tab.
3. Rank
Sort the scores. The best one comes first.
4. Return
Take the top k and give back their chunk text and metadata. The text goes into the prompt as context.
Searching is a comparison of vectors. The words of the question never meet the words of the chunks. Step 2 is the slow part when there are many vectors, and the third tab fixes it.

Top-k: How Many Do You Want?

The search returns the k best chunks. In your code, k=3 means three.

retrieved_docs= vector_store.similarity_search(query=user_question,k=3)
RankChunkSimilarity score
1The main advantage is access to fresh knowledge …0.89returned
2Retrieval reduces hallucination because …0.84returned
3RAG combines a retriever with a generator …0.80returned
4The encoder maps text to vectors …0.52
5Training used a large public corpus …0.31
Example scores. With k=3 the top three go to the prompt as context.