pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #640: Late interaction embedding support
20+ messages / 9 participants
[nested] [flat]
* [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-08-08 14:02 "az-arteria (@az-arteria)" <noreply+az-arteria@github.com>
0 siblings, 0 replies; 20+ messages in thread
From: az-arteria (@az-arteria) @ 2024-08-08 14:02 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi,
I wanted to ask a question: does pgvector currently support working with late interaction text embeddings, like the ones that come from a ColBERT model for example? This is an example of a vector that I would be referring to:
```
[
[-0.013, 0.020, -0.007, -0.111],
[-0.030, -0.055, 0.001, 0.072],
[-0.041, 0.014, -0.032, -0.062],
....
]
```
Currently when inserting such a vector I seem to get `ValueError: expected ndim to be 1`, with this usage:
```python
conn.execute("INSERT INTO documents (content, embedding) VALUES (%s, %s)", (content, embedding))
# content = text, embedding = vector
```
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-08-09 03:37 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: jkatz (@jkatz) @ 2024-08-09 03:37 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@az-arteria You can have an array of vectors (similar to a PostgreSQL [multi-dimensional array[(https://www.postgresql.org/docs/current/arrays.html)) e.g.
```sql
SELECT ARRAY[
[
'[-0.013, 0.020, -0.007, -0.111]'::vector,
'[-0.030, -0.055, 0.001, 0.072]'::vector,
'[-0.041, 0.014, -0.032, -0.062]'::vector
]
```
where `embedding` would be a `vector[]` type.
but I'm curious how you would intend to use / search over this data.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-08-09 15:08 ` "az-arteria (@az-arteria)" <noreply+az-arteria@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: az-arteria (@az-arteria) @ 2024-08-09 15:08 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi, thanks for the reply
So if I'm understanding correctly I should be able to do something like this?
```python
conn.execute("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector[])")
conn.execute("INSERT INTO documents (content, embedding) VALUES (%s, %s)", ("example content", [[0.08865993, 0.16410036], [0.0221169, 0.1254668]]))
```
I'm still getting `ValueError: expected ndim to be 1` from this block of code, so I'm assuming syntax-wise maybe I'm doing something wrong here.
As for how to use / search, I see that some vector databases like [here](https://qdrant.tech/documentation/concepts/vectors/#multivectors) support multiple vectors for a single text, and have a function for computing similarity score for two separate lists of vectors (shown in url). This is particularly useful for models like colbert, which output multiple vectors for one piece of text.
Do you know if this sort of functionality is supported by PGVector as well?
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-08-12 22:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: ankane (@ankane) @ 2024-08-12 22:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-08-12 22:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: ankane (@ankane) @ 2024-08-12 22:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @az-arteria, with Psycopg, you'll need to pass a list of NumPy arrays for the `vector[]` type.
```python
embeddings = [np.array([1, 2, 3]), np.array([4, 5, 6])]
conn.execute('INSERT INTO documents (embedding) VALUES (%s)', (embeddings,))
```
For searching, you could write a SQL function to compute MaxSim.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-08-13 06:50 ` "ankane (@ankane)" <noreply+ankane@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: ankane (@ankane) @ 2024-08-13 06:50 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Added a [full example](https://github.com/pgvector/pgvector-python/blob/master/examples/colbert_exact.py) for exact search.
For approximate search, you could store each vector in a separate row and use the approach described in Section 3.6 of the [ColBERT paper](https://arxiv.org/abs/2004.12832).
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-10-10 07:44 ` "olad32 (@olad32)" <noreply+olad32@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: olad32 (@olad32) @ 2024-10-10 07:44 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@az-arteria have you any findings or limitation to share about this ? did you try exact or approximate search ? does it scale well with pgvector ?
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-10-30 18:51 ` "CarterBurns33 (@CarterBurns33)" <noreply+CarterBurns33@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: CarterBurns33 (@CarterBurns33) @ 2024-10-30 18:51 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane Is this something that can work with binary quantization as well? multi-dimensional embeddings do not scale well on a large enough dataset due to their sheer size - for example each ColQwen embedding is [755,128]. I haven't been able to insert data into a BIT[] type column, although that may be user error on my part.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-10-30 19:04 ` "ankane (@ankane)" <noreply+ankane@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: ankane (@ankane) @ 2024-10-30 19:04 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@CarterBurns33 Not sure what error you're seeing, but it should work for `bit`.
```sql
CREATE TABLE items (embeddings bit(3)[]);
INSERT INTO items (embeddings) VALUES ('{"000", "111"}');
```
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-10-30 19:30 ` "CarterBurns33 (@CarterBurns33)" <noreply+CarterBurns33@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: CarterBurns33 (@CarterBurns33) @ 2024-10-30 19:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks for the reply! Yes, it was just some formatting error since it's slightly different than the vector syntax. Got it to work with a slight tweak to the max_sim function as well.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-11-04 17:11 ` "blobgetblob (@blobgetblob)" <noreply+blobgetblob@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: blobgetblob (@blobgetblob) @ 2024-11-04 17:11 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@CarterBurns33 hey can you share your snippet working on colipali with pgvector, thx
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2024-11-16 23:51 ` "ankane (@ankane)" <noreply+ankane@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: ankane (@ankane) @ 2024-11-16 23:51 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Added an example of ColPali (ColQwen2) with binary quantization [here](https://github.com/pgvector/pgvector-python/blob/master/examples/colpali/exact.py).
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2025-01-23 13:48 ` "matheusft (@matheusft)" <noreply+matheusft@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: matheusft (@matheusft) @ 2025-01-23 13:48 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hello,
I'm struggling to add my Multi-Vector (ColBERT) embedding into my Postgres DB.
Below is a demo version of the python code I'm using.
```python
from dotenv import load_dotenv
import os
import psycopg
import numpy as np
load_dotenv()
# Connect to your postgres DB
conn = psycopg.connect(
"dbname={} user={} password={} host={}".format(
os.getenv("DB"),
os.getenv("USER"),
os.getenv("PASSWORD"),
os.getenv("HOST"),
)
)
# Open a cursor to perform database operations
cur = conn.cursor()
embedding = [np.random.rand(3, 128)]
# Insert the formatted string into the database
conn.execute('INSERT INTO SCHEMA.TABLE (content, embeddings) VALUES (%s, %s)',
("content", embedding))
```
This is how my table looks like
```bash
MY_DB=# \d SCHEMA.TABLE;
Table "SCHEMA.TABLE"
Column | Type | Collation | Nullable | Default
------------+---------------+-----------+----------+---------------------------------------
id | bigint | | not null | nextval('documents_id_seq'::regclass)
content | text | | |
embeddings | vector(128)[] | | |
Indexes:
"documents_pkey" PRIMARY KEY, btree (id)
```
This is what the python code above gives me.
⚠️⚠️⚠️
`psycopg.ProgrammingError: cannot adapt type 'ndarray' using placeholder '%s' (format: AUTO)`
⚠️⚠️⚠️
Any help?
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2025-01-23 17:38 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: jkatz (@jkatz) @ 2025-01-23 17:38 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
You need to use the `pgvector` python adapter, which will convert the `ndarray`:
https://github.com/pgvector/pgvector-python
Instructions for [psycopg3 are here](https://github.com/pgvector/pgvector-python?tab=readme-ov-file#psycopg-3).
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2025-01-24 12:05 ` "matheusft (@matheusft)" <noreply+matheusft@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: matheusft (@matheusft) @ 2025-01-24 12:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
The problem has been now solved 🎉.
I was having issues with to main things:
1. I was not registering vector types with the database connection `register_vector(conn)`
2. The multi-vector embedding format accepted by `psycopg` is a list of one-dimensional vectors `[List[rows*np.array(columns)]`, not a list of a single multi-dimensional vector `[List[np.array(rows,columns)]` as I was doing.
The code below works fine.
```python
from dotenv import load_dotenv
import os
import psycopg
from pgvector.psycopg import register_vector
import numpy as np
load_dotenv()
# Connect to your postgres DB
conn = psycopg.connect(
"dbname={} user={} password={} host={}".format(
os.getenv("DB"),
os.getenv("USER"),
os.getenv("PASSWORD"),
os.getenv("HOST"),
)
)
# Register vector types with the database connection
register_vector(conn)
# Create a multi-dimensional vector (list of one-dimensional vectors)
input_multi_vector = [np.random.rand(128) for _ in range(10)]
with conn.cursor() as cur:
cur.execute("""
INSERT INTO SCHEMA.TABLE (content, embeddings)
VALUES (%s, %s);
""", ("content", input_multi_vector,))
conn.commit()
conn.close()
```
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2025-04-23 11:03 ` "winstxnhdw (@winstxnhdw)" <noreply+winstxnhdw@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: winstxnhdw (@winstxnhdw) @ 2025-04-23 11:03 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Can we get index support for Late Interaction Embeddings too?
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2025-05-16 16:35 ` "fsommers (@fsommers)" <noreply+fsommers@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: fsommers (@fsommers) @ 2025-05-16 16:35 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
That's my question, also, adding index support. I use the approach @ankane provides [here](https://github.com/pgvector/pgvector-python/blob/master/examples/colbert/exact.py) and [here](https://github.com/pgvector/pgvector-python/blob/master/examples/colpali/exact.py). But that doesn't scale to a large dataset.
In the Colpali example, the column datatype is bit(128)[], and I don't think pgvector supports indexing a column with this type (as, e.g., hsnw requires a column with a single vector data type).
Any suggestions on how to scale this to a larger dataset would be welcome.
ps: As it is, I'm planning to flatten the bit(128)[] to a set of rows and index on that, and then perform the `maxsim()` function on the set of rows.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2025-05-16 19:10 ` "ankane (@ankane)" <noreply+ankane@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: ankane (@ankane) @ 2025-05-16 19:10 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
There's an example of approximate search [here](https://github.com/pgvector/pgvector-python/blob/master/examples/colbert/approximate.py) (using the approach described in Section 3.6 of the ColBERT paper). The example uses the `vector` type, but the approach can be applied to `bit` as well.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2025-05-17 00:47 ` "fsommers (@fsommers)" <noreply+fsommers@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: fsommers (@fsommers) @ 2025-05-17 00:47 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks @ankane! I will use that approach. I have a quick question about the examples: In the ColQwen example you use bit[128] but the example you reference here uses vector(128). The bit[] example binary quantizes the ColQwen embedding vectors. My understanding is that this works well, because even though the quantization loses the magnitude, it keeps the vector direction, which works well for the similarity search. If you could kindly comment / share some opinions on the tradeoffs between bit[] vs vector embeddings for this use-case, it'd be much appreciated! Is there an advantage to the bit[] representation vs just storing the floating-point vector values that ColQwen produces? (Of course, using cosine vs Hemming distance in the vector case). Thanks, in advance.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgvector/pgvector] issue #640: Late interaction embedding support
@ 2025-05-17 01:26 ` "ankane (@ankane)" <noreply+ankane@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: ankane (@ankane) @ 2025-05-17 01:26 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
The examples vary to show how to implement max sim with either type, but you can use either type for either model. `bit` will use less space but have lower recall than `vector` or `halfvec`. I'd recommend trying and measuring recall for `bit` and `halfvec`, as well as binary quantization with reranking, to see which is best for your application.
^ permalink raw [nested|flat] 20+ messages in thread
end of thread, other threads:[~2025-05-17 01:26 UTC | newest]
Thread overview: 20+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-08-08 14:02 [pgvector/pgvector] issue #640: Late interaction embedding support "az-arteria (@az-arteria)" <noreply+az-arteria@github.com>
2024-08-09 03:37 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-08-09 15:08 ` "az-arteria (@az-arteria)" <noreply+az-arteria@github.com>
2024-08-12 22:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-12 22:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-13 06:50 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-10 07:44 ` "olad32 (@olad32)" <noreply+olad32@github.com>
2024-10-30 18:51 ` "CarterBurns33 (@CarterBurns33)" <noreply+CarterBurns33@github.com>
2024-10-30 19:04 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-30 19:30 ` "CarterBurns33 (@CarterBurns33)" <noreply+CarterBurns33@github.com>
2024-11-04 17:11 ` "blobgetblob (@blobgetblob)" <noreply+blobgetblob@github.com>
2024-11-16 23:51 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-23 13:48 ` "matheusft (@matheusft)" <noreply+matheusft@github.com>
2025-01-23 17:38 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2025-01-24 12:05 ` "matheusft (@matheusft)" <noreply+matheusft@github.com>
2025-04-23 11:03 ` "winstxnhdw (@winstxnhdw)" <noreply+winstxnhdw@github.com>
2025-05-16 16:35 ` "fsommers (@fsommers)" <noreply+fsommers@github.com>
2025-05-16 19:10 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-17 00:47 ` "fsommers (@fsommers)" <noreply+fsommers@github.com>
2025-05-17 01:26 ` "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