Command Palette

Search for a command to run...

All labs
Intermediate55 minProduction RAG

Embeddings & Vector Search

Embed documents, store in pgvector, and implement cosine + ANN search with metadata filtering.

Scenario

Build the retrieval layer: embed documents, store in pgvector, and query with metadata filters for access control.

Objective

Implement a VectorStore with embed, insert, and search(filter) methods.

Starter code
Implement the TODOs to complete the lab.
vectorstore.pypython
import pgvector
from openai import OpenAI

client = OpenAI()

class VectorStore:
    def embed(self, text: str) -> list[float]:
        """Embed text using text-embedding-3-small."""
        # TODO
        pass

    def insert(self, text: str, metadata: dict) -> str:
        """Insert document with embedding and metadata."""
        # TODO
        pass

    def search(self, query: str, k: int = 5, filters: dict = None) -> list[dict]:
        """Search by similarity with optional metadata filter."""
        # TODO — filter must apply at SQL level, not display
        pass
Solution hints
  • 1Use pgvector's <=> operator for cosine distance
  • 2Apply metadata filters in the WHERE clause
  • 3Create an HNSW index for ANN
  • 4Return chunk_id, score, text, metadata
Validation steps
Your implementation should pass these checks.
  • Insert 10 docs, search returns relevant ones
  • Metadata filter excludes docs not matching filter
  • Search respects HNSW index (check EXPLAIN)
  • Cosine similarity scores are in [-1, 1]

Run validation

This is a simulated validation environment. In production, this would run your code against the validation steps.