Fastapi和pgvector:InvalidRequestError:未知的PG数字类型

huangapple go评论58阅读模式
英文:

Fastapi and pgvector: InvalidRequestError: Unknown PG numeric type

问题

我正在使用 pgvector、fastapi 和 sqlmodel 将向数据库插入向量。但当我需要查询表格(例如更新一行)时,我收到以下错误:

  File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/fastapi.py", line 84, in _sentry_call
    return old_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/app/app/api/controllers/records.py", line 119, in create_or_update_record_endpoint
    return create_or update_record(session, record)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/app/app/api/controllers/records.py", line 53, in create_or_update_record
    db_record = session.exec(
                    ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlmodel/orm/session.py", line 60, in exec
    results = super().execute(
              ^^^^^^^^^^^^^^^^
错误的其余部分...
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/sql/type_api.py", line 702, in _cached_result_processor
    d[coltype] = rp = d["impl"].result_processor(dialect, coltype)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 525, in result_processor
    raise exc.InvalidRequestError(
sqlalchemy.exc.InvalidRequestError: Unknown PG numeric type: 24664

我之前在插入向量时遇到了问题,但通过从以下方式更改解决了它:

def create_record(session: Session, record: RecordCreate):
    new_record = Record(**record.dict())
    session.add(new_record)
    session.commit()
    session.refresh(new_record)
    return new_record

到:

def create_record(session: Session, record: RecordCreate):
    new_record = Record(**record.dict())
    session.add(new_record)
    session.commit()
    # session.refresh(new_record)
    return dict(new_record) 

你有什么想要解决这个问题的想法吗?

英文:

I'm using pgvector, fastapi and sqlmodel to insert vectors to the database.

from pgvector.sqlalchemy import Vector

## model
class Record(SQLModel, table=True):
    id: UUID = Field(default_factory=uuid4, primary_key=True)
    text: str = Field(default=None)
    vector: List[float] = Field(default=None, sa_column=Vector(1536))

## controllers
def get_record(session: Session, id: UUID):
    return session.get(Record, id)

def create_or_update_record(session: Session, record: RecordCreate):
    db_record = session.exec(
        select(Record).where(Record.id == record.id)
    ).first()
    # rest of the function

But when I need to query the table (for updating a row for example), I get the following error:

  File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/fastapi.py", line 84, in _sentry_call
    return old_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/app/app/api/controllers/records.py", line 119, in create_or_update_record_endpoint
    return create_or_update_record(session, record)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/app/app/api/controllers/records.py", line 53, in create_or_update_record
    db_record = session.exec(
                    ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlmodel/orm/session.py", line 60, in exec
    results = super().execute(
              ^^^^^^^^^^^^^^^^
rest of the error ...
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/sql/type_api.py", line 702, in _cached_result_processor
    d[coltype] = rp = d["impl"].result_processor(dialect, coltype)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 525, in result_processor
    raise exc.InvalidRequestError(
sqlalchemy.exc.InvalidRequestError: Unknown PG numeric type: 24664

I had the issue with inserting vectors as well, but I fixed it by changing from:

def create_record(session: Session, record: RecordCreate):
    new_record = Record(**record.dict())
    session.add(new_record)
    session.commit()
    session.refresh(new_record)
    return new_record

to:

def create_record(session: Session, record: RecordCreate):
    new_record = Record(**record.dict())
    session.add(new_record)
    session.commit()
    # session.refresh(new_record)
    return dict(new_record) 

Any idea how can I fix this?

答案1

得分: 0

Changing the definition of vector column in the model from:

vector: List[float] = Field(default=None, sa_column=Vector(1536))

to:

vector: List[float] = Field(default=None, sa_column=Column(Vector(1536)))

fixed the issue.

英文:

Changing the definition of vector column in the model from:

vector: List[float] = Field(default=None, sa_column=Vector(1536))

to:

vector: List[float] = Field(default=None, sa_column=Column(Vector(1536)))

fixed the issue.

huangapple
  • 本文由 发表于 2023年4月10日 22:02:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定