pgvector: Embeddings and vector similarity
pgvector is a PostgreSQL extension for vector similarity search. It can also be used for storing embeddings.
Concepts#
Vector similarity#
Vector similarity refers to a measure of the similarity between two related items. For example, if you have a list of products, you can use vector similarity to find similar products. To do this, you need to convert each product into a "vector" of numbers, using a mathematical model. You can use a similar model for text, images, and other types of data. Once all of these vectors are stored in the database, you can use vector similarity to find similar items.
Embeddings#
This is particularly useful if you're building on top of OpenAI's GPT-3. You can create and store embeddings which match the GPT model you're using.
Usage#
Enable the extension#
- Go to the Database page in the Dashboard.
- Click on Extensions in the sidebar.
- Search for "vector" and enable the extension.
Usage#
Create a table to store vectors#
create table posts (
id serial primary key,
title text not null,
body text not null,
embedding vector(1536)
);
Storing a vector / embedding#
In this example we'll generate a vector using the OpenAI API client, then store it in the database using the Supabase client.
const title = 'First post!'
const body = 'Hello world!'
// Generate a vector using OpenAI
const embeddingResponse = await openai.createEmbedding({
model: 'text-embedding-ada-002',
input: body,
})
const [responseData] = embeddingResponse.data.data.
// Store the vector in Postgres
const { data, error } = await supabase.from('posts').insert({
title,
body,
embedding: responseData.embedding,
})
Resources#
- Source code: github.com/pgvector/pgvector