Lesson 14 ยท Intermediate

Vector Databases and Semantic Search

Learn what vector databases do and how they make embedding search practical at scale.

Read the explanation carefully, then review the examples and coding section. The goal is to understand both the concept and how it appears inside a real application workflow.

Explanation

A vector database stores embeddings and supports similarity search across many records.

Metadata filters let you narrow results by source, date, topic, or access level.

Good retrieval quality depends on embeddings, chunking, metadata, and ranking strategy.

Why this topic matters in practice

In generative AI products, the model is only one part of the system. The surrounding workflow determines whether the output is useful, safe, and maintainable. This lesson matters because it helps you connect the idea to tasks such as tutoring, search, copilots, business assistants, and production automation.

Examples

Course search

A learner asks about overfitting and retrieves multiple lessons even if the exact word appears only in one of them.

Support knowledge base

Semantic search can find policy passages related to a refund query across many documents.

Internal wiki

Staff can search by meaning and still filter results to HR documents or a recent time window.

A simple in-memory semantic search demo

The code below is intentionally concise so the underlying pattern stays clear. It focuses on the application logic you can reuse, even if you later switch model providers or deployment environments.

documents = [
    {"id": 1, "title": "Intro to Regression", "vector": [0.9, 0.1, 0.0]},
    {"id": 2, "title": "Neural Networks Basics", "vector": [0.1, 0.9, 0.2]},
    {"id": 3, "title": "Prompt Engineering", "vector": [0.2, 0.7, 0.8]},
]

query_vector = [0.15, 0.88, 0.25]

def score(v1, v2):
    return sum(a * b for a, b in zip(v1, v2))

ranked = sorted(documents, key=lambda d: score(query_vector, d["vector"]), reverse=True)
for item in ranked:
    print(item["title"], score(query_vector, item["vector"]))

How the coding section works

  • This example uses a dot-product style score for simplicity.
  • A vector database does this more efficiently and supports much larger collections.
  • Real systems also store metadata so results can be filtered and explained.

Implementation advice

When turning this lesson into a real feature, think beyond the code snippet itself. Decide what inputs should be allowed, how you will validate outputs, how you will recover from errors, and how you will measure whether the feature is actually helping users. Those surrounding choices often determine whether an AI feature feels polished or unreliable.

Summary / key takeaways

  • Vector databases make embedding-based search practical at scale.
  • Metadata is just as important as vector similarity in real applications.
  • Retrieval quality is a systems problem, not just a database problem.

Exercises

  1. Why is metadata filtering useful in semantic search?
  2. Add a metadata field such as topic or level to the sample documents.
  3. Compare keyword search and semantic search for a tutorial website.