Building a Naive RAG with FAISS or Chroma
Write 40 lines of Python: embed query, top-K nearest chunks, stuff into the prompt, ask the LLM.
Building a Naive RAG with FAISS or Chroma is a free AI Agents lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A 40-Line RAG
You now have all the pieces. We will assemble them into a working naive RAG over a small set of docs.
Step 1: Install
# pip install openai chromadbStep 2: Sample Corpus
docs = [
'Python is a high-level interpreted programming language created by Guido van Rossum in 1991.',
'Pizza is a savory dish of Italian origin consisting of a round flat base of dough.',
'The Eiffel Tower is a wrought-iron lattice tower in Paris, France, completed in 1889.',
'NumPy is a Python library used for working with arrays and numerical computing.',
'The Great Wall of China is a series of fortifications built across northern China.'
]
print(f"Loaded {len(docs)} documents")
for i, d in enumerate(docs):
print(f"{i+1}. {d[:50]}...")
Step 3: Set Up Chroma
import chromadb
from openai import OpenAI
client = chromadb.Client()
collection = client.create_collection('demo')
openai_client = OpenAI()Step 4: Embed and Store
embed_response = openai_client.embeddings.create(
model='text-embedding-3-small',
input=docs
)
vectors = [d.embedding for d in embed_response.data]
collection.add(
ids=[f'd{i}' for i in range(len(docs))],
embeddings=vectors,
documents=docs
)Step 5: Embed the Query
query = 'Tell me about Python programming'
query_vec = openai_client.embeddings.create(
model='text-embedding-3-small',
input=query
).data[0].embeddingStep 6: Retrieve Top-K
results = collection.query(
query_embeddings=[query_vec],
n_results=2
)
retrieved = results['documents'][0]
print(retrieved)
# ['Python is a high-level...', 'NumPy is a Python library...']Step 7: Build the RAG Prompt
context = '\n'.join(f'- {chunk}' for chunk in retrieved)
prompt = f'''
Using only the context below, answer the question. If unknown, say so.
Context:
{context}
Question: {query}
'''Step 8: Call the LLM
response = openai_client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content': prompt}],
temperature=0,
)
print(response.choices[0].message.content)FAISS Alternative
If you want pure local with no DB server, FAISS is even simpler:
import faiss
import numpy as np
index = faiss.IndexFlatIP(1536) # inner product, normalised vectors
index.add(np.array(vectors).astype('float32'))
D, I = index.search(np.array([query_vec]).astype('float32'), k=2)
retrieved = [docs[i] for i in I[0]]Limitations of This Naive RAG
- No chunking — only works for short docs
- No re-ranking
- No metadata filtering
- No evaluation
All of these we will fix in later courses.
Cost Per Query
For each query you make:
- 1 embedding call (~$0.00002)
- 1 chat completion with ~500 tokens of context (~$0.0001)
Total: about 1/100th of a cent per question.
Ship This and Iterate
This 40-line RAG already beats most no-context chatbots for factual Q&A over your data. Ship it, watch users break it, then add advanced techniques where it matters most.
Naive RAG Top-K
What does "top-K" retrieval mean?
Recap
You built RAG. Next we look at vector databases that scale this beyond a single laptop.
Frequently asked questions
Is the “Building a Naive RAG with FAISS or Chroma” lesson free?
Yes — the full text of “Building a Naive RAG with FAISS or Chroma” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Building a Naive RAG with FAISS or Chroma”?
Write 40 lines of Python: embed query, top-K nearest chunks, stuff into the prompt, ask the LLM. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a Naive RAG with FAISS or Chroma” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What RAG Solves (Knowledge Cut-off, Hallucinations)
- Chunking Strategies (Fixed, Sentence, Semantic)
- Indexing a Document Set
- Building a Naive RAG with FAISS or Chroma