DEV Community

Sivakami Thangaraj
Sivakami Thangaraj

Posted on

RAG and Vector Databases:

Large Language Models (LLMs) like ChatGPT are powerful, but they have one major limitation—they do not know your private documents, latest company data, or newly uploaded PDFs.

This is where RAG (Retrieval-Augmented Generation) helps.

RAG allows LLMs to search external documents, retrieve the most relevant information, and generate accurate answers instead of guessing.

Step 1: Chunking the Data

When we upload a PDF, the LLM does not read the full file at once.

It first splits the document into smaller parts called:
Chunks

Example:

Today is Wednesday
Tomorrow is Thursday
I am travelling Today
This process is called Chunking.

Step 2: Converting Text into Vectors

Machines understand numbers, not words.

So text is converted into vectors.

The basic method is:

One-Hot Encoding

Example:

Today is Wednesday
Line 1 = [1,1,1,0,0,0,0,0,0,0,0]
Line 2 = [0,1,0,1,1,0,0,0,0,0,0]
Line 3 = [1,0,0,0,0,1,1,1,0,0,0]
Line 4 = [0,1,1,0,0,0,0,0,1,1,1]

But this method cannot understand meaning.

So modern systems use:
Embeddings

Embedding models like nomic-embed-text (Ollama) convert sentences into meaningful vectors.

Example:

Today is Wednesday
→ [0.23, -0.45, 0.88, ...]

Step 3: Storing in Vector Database

These vectors are stored in a:
**
Vector Database**

Popular tools include:

  • ChromaDB
  • FAISS
  • Pinecone
  • Qdrant
  • MongoDB Vector Search

Traditional databases like MySQL do exact search.

Vector databases do:

Semantic Search

which means searching by meaning, not exact words.

Example:
Search: AI Course
Result: Machine Learning, Deep Learning

Step 4: Similarity Search

When a user asks:

What day is tomorrow?

The query is also converted into a vector.

The system finds the nearest vectors using:

  • Cosine Similarity ⭐
  • Euclidean Distance
  • Manhattan Distance

This helps retrieve:

Tomorrow is Thursday

and sends it to the LLM as context.

Final RAG Flow

PDF

Chunking

Embedding

Vector Database

User Query

Similarity Search

Relevant Chunks

LLM Final Answer

In One Line
*Embedding creates meaning
Vector DB stores meaning
Semantic Search finds meaning
RAG gives smart answers ✨
*

Top comments (0)