Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inserting binary vectors using SQLalchemy #84

Closed
JungeAlexander opened this issue Jul 18, 2024 · 2 comments
Closed

Inserting binary vectors using SQLalchemy #84

JungeAlexander opened this issue Jul 18, 2024 · 2 comments

Comments

@JungeAlexander
Copy link

JungeAlexander commented Jul 18, 2024

Hello,

Thanks for the work on this, @ankane and team! It's amazing to see more vector types and distance metrics being added continuously :) I am trying to insert a binary vector using SQLalchemy and my code looks like this:

from pgvector.sqlalchemy import BIT
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Entity(DeclarativeBase):
    [...]
    embedding: Mapped[np.array] = mapped_column(BIT(2048))

I am then trying to insert embedding as a list[int] of 0s and 1s only. However, this raises the following error:

RepositoryError: An exception occurred: (psycopg.errors.DatatypeMismatch) column "embedding" is of type bit but expression is of type smallint[]
                                                             ^
HINT:  You will need to rewrite or cast the expression.

A similar approach works fine using a list[float] when inserting into an embedding of type pgvector.sqlalchemy.Vector.

My question is: which Python type do I need to cast embedding to before insertion?

Thanks for the help in advance!

@JungeAlexander
Copy link
Author

Addendum: I tried to insert list[float] and list[bool] values as binary vectors but this did not work either.

@ankane
Copy link
Member

ankane commented Jul 18, 2024

Hi @JungeAlexander, it currently only accepts a bit string.

binary_embedding = [1, 0, 1]
''.join([str(v) for v in binary_embedding]) # '101'

@ankane ankane closed this as completed Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants