promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_

Embedding that fits the budget — pick a model that matches your corpus — step 9 of 9

Checkpoint

One last thing before we move on. pass this to mark the lesson done, or skip and keep moving. hop to the next when you're ready.

Final drill. Wire it all together into a tiny FAQ search engine — the same shape as a real RAG retrieval step, just with stub embeddings instead of an API call.

Build top_k(query_vec, faqs, k):

  • faqs is a list of (text, vec) tuples — 5 of them in the starter.
  • query_vec is a pre-computed embedding for the user's question.
  • Compute cosine similarity between query_vec and each FAQ vec.
  • Return the k highest-scoring FAQs as (text, score) tuples, best first.

The starter calls your function with k=2 to return the top-2 closest FAQs to the question "I forgot my password."

Real-world framing: this is the retrieval step of RAG. In production you'd swap the hand-picked vectors for real OpenAI embeddings, swap the list of 5 for thousands of chunks in a vector database, and feed the top-K results into the prompt you send to the model.

Expected output:

query: 'I forgot my password'
top 2 matches:
  0.9963  how do I reset my password?
  0.2457  how do I upgrade my plan?

full-screen editor opens — close anytime to keep reading.