Vector database

From kumaran-is/claude-code-onboarding · View interactive version

Vector database is an AI agent skill from the GitHub repository kumaran-is/claude-code-onboarding. Use for all vector database work — pgvector schema design, Weaviate collection creation, RAG pipeline scaffolding, embedding model selection, index tuning (HNSW vs IVFFlat), and embedding model migration. Triggers on: 'vector search', 'pgvector', 'weaviate', 'embedding', 'RAG pipeline', 'semantic se

How to use this skill

Skill file

Vector Database Skill

Iron Law

Pin dimensions at model selection. The embedding dimension must match the model exactly and is set once — changing models requires a full re-embedding migration. Store embedding_model varchar(100) alongside every vector column so the model is always auditable.


Embedding Model Selection

Choose model before writing any schema. Dimension determines index shape.

| Model | Dims | Notes |

|-------|------|-------|

| text-embedding-3-small (OpenAI) | 1536 | Best cost/quality for most cases |

| text-embedding-3-large (OpenAI) | 3072 | Max OpenAI quality, 2× cost |

| embed-english-v3.0 (Cohere) | 1024 | Native Weaviate integration |

| voyage-3-large (Voyage AI) | 1024 | Top retrieval benchmarks |

| nomic-embed-text (local) | 768 | Free, on-prem, no API call |

Rule: Once a dimension is written into schema, it cannot change without /migrate-embedding-model.


pgvector

When to Use

Vector Column Declaration

-- ALWAYS enable extension in migration up
CREATE EXTENSION IF NOT EXISTS vector;

-- Column: dimension must match model exactly
ALTER TABLE vendors ADD COLUMN embedding vector(1536);
ALTER TABLE vendors ADD COLUMN embedding_model varchar(100) DEFAULT 'text-embedding-3-small';

Distance Operators

| Operator | Distance Type | Use When |

|----------|--------------|----------|

| <-> | L2 (Euclidean) | Default; requires L2-normalized embeddings |

| <#> | Negative inner product | Dot-product similarity (fast for normalized vecs) |

| <=> | Cosine | Cosine similarity; works unnormalized |

Critical: The operator used in queries MUST match the operator used in CREATE INDEX. Mismatch = full table scan silently.

Index Selection

| | HNSW | IVFFlat |

|--|------|---------|

| Build speed | Slow, high memory | Fast, needs existing data |

| Query recall | Higher (~0.99) | Tunable via probes |

| Use when | >100K rows, production | Dev / budget-constrained / <100K rows |

| Update behavior | Good — no rebuild needed | Degrades; needs ANALYZE after bulk inserts |

-- HNSW (production default)
CREATE INDEX ON vendors USING hnsw (embedding vector_cosine_ops)
  WITH (m = 16, ef_construction = 64);

-- IVFFlat (dev/budget)
CREATE INDEX ON vendors USING ivfflat (embedding vector_cosine_ops)
  WITH (lists = 100);
-- After bulk insert: ANALYZE vendors;

Example Query Pattern (vendor matching)

SELECT v.id, v.name, v.city, v.rating,
       1 - (v.embedding <=> $1::vector) AS similarity
FROM vendors v
WHERE v.city = $2
  AND v.category = $3
  AND v.rating >= $4
  AND v.embedding IS NOT NULL
ORDER BY v.embedding <=> $1::vector
LIMIT 10;

For full migration template → see references/pgvector-migration-template.md


Weaviate

When to Use

Collection Schema Essentials

import weaviate
import weaviate.classes as wvc

client = weaviate.connect_to_weaviate_cloud(
    cluster_url=os.environ["WEAVIATE_URL"],
    auth_credentials=wvc.init.Auth.api_key(os.environ["WEAVIATE_API_KEY"]),
)

# ALWAYS set multi_tenancy at creation — cannot change later
client.collections.create(
    name="VendorReview",
    vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(
        model="text-embedding-3-small",
    ),
    vector_index_config=wvc.config.Configure.VectorIndex.hnsw(
        distance_metric=wvc.config.VectorDistances.COSINE,
    ),
    multi_tenancy_config=wvc.config.Configure.multi_tenancy(enabled=True),
    properties=[
        wvc.config.Property(name="text", data_type=wvc.config.DataType.TEXT),
        wvc.config.Property(name="source", data_type=wvc.config.DataType.TEXT),
        wvc.config.Property(name="vendor_id", data_type=wvc.config.DataType.TEXT),
    ],
)

Query Patterns

collection = client.collections.get("VendorReview")

# nearText (semantic)
result = collection.query.near_text(query="plumber emergency repair", limit=5)

# Hybrid search (BM25 + vector)
# alpha=1.0 = pure vector | alpha=0.0 = pure BM25
result = collection.query.hybrid(
    query="24-hour plumber",
    alpha=0.75,
    fusion_type=wvc.query.HybridFusion.RELATIVE_SCORE,
    limit=10,
)

Fusion algorithms:

For named vectors and multi-tenant patterns → see references/weaviate-collection-patterns.md


Two-Tier Architecture Decision

When building a system with both structured and unstructured data:

| Tier | Store | Use For |

|------|-------|---------|

| 1 | pgvector (in Cloud SQL) | Data with a DB row — vendors, tickets, invoices, tenant records |

| 2 | Weaviate Serverless | Data outside DB — scraped reviews, PDFs in GCS, external docs |

Rule: Keep data in the tier where it originates. Don't sync Postgres rows to Weaviate at MVP — async sync lag introduces consistency bugs. Move to Weaviate only when matching against external documents.


Slash Commands

Reference Files

| File | Content | When to Use |

|------|---------|-------------|

| references/pgvector-migration-template.md | Full migration SQL with up/down, index, metadata column | pgvector schema design |

| references/weaviate-collection-patterns.md | Named vectors, multi-tenancy, advanced queries | Weaviate collection design |

| references/rag-pipeline-patterns.md | Chunking strategies, retrieval, reranking, LangChain wiring | Building RAG pipelines |

| references/embedding-migration-guide.md | Step-by-step model switching procedure | Re-embedding migrations |

| references/vector-index-tuning-playbook.md | Quantization strategies, HNSW benchmarking, memory estimation, Qdrant config | Index tuning and performance optimization |

| references/ann-vs-knn.md | KNN vs ANN tradeoffs, HNSW/IVFFlat tuning, recall measurement, high-risk scoped verification pattern | Choosing and tuning retrieval index strategy |

| references/rag-ingest-checklist.md | Production ingestion checklist: embeddings, chunking, null guards, multi-tenancy | Safe data ingestion into vector stores |

| references/tier-1-tier-2-decision.md | Decision framework: pgvector (hot path, SQL joins) vs Weaviate (cold path, pure semantic) | Choosing the right vector store for a use case |

Documentation Sources

Before generating code, consult these sources for current APIs:

| Source | Tool | Purpose |

|--------|------|---------|

| pgvector | Context7 MCP (pgvector library) | Vector operators, index syntax, distance functions |

| Weaviate Python v4 | weaviate-docs MCP | Collection API, query patterns, named vectors |

| OpenAI Embeddings | Context7 MCP (openai library) | text-embedding-3-small/large, dimensions, pricing |

| Cohere Embed | Context7 MCP (cohere library) | embed-english-v3.0, batch embedding |

Post-Code Review

After writing vector database code, dispatch:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.