🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Search a Real Vector Store

Build a real top-k similarity search across 5 cached, real-embedding-derived vectors and interpret why unrelated-sounding chunks can still rank highly.

Narrated Video Summary
data-composition-id="ragchatbotmasterclass-module2_lesson4"1280×720 @ 30fps3 clips0:49 total

Module 2: Vector Store & Retrieval

You now have real embeddings for three chunks. A real handbook has more sections than that — so this module scales up to five chunks and builds the actual search function your chatbot will call at query time: given a question, which chunks are actually relevant?

# Module 2 Goal

retrieve("How many PTO days carry over?")
# -> ["PTO Policy chunk", "Parental Leave chunk"]

Raw Chunks Aren't a Prompt Yet

similarity_search() gives you back a list of chunk ids — not something you can paste into a prompt yet. Next lesson: formatting retrieved chunks into clean, labeled context the LLM can actually cite.

/* Next: Formatting Context */
0:00 / 0:49
Scene 1 / 3 — Module 2: Vector Store & Retrieval
Total XP: 0|💻 ragchatbotmasterclass XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Vector Store Search

Real top-k retrieval.

Quick Quiz //

Why does production RAG cache document embeddings instead of recomputing them per search?


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Scale your similarity search from one comparison to a real multi-document vector store, and see why the results make semantic sense even when the wording doesn't match.

1Why You Cache Embeddings Instead of Re-Computing Them

Generating an embedding costs a real API call every time. If your chatbot re-embedded every document in your knowledge base on every single user question, costs and latency would explode for no reason — the documents haven't changed. Production systems embed each document once at ingestion time, store the vector, and only embed the live query on each request.

2Top-K and Semantic Neighbors

Retrieval almost never returns just one result — you typically ask for the top few (top_k) matches, because the single best match might not contain everything needed to answer fully, and adjacent, related chunks often add useful context. That's exactly what happened here: a PTO question also pulled in the Parental Leave chunk, because both are 'leave policy' concepts sitting close together in embedding space.

3Step-by-Step Breakdown

Module 2: Vector Store & Retrieval. You now have real embeddings for three chunks. A real handbook has more sections than that — so this module scales up to five chunks and builds the actual search function your chatbot will call at query time: given a question, which chunks are actually relevant?

Search a Real Vector Store. These 5 vectors were generated once by calling the real embeddings API — exactly like you did last lesson — then cached here, because re-embedding unchanged documents on every search would be wasteful. Finish similarity_search(): sort scored so the best match comes first, then keep only the top_k ids.

Why does the vacation-days query also retrieve the Parental Leave chunk, even though the question never mentions parental leave?

  • Both are about time-off policies, so their embeddings sit close together in vector space — semantic similarity, not keyword overlap, drives the match.
  • Because 'parental-leave' comes right after 'pto-policy' in the dictionary's insertion order.

Raw Chunks Aren't a Prompt Yet. similarity_search() gives you back a list of chunk ids — not something you can paste into a prompt yet. Next lesson: formatting retrieved chunks into clean, labeled context the LLM can actually cite.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Expose Retrieved Source Count to Assistive Tech

When a RAG UI shows 'answered using 2 sources', make sure that count is in real text content, not just a visual badge, so screen reader users get the same transparency about grounding.

<span>Answered using 2 retrieved sources</span>

SEO Implications

  • 1

    Target 'vector similarity search' and 'top-k retrieval' as distinct searches

    Developers hit these as separate, specific implementation problems once past the basic embeddings tutorial stage.

Best Practices

Cache Document Embeddings, Only Embed the Live Query

Re-embedding unchanged documents on every request wastes API cost and adds latency for zero benefit — embed once at ingestion, store the vector, and reuse it indefinitely until the source document changes.

Frequent Bugs

THE BUG

Forgetting to re-sort after adding new documents to an in-memory vector store, so top_k results are stale.

THE FIX

Always compute similarity scores fresh against the current query — never cache search *results*, only the document embeddings themselves.

Real-World Examples

HR Knowledge Base

An HR chatbot's vector store returns both the PTO policy and the parental leave policy for a vacation question, giving the LLM enough related context to also proactively mention parental leave exists — a genuinely useful side effect of semantic (not keyword) search.

results = similarity_search(query_vector, top_k=2)

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Not reading error messages carefully

TypeError: '<' not supported between instances of 'str' and 'float' // Solution: check what you're actually sorting — a list of tuples sorts by the first element by default.

The Solution //

Most of the time, the interpreter tells you exactly what line caused the crash and why. Read tracebacks from the top down to identify the root cause.

Lesson Glossary

[01]Vector Store

A collection of pre-computed embedding vectors (usually paired with their source text) that can be searched by similarity.

Code Preview
[{id, vector}, ...]

[02]Top-K Retrieval

Returning the K highest-scoring matches from a similarity search, rather than just the single best one.

Code Preview
top_k=2

Continue Learning