4.6 KiB
title, source, author, published, created, description, tags
| title | source | author | published | created | description | tags | ||
|---|---|---|---|---|---|---|---|---|
| AI Learning Series — Part 4:Embeddings, ChromaDB, Chunking & Retrieval (With a PDF → Chroma →… | https://medium.com/@vasanthancomrads/ai-learning-series-part-4-embeddings-chromadb-chunking-retrieval-with-a-pdf-chroma-abc5a98ecb5f |
|
2025-11-24 | 2026-04-06 | Master RAG essentials with embeddings, ChromaDB, and retrieval techniques. Follow a beginner-friendly walkthrough to build a PDF-to-Chroma RAG app. |
|
Retrieval-Augmented Generation (RAG) is one of the most practical AI patterns used today. Whether you’re building chatbots, internal knowledge assistants, or customer-facing AI apps, you need to understand embeddings, chunking, vector databases and retrieval.
In this blog, we’ll cover the core concepts step-by-step:
- Embeddings
- ChromaDB setup
- Chunking strategy (50–200 tokens)
- Retrieval pipeline
- Exercise: Build a complete PDF → Chroma → Answer RAG app in Python
🔍 1. What Are Embeddings?
Embeddings are numerical representations of text.
They convert sentences into high-dimensional vectors (arrays of floats) that capture meaning.
Example:
"Apple the fruit" → [0.12, -0.93, 0.04, … ]
"Apple the company" → [1.02, 0.55, -0.31, … ]
Similar meanings → similar vectors.
Different meanings → distant vectors.
This is the foundation of semantic search and RAG.
⚙️ Generating Embeddings (Python Example)
from openai import OpenAI
client = OpenAI()
text = "AI helps developers build faster."
emb = client.embeddings.create(
model="text-embedding-3-small",
input=text
)
print(len(emb.data[0].embedding)) # Dimensionality
🗂️ 2. ChromaDB Setup
ChromaDB is a lightweight vector database ideal for RAG apps.
Install
pip install chromadb pypdf openai
Initialize a collection
import chromadb
client = chromadb.Client()
collection = client.create_collection(
name="docs",
metadata={"hnsw:space": "cosine"} # cosine similarity
)
✂️ 3. Chunking Strategy (50–200 Tokens)
Large documents need to be chunked before embedding.
Why chunk?
- Embedding long text → loses precision
- Smaller chunks → better retrieval
- Great trade-off size: 50–200 tokens
Simple chunking example
def chunk_text(text, chunk_size=150):
words = text.split()
for i in range(0, len(words), chunk_size):
yield " ".join(words[i:i + chunk_size])
🔄 4. Retrieval Pipeline
Here’s the basic RAG flow:
🧪 Exercise: Build a “PDF → Chroma → Answer” RAG App
Let’s build a fully working pipeline.
Step 1: Load and Chunk the PDF
from pypdf import PdfReader
def load_pdf(path):
reader = PdfReader(path)
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
return text
pdf_text = load_pdf("sample.pdf")
chunks = list(chunk_text(pdf_text, chunk_size=120))
print("Total chunks:", len(chunks))
Step 2: Embed and Insert into Chroma
from openai import OpenAI
client = OpenAI()
embeddings = client.embeddings.create(
model="text-embedding-3-small",
input=chunks
)
vectors = [e.embedding for e in embeddings.data]
collection.add(
documents=chunks,
embeddings=vectors,
ids=[f"id_{i}" for i in range(len(chunks))]
)
Step 3: Retrieval Function
def retrieve(query, top_k=3):
q_emb = client.embeddings.create(
model="text-embedding-3-small",
input=query
).data[0].embedding
results = collection.query(
query_embeddings=[q_emb],
n_results=top_k
)
return results["documents"][0]
Step 4: Generate Final Answer Using Retrieved Context
def answer(query):
context_chunks = retrieve(query)
context = "\n".join(context_chunks)
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant for answering based on context."},
{"role": "user", "content": f"Context:\n{context}\n\nQuestion:\n{query}"}
]
)
return response.choices[0].message["content"]
print(answer("What is the main idea of the document?"))
🎉 Final Output: A Working RAG App
You now have a complete:
PDF → Chunk → Embed → ChromaDB → Retrieve → Answer
pipeline.
This architecture is the foundation for:
- chat-with-PDF apps
- enterprise knowledge bots
- Q&A search tools
- internal documentation assistants
