pgvector / pgvector-python

pgvector support for Python
MIT License
886 stars 61 forks source link

How insert embedding data use sqlalchemy session.execute #62

Closed mwcodefun closed 6 months ago

mwcodefun commented 6 months ago

Hello,

I am currently working on a Python script that involves inserting embedding data into a database. The snippet of the code I’m using is as follows:

session.execute(text(f"INSERT INTO mytable (id, embeddings) VALUES (1, {embedding_data})"))

However, I’ve encountered an error when attempting to pass OpenAI’s embedding object to the embedding_data placeholder. I’d appreciate any guidance on whether I’m using the API correctly. Is it possible to insert an OpenAI embedding object directly, or do I need to perform some type of serialization or conversion before insertion?

Thank you in advance for your help and suggestions.

ankane commented 6 months ago

Hi @mwcodefun, check out the readme for how to insert vectors with SQLAlchemy. You can also use:

session.execute(insert(Item).values(id=1, embedding=np.array([1, 2, 3])))
# or
session.execute(insert(Item), [{'id': 1, 'embedding': np.array([1, 2, 3])}])
# or
session.execute(text('INSERT INTO mytable (id, embedding) VALUES (:id, :embedding)'), {'id': 1, 'embedding': np.array([1, 2, 3])})