pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #814: Cannot find a way to get a similarity score between 0 and 1
3+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #814: Cannot find a way to get a similarity score between 0 and 1
@ 2025-04-04 07:50 "andyjessop (@andyjessop)" <noreply+andyjessop@github.com>
0 siblings, 0 replies; 3+ messages in thread
From: andyjessop (@andyjessop) @ 2025-04-04 07:50 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
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?
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] issue #814: Cannot find a way to get a similarity score between 0 and 1
@ 2025-04-04 08:36 ` "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2025-04-04 08:36 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @andyjessop, you can convert the cosine distance to a similarity between 0 and 1 with `1 - (cosine distance / 2)`.
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] issue #814: Cannot find a way to get a similarity score between 0 and 1
@ 2025-04-04 08:36 ` "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2025-04-04 08:36 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 3+ messages in thread
end of thread, other threads:[~2025-04-04 08:36 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-04-04 07:50 [pgvector/pgvector] issue #814: Cannot find a way to get a similarity score between 0 and 1 "andyjessop (@andyjessop)" <noreply+andyjessop@github.com>
2025-04-04 08:36 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-04-04 08:36 ` "ankane (@ankane)" <noreply+ankane@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox