Files
Obsidian/MyselfDev/langchain/AI Learning Series — Part 4Embeddings, ChromaDB, Chunking & Retrieval (With a PDF → Chroma →….md
coco 8b94d76799 a
2026-07-21 13:57:15 +08:00

185 lines
4.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "AI Learning Series — Part 4:Embeddings, ChromaDB, Chunking & Retrieval (With a PDF → Chroma →…"
source: "https://medium.com/@vasanthancomrads/ai-learning-series-part-4-embeddings-chromadb-chunking-retrieval-with-a-pdf-chroma-abc5a98ecb5f"
author:
- "[[Vasanthan K]]"
published: 2025-11-24
created: 2026-04-06
description: "Master RAG essentials with embeddings, ChromaDB, and retrieval techniques. Follow a beginner-friendly walkthrough to build a PDF-to-Chroma RAG app."
tags:
- "clippings"
---
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:
```c
"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)
```c
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](https://www.trychroma.com/) is a lightweight vector database ideal for RAG apps.
### Install
```c
pip install chromadb pypdf openai
```
### Initialize a collection
```c
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
```c
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:
![](https://miro.medium.com/v2/resize:fit:1100/format:webp/1*iAiYLnk_PUtZsnDsfjdorw.png)
### 🧪 Exercise: Build a “PDF → Chroma → Answer” RAG App
Let’s build a **fully working** pipeline.
### Step 1: Load and Chunk the PDF
```c
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
```c
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
```c
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
```c
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