GeekHub Learn
Module
Lesson 8.55 of 5 in this module2 min read Module 8: Vector Embeddings Simplified

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:

  1. Embed your documents.
  2. Store the vectors with their text (and ideally metadata).
  3. 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.

Visualize it

A flow diagram of the script: files in -> embed -> Chroma -> query -> top results out.

Try it now

Run the script on your own notes folder. Marvel at retrieval.

Hands-on lab

Build the script. Add at least 10 documents. Run 5 queries. Save results for Module 9.

Try it now

Why is this not yet RAG?

Common mistakes

  • Forgetting to persist Chroma (use PersistentClient)
  • Embedding the same document twice (deduplicate)
  • Not storing metadata (you will want filters later)

Debugging tip

If results are weak, your documents may be too long for one embedding. Chunk them (we will do this in Module 9).

Challenge

Add a date filter so you can search "notes from the last 30 days only".

Where this shows up

  • Personal note search
  • Internal wiki search
  • Product catalog search
  • Code snippet retrieval

From the field

This 50-line script is the production seed of dozens of "find similar" features in real products. Master it.

Recap

Embed, store, query. Three steps to your first semantic search. Module 9 adds the LLM and you have RAG.


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. 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