RAG vector index helpers for HNSW and IVFFlat
npm install @sf-bot/rag-indexes
RAG vector index helpers for HNSW and IVFFlat.
This package provides SQL functions for creating and managing pgvector indexes on RAG embeddings.
Create an HNSW index for fast approximate nearest neighbor search.
``sql
-- Create HNSW index with defaults (m=16, ef_construction=64, cosine distance)
SELECT rag.create_hnsw_index('collection-uuid');
-- Create with custom parameters
SELECT rag.create_hnsw_index(
'collection-uuid',
'text-embedding-3-small',
32, -- m: connections per layer
128, -- ef_construction: index build quality
'l2' -- distance: cosine, l2, or ip
);
`
Parameters:
- m: Max connections per layer (default: 16). Higher = better recall, more memoryef_construction
- : Build-time quality (default: 64). Higher = better recall, slower builddistance_op
- : Distance function - cosine, l2/euclidean, or ip/inner_product
Create an IVFFlat index for fast indexing with good recall.
`sql
-- Create IVFFlat index with defaults (lists=100, cosine distance)
SELECT rag.create_ivfflat_index('collection-uuid');
-- Create with custom parameters
SELECT rag.create_ivfflat_index(
'collection-uuid',
'text-embedding-3-small',
200, -- lists: number of clusters
'cosine' -- distance: cosine, l2, or ip
);
`
Note: IVFFlat requires data to be present before creating the index.
List all vector indexes, optionally filtered by collection.
`sql`
SELECT * FROM rag.list_vector_indexes();
SELECT * FROM rag.list_vector_indexes('collection-uuid');
Drop a vector index by name.
`sql`
SELECT rag.drop_vector_index('idx_rag_embedding_hnsw_...');
| Index | Best For | Trade-offs |
|-------|----------|------------|
| HNSW | High recall, easy management | Slower inserts, more memory |
| IVFFlat | Fast indexing, large datasets | Requires pre-populated data |
- @sf-bot/rag-core`