PostgreSQL Extension
The Embedefy PostgreSQL Extension provides access to embeddings directly from your database, without building and maintaining additional applications. Once the extension is installed, you can query your database as you normally would, but with the benefits of embeddings.
Quick Start
Step 1: Get an access token
Sign up for an Embedefy account. It is free. Once you sign in, go to Access Tokens page an click on the "New Token" button to create a new access token.
Step 2: Install the PostgreSQL extension
Installing the Embedefy PostgreSQL extension is no different from installing any other PostgreSQL extension. You just need to install the dependencies, build the extension, and then install it.
To use the PostgreSQL extension, the embedefy.access_token
variable must be set. In general, there are two options:
- Option 1. Add
embedefy.access_token=<your access token>
to yourpostgresql.conf
file for all database sessions. - Option 2. Run
SET embedefy.access_token TO '<your access token>'
for the current database session.
-- Set the Embedefy access token if it is not in the postgresql.conf file
SET embedefy.access_token TO '<your access token>';
-- Install the Embedefy extension
CREATE EXTENSION embedefy CASCADE;
Step 3: Use embeddings
Get embeddings for a given text:
select embedefy_embeddings('all-minilm-l6-v2', 'hello there');
Let's create a table, insert some data, and then query it using embeddings:
-- Create a table and insert some data
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name text UNIQUE
);
INSERT INTO products (name)
VALUES
('Bacon'),
('Bagel'),
('Coffee'),
('Croissant'),
('Oatmeal'),
('Omelette'),
('Orange juice'),
('Pancake mix'),
('Tea'),
('Cream cheese'),
('Brocoli'),
('Chicken wings'),
('Eggplant'),
('Hummus'),
('Meatballs'),
('Mixed salad'),
('Noodles'),
('Pasta'),
('Soda'),
('Sparkling water'),
('Beef steak'),
('Caesar salad'),
('Green tea'),
('Lamb chops'),
('Pizza sauce'),
('Red wine'),
('Rice'),
('Salmon fillet'),
('Sweet potatoes'),
('Tiramisu');
-- Create the embeddings table
SELECT embedefy_embeddings_table_create('products', ARRAY['id'], 'bge-small-en-v1.5');
-- Process the embeddings
SELECT embedefy_embeddings_table_process('products', 'name', 'bge-small-en-v1.5');
-- Query the table by cosine similarity
SELECT p.name
FROM products p, embedefy_products ep
WHERE p.id = ep.id
ORDER BY 1 - ((SELECT embedefy_embeddings('bge-small-en-v1.5', 'looking for breakfast items')::vector(384)) <=> ep.embedding) DESC
LIMIT 5;
name
-------------
Pancake mix
Omelette
Oatmeal
Noodles
Bacon
If you have any questions, visit our GitHub repo and open an issue.