postgres - working with vector
Here is a simple tutorial how to work with postgres vector. We will create a table and one of its column embedding uses a embedding dimension of 2.
CREATE TABLE doctest (
id SERIAL PRIMARY KEY,
content TEXT,
embedding vector(2) -- OpenAI's ada-002 embedding size
);
To insert value into the table
INSERT INTO doctest (content, embedding) VALUES ('test', '[1.23,1.245]')
INSERT INTO doctest (content, embedding) VALUES ('test7', '[7, 10]')
To select
SELECT content, 1 - (embedding <=> '[7, 9]') AS similarity
FROM doctest
ORDER BY similarity DESC
LIMIT 1;
And gives you the output here:
If you make another search
SELECT content, 1 - (embedding <=> '[1.23, 1.3]') AS similarity
FROM doctest
ORDER BY similarity DESC
LIMIT 1;
Comments