I am trying to implement hybrid search in postgresql with pgvector and sqlalchemy.
Below is the table schema:
class Project_images(Base):
__tablename__ = "project_images"
id = Column(Integer, Sequence("project_image_id_seq"), primary_key=True)
image_link = Column(String(255))
image_vector = Column(Vector(512))
keywords = Column(String(255))
keyword_vector = Column(Vector(768))
And this is the function I call to perform the search:
def query_db(
image_encoding,
image_search_weight,
keyword_encoding,
keyword_search_weight,
):
search_query = text(
"""
SELECT *,
((:image_encoding <=> image_vector) * :image_search_weight + (:keyword_encoding <=> keyword_vector) * :keyword_search_weight)
AS vector_sum
FROM project_images
ORDER BY vector_sum
LIMIT 50
"""
)
params = {
"image_encoding": image_encoding,
"image_search_weight": image_search_weight,
"keyword_encoding": keyword_encoding,
"keyword_search_weight": keyword_search_weight,
}
with session_class() as session:
result = session.execute(search_query, params)
return result
Trying to call query_db gives me the following error:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedFunction) operator does not exist: record <=> vector LINE 3: ...02891638, 0.08573333, -0.011385784, -0.020549707) <=> image_... ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
I have tried session.execute(text("CREATE EXTENSION IF NOT EXISTS vector")), but I still run into the same error.