SupabaseVectorStore
Supabase is an open-source Firebase alternative. Supabase is built on top of PostgreSQL, which offers strong SQL querying capabilities and enables a simple interface with already-existing tools and frameworks.
LangChain.js supports using a Supabase Postgres database as a vector
store, using the pgvector
extension. Refer to the Supabase blog
post for
more information.
This guide provides a quick overview for getting started with Supabase
vector stores. For detailed
documentation of all SupabaseVectorStore
features and configurations
head to the API
reference.
Overviewβ
Integration detailsβ
Class | Package | PY support | Package latest |
---|---|---|---|
SupabaseVectorStore | @langchain/community | β |
Setupβ
To use Supabase vector stores, youβll need to set up a Supabase database
and install the @langchain/community
integration package. Youβll also
need to install the official
@supabase/supabase-js
SDK as a peer dependency.
This guide will also use OpenAI
embeddings, which require you
to install the @langchain/openai
integration package. You can also use
other supported embeddings models
if you wish.
- npm
- yarn
- pnpm
npm i @langchain/community @langchain/core @supabase/supabase-js @langchain/openai
yarn add @langchain/community @langchain/core @supabase/supabase-js @langchain/openai
pnpm add @langchain/community @langchain/core @supabase/supabase-js @langchain/openai
Once youβve created a database, run the following SQL to set up
pgvector
and create the
necessary table and functions:
-- Enable the pgvector extension to work with embedding vectors
create extension vector;
-- Create a table to store your documents
create table documents (
id bigserial primary key,
content text, -- corresponds to Document.pageContent
metadata jsonb, -- corresponds to Document.metadata
embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed
);
-- Create a function to search for documents
create function match_documents (
query_embedding vector(1536),
match_count int DEFAULT null,
filter jsonb DEFAULT '{}'
) returns table (
id bigint,
content text,
metadata jsonb,
embedding jsonb,
similarity float
)
language plpgsql
as $$
#variable_conflict use_column
begin
return query
select
id,
content,
metadata,
(embedding::text)::jsonb as embedding,
1 - (documents.embedding <=> query_embedding) as similarity
from documents
where metadata @> filter
order by documents.embedding <=> query_embedding
limit match_count;
end;
$$;
Credentialsβ
Once youβve done this set the SUPABASE_PRIVATE_KEY
and SUPABASE_URL
environment variables:
process.env.SUPABASE_PRIVATE_KEY = "your-api-key";
process.env.SUPABASE_URL = "your-supabase-db-url";
If you are using OpenAI embeddings for this guide, youβll need to set your OpenAI key as well:
process.env.OPENAI_API_KEY = "YOUR_API_KEY";
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:
// process.env.LANGCHAIN_TRACING_V2="true"
// process.env.LANGCHAIN_API_KEY="your-api-key"
Instantiationβ
import { SupabaseVectorStore } from "@langchain/community/vectorstores/supabase";
import { OpenAIEmbeddings } from "@langchain/openai";
import { createClient } from "@supabase/supabase-js";
const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-small",
});
const supabaseClient = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_PRIVATE_KEY
);
const vectorStore = new SupabaseVectorStore(embeddings, {
client: supabaseClient,
tableName: "documents",
queryName: "match_documents",
});
Manage vector storeβ
Add items to vector storeβ
import type { Document } from "@langchain/core/documents";
const document1: Document = {
pageContent: "The powerhouse of the cell is the mitochondria",
metadata: { source: "https://example.com" },
};
const document2: Document = {
pageContent: "Buildings are made out of brick",
metadata: { source: "https://example.com" },
};
const document3: Document = {
pageContent: "Mitochondria are made out of lipids",
metadata: { source: "https://example.com" },
};
const document4: Document = {
pageContent: "The 2024 Olympics are in Paris",
metadata: { source: "https://example.com" },
};
const documents = [document1, document2, document3, document4];
await vectorStore.addDocuments(documents, { ids: ["1", "2", "3", "4"] });
[ 1, 2, 3, 4 ]
Delete items from vector storeβ
await vectorStore.delete({ ids: ["4"] });
Query vector storeβ
Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.
Query directlyβ
Performing a simple similarity search can be done as follows:
const filter = { source: "https://example.com" };
const similaritySearchResults = await vectorStore.similaritySearch(
"biology",
2,
filter
);
for (const doc of similaritySearchResults) {
console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* Mitochondria are made out of lipids [{"source":"https://example.com"}]
If you want to execute a similarity search and receive the corresponding scores you can run:
const similaritySearchWithScoreResults =
await vectorStore.similaritySearchWithScore("biology", 2, filter);
for (const [doc, score] of similaritySearchWithScoreResults) {
console.log(
`* [SIM=${score.toFixed(3)}] ${doc.pageContent} [${JSON.stringify(
doc.metadata
)}]`
);
}
* [SIM=0.165] The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* [SIM=0.148] Mitochondria are made out of lipids [{"source":"https://example.com"}]
Metadata Query Builder Filteringβ
You can also use query builder-style filtering similar to how the
Supabase JavaScript
library
works instead of passing an object. Note that since most of the filter
properties are in the metadata column, you need to use arrow operators
(-> for integer or ->> for text) as defined in Postgrest API
documentation
and specify the data type of the property (e.g.Β the column should look
something like metadata->some_int_prop_name::int
).
import { SupabaseFilterRPCCall } from "@langchain/community/vectorstores/supabase";
const funcFilter: SupabaseFilterRPCCall = (rpc) =>
rpc.filter("metadata->>source", "eq", "https://example.com");
const funcFilterSearchResults = await vectorStore.similaritySearch(
"biology",
2,
funcFilter
);
for (const doc of funcFilterSearchResults) {
console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* Mitochondria are made out of lipids [{"source":"https://example.com"}]
Query by turning into retrieverβ
You can also transform the vector store into a retriever for easier usage in your chains.
const retriever = vectorStore.asRetriever({
// Optional filter
filter: filter,
k: 2,
});
await retriever.invoke("biology");
[
Document {
pageContent: 'The powerhouse of the cell is the mitochondria',
metadata: { source: 'https://example.com' },
id: undefined
},
Document {
pageContent: 'Mitochondria are made out of lipids',
metadata: { source: 'https://example.com' },
id: undefined
}
]
Usage for retrieval-augmented generationβ
For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:
- Tutorials: working with external knowledge.
- How-to: Question and answer with RAG
- Retrieval conceptual docs
API referenceβ
For detailed documentation of all SupabaseVectorStore
features and
configurations head to the API
reference.
Relatedβ
- Vector store conceptual guide
- Vector store how-to guides