To implement vector similarity search in PostgreSQL with Drizzle ORM, you can use the pgvector extension. This extension provides a set of functions to work with vectors and perform similarity search.
As for now, Drizzle doesnβt create extension automatically, so you need to create it manually. Create an empty migration file and add SQL query:
To perform similarity search, you need to create a table with a vector column and an HNSW or IVFFlat index on this column for better performance:
schema.ts
migration.sql
The embedding column is used to store vector embeddings of the guide descriptions. Vector embedding is just a representation of some data. It converts different types of data into a common format (vectors) that language models can process. This allows us to perform mathematical operations, such as measuring the distance between two vectors, to determine how similar or different two data items are.
In this example we will use OpenAI model to generate embeddings for the description:
To search for similar guides by embedding, you can use gt and sql operators with cosineDistance function to calculate the similarity between the embedding column and the generated embedding: