Database / ChromaDB Interview Questions
How do you build a basic RAG (Retrieval-Augmented Generation) pipeline with ChromaDB?
RAG combines ChromaDB's semantic retrieval with an LLM's generation ability. The pipeline has two phases: indexing (chunk documents, embed, store in ChromaDB) and retrieval (embed the user query, fetch similar chunks, inject into LLM prompt).
import chromadb
from chromadb.utils import embedding_functions
from openai import OpenAI
import os
# --- INDEXING PHASE (run once) ---
chroma_client = chromadb.PersistentClient(path="./rag_db")
ef = embedding_functions.OpenAIEmbeddingFunction(
api_key=os.environ["OPENAI_API_KEY"],
model_name="text-embedding-3-small",
)
collection = chroma_client.get_or_create_collection(
"company_docs", embedding_function=ef, metadata={"hnsw:space": "cosine"}
)
# Chunk and index your knowledge base
documents = [
"ChromaDB supports cosine, l2, and inner-product distance metrics.",
"Persistent storage in ChromaDB uses SQLite under the hood.",
"The default embedding model is all-MiniLM-L6-v2 with 384 dimensions.",
"ChromaDB collections support metadata filtering with $eq, $gt, $in operators.",
]
collection.add(
documents=documents,
ids=[f"doc-{i}" for i in range(len(documents))],
)
# --- RETRIEVAL + GENERATION PHASE (run per query) ---
def rag_answer(user_question: str, n_results: int = 3) -> str:
# 1. Retrieve relevant chunks from ChromaDB
results = collection.query(
query_texts=[user_question],
n_results=n_results,
include=["documents", "distances"],
)
context_chunks = results["documents"][0] # list of retrieved texts
context = "\n\n".join(
f"[{i+1}] {chunk}" for i, chunk in enumerate(context_chunks)
)
# 2. Build an augmented prompt
prompt = f"""Answer the question using ONLY the context below.
If the answer is not in the context, say "I don't know."
Context:
{context}
Question: {user_question}
Answer:"""
# 3. Generate answer with LLM
openai_client = OpenAI()
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content
print(rag_answer("What distance metrics does ChromaDB support?"))
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
