-
Notifications
You must be signed in to change notification settings - Fork 75
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
Error: could not convert string to float when executing SELECT or ADD query with cosine_distance #114
Comments
I changed my code from asynchronous to synchronous, and it works normally. Why is this? Does it temporarily not support this kind of asynchronous operation? Dependency versions: name : sqlalchemy
version : 2.0.37
description : Database Abstraction Library
dependencies
- greenlet !=0.4.17
- typing-extensions >=4.6.0
required by
- alembic requires >=1.3.0
name : asyncpg
version : 0.30.0
description : An asyncio PostgreSQL driver
The entire code is as follows: # -*- coding:utf-8 -*-
from sqlalchemy import URL, PrimaryKeyConstraint, Index, BigInteger, TIMESTAMP, func, select, insert, text, event
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from pgvector.sqlalchemy.vector import VECTOR
from pgvector.utils.vector import Vector as VectorUtils
url = URL.create(
drivername='postgresql',
username='postgres',
password='vector123',
host='localhost',
port=5433,
database='vector_demo',
)
engine_project = create_engine(url, echo=True, future=True, pool_pre_ping=True)
session_target = sessionmaker(bind=engine_project, autoflush=False, expire_on_commit=False)
@event.listens_for(engine_project, "connect")
def connect(dbapi_connection, connection_record):
from pgvector.psycopg2 import register_vector
print('Init PGVector')
register_vector(dbapi_connection, globally=False, arrays=True)
Base = declarative_base()
class ORMEmbeddingTest(Base):
table_name = "orm_embedding_3"
__tablename__ = table_name
__table_args__ = (
PrimaryKeyConstraint('id', name=f'{table_name}_pkey'),
Index(
f'{table_name}_embedding_index',
'embedding',
postgresql_using='hnsw',
postgresql_with={'m': 16, 'ef_construction': 64},
postgresql_ops={'embedding': 'vector_cosine_ops'}
)
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
embedding: Mapped[list] = mapped_column(VECTOR(3), nullable=False)
created: Mapped[str] = mapped_column(TIMESTAMP, server_default=func.now(), nullable=False)
modified: Mapped[str] = mapped_column(TIMESTAMP, server_default=func.now(), onupdate=func.now(), nullable=False)
def create_table():
"""创建数据库表"""
Base.metadata.create_all(
engine_project,
tables=[Base.metadata.tables[ORMEmbeddingTest.__tablename__]]
)
def drop_table():
"""创建数据库表"""
Base.metadata.drop_all(
engine_project,
tables=[Base.metadata.tables[ORMEmbeddingTest.__tablename__]]
)
def run_test():
drop_table()
create_table()
query_embedding = [1, 3, 4]
db_obj = ORMEmbeddingTest(
id=99,
embedding=query_embedding
)
with session_target() as db:
db.add(db_obj)
db.commit()
try:
with session_target() as db:
select_test = db.execute(
select(
ORMEmbeddingTest.id,
ORMEmbeddingTest.embedding.cosine_distance(query_embedding).label('score')
).order_by(text('score'))
)
records = select_test.fetchall()
for record in records:
print(record.id, record.score)
except Exception as e:
print(f'orm insert:{e}')
if __name__ == '__main__':
run_test() |
Hi @SharkSyl, it'll work if you remove the |
When running a SELECT or ADD query with cosine_distance on the embedding column, I encountered the following error:
The complete information is as follows:
My entire code is as follows:
The traceback:
The text was updated successfully, but these errors were encountered: