Supabase Vector Store
To use this vector store, you need a Supabase project. You can create one at supabase.com.
Installation
Section titled “Installation”npm i llamaindex @llamaindex/supabase
Database Setup
Section titled “Database Setup”Before using the vector store, you need to:
- Enable the
pgvector
extension - Create a table for storing vectors
- Create a vector similarity search function
create table documents (id uuid primary key,content text,metadata jsonb,embedding vector(1536));
— Create a function for similarity search with filtering support
create function match_documents (query_embedding vector(1536),match_count int,filter jsonb DEFAULT '{}') returns table (id uuid,content text,metadata jsonb,embedding vector(1536),similarity float)language plpgsqlas $$#variable_conflict use_columnbeginreturn queryselectid,content,metadata,embedding,1 - (embedding <=> query_embedding) as similarityfrom documentswhere metadata @> filterorder by embedding <=> query_embeddinglimit match_count;end;$$;
Importing the modules
Section titled “Importing the modules”import { Document, VectorStoreIndex } from "llamaindex";import { SupabaseVectorStore } from "@llamaindex/supabase";
Setup Supabase
Section titled “Setup Supabase”const vectorStore = new SupabaseVectorStore({ supabaseUrl: process.env.SUPABASE_URL, supabaseKey: process.env.SUPABASE_KEY, table: "documents",});
Setup the index
Section titled “Setup the index”const documents = [ new Document({ text: "Sample document text", metadata: { source: "example" } })];
const storageContext = await storageContextFromDefaults({ vectorStore });const index = await VectorStoreIndex.fromDocuments(documents, { storageContext,});
Query the index
Section titled “Query the index”const queryEngine = index.asQueryEngine();
// Basic query without filtersconst response = await queryEngine.query({ query: "What is in the document?",});
// Output responseconsole.log(response.toString());
Query with filters
Section titled “Query with filters”You can filter documents based on metadata when querying:
import { FilterOperator, MetadataFilters } from "llamaindex";
// Create a filter for documents with author = "Jane Smith"const filters: MetadataFilters = { filters: [ { key: "author", value: "Jane Smith", operator: FilterOperator.EQ, }, ],};
// Query with filtersconst filteredResponse = await vectorStore.query({ queryEmbedding: embedModel.getQueryEmbedding("What is vector search?"), similarityTopK: 5, filters,});
Full code
Section titled “Full code”import { Document, VectorStoreIndex, storageContextFromDefaults } from "llamaindex";import { SupabaseVectorStore } from "@llamaindex/supabase";
async function main() { // Initialize the vector store const vectorStore = new SupabaseVectorStore({ supabaseUrl: process.env.SUPABASE_URL, supabaseKey: process.env.SUPABASE_KEY, table: "documents", });
// Create sample documents const documents = [ new Document({ text: "Vector search enables semantic similarity search", metadata: { source: "research_paper", author: "Jane Smith", }, }), ];
// Create storage context const storageContext = await storageContextFromDefaults({ vectorStore });
// Create and store embeddings const index = await VectorStoreIndex.fromDocuments(documents, { storageContext, });
// Query the index const queryEngine = index.asQueryEngine(); const response = await queryEngine.query({ query: "What is vector search?", });
// Output response console.log(response.toString());}
main().catch(console.error);