Your first embedding query in Python
This is the lesson where you make embeddings real. By the end you have a working semantic search over your own data. Module 9 will turn this into RAG.
Today you build a tiny private Google for whatever text you give it. Cool.
Three steps:
- Embed your documents.
- Store the vectors with their text (and ideally metadata).
- At query time, embed the question and find nearest vectors.
We will use OpenAI embeddings + ChromaDB. Swap providers later as needed.
import os, glob, chromadb
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client_oai = OpenAI()
client_chroma = chromadb.PersistentClient(path="./chroma_db")
col = client_chroma.get_or_create_collection("notes")
def embed(text):
return client_oai.embeddings.create(
model="text-embedding-3-small", input=text
).data[0].embedding
# Ingest .txt files from ./notes
for path in glob.glob("notes/*.txt"):
text = open(path).read()
col.add(
ids=[path],
documents=[text],
embeddings=[embed(text)],
metadatas=[{"source": path}],
)
# Query
question = "what does GeekHub do?"
results = col.query(query_embeddings=[embed(question)], n_results=3)
for doc, meta in zip(results["documents"][0], results["metadatas"][0]):
print(meta["source"], "->", doc[:200])
50 lines and you have a semantic search engine.
Quick recall
3 prompts · think before you flip
Prompt 1 of 3
What is the difference between semantic search and RAG?
Quiz time
1 question · tap an answer to check it
1. The script above is missing what to make it RAG?
Finished lesson 8.5?
Mark complete to update your module progress and unlock the streak.
Loading