From: andyjessop (@andyjessop) <noreply+andyjessop@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #814: Cannot find a way to get a similarity score between 0 and 1
Date: Fri, 04 Apr 2025 07:50:35 +0000
Message-ID: <gh-pgvector-pgvector-814@github.com> (raw)
I'm sure this is me doing something wrong, but I've tried various different ways of doing a similarity search, and I keep getting some odd numbers as a similarity score.
It is my understanding that I should be getting a score between 0 and 1, but I'm seeing scores clustered tightly around 2, e.g.
```
1.9999345
2
2.0000123
2.0000456
```
I'm using the OpenAI [text-embedding-3-small](https://platform.openai.com/docs/models/text-embedding-3-small) which I believe is normalised. I also created a custom normalise function just in case, and got the same result.
The vector column was created with the right dimensions, I believe:
```pgsql
ALTER TABLE node
DROP COLUMN IF EXISTS vector;
ALTER TABLE node
ADD COLUMN vector vector(1536);
```
```pgsql
DROP FUNCTION IF EXISTS get_similar_nodes;
CREATE OR REPLACE FUNCTION get_similar_nodes (
vector vector(1536),
user_id uuid,
threshold float DEFAULT 0.6,
count int DEFAULT 200
)
RETURNS TABLE (
node node,
similarity_score float
)
LANGUAGE sql
AS $$
SELECT
n AS node,
1 - (n.vector <#> vector) AS similarity_score
FROM node n
JOIN memory m ON m.id = n.memory_id
WHERE m.user_id = user_id
AND 1 - (n.vector <#> vector) >= threshold
ORDER BY similarity_score DESC
LIMIT LEAST(count, 200);
$$;
```
I've tried cosine distance and negative inner product, but still the same results.
Is there something obvious I'm missing?