Coder Social home page Coder Social logo

Comments (5)

ankane avatar ankane commented on August 30, 2024 1

I'd confirm you're calling register_vector on the connection you're using for semantic_search. This script produces the same error:

import numpy as np
import psycopg

conn = psycopg.connect(dbname='test')
conn.execute('SELECT %s', (np.array([1.0, 2.0, 3.0]),))
psycopg.ProgrammingError: cannot adapt type 'ndarray' using placeholder '%s' (format: AUTO)

from pgvector-python.

ankane avatar ankane commented on August 30, 2024

Hi @AbhiPawar5, please create a minimal reproducible example if you're looking for help (re: pgvector/pgvector#327).

from pgvector-python.

AbhiPawar5 avatar AbhiPawar5 commented on August 30, 2024

Hi @ankane, here's an boilerplate code that I have. I hope this helps you understand the issue:

from pgvector.psycopg import register_vector
import psycopg
from sentence_transformers import SentenceTransformer
from tqdm import tqdm

conn = psycopg.connect(dbname='test', autocommit=True)
conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
register_vector(conn)

conn.execute(''' CREATE TABLE assets 
            (id bigserial PRIMARY KEY, 
            assetId varchar(40),
            assetTypeId varchar(40),
            Class varchar(40),
            AssetTypeName varchar(40),
            Name text,
            Value text,
            FormattedValue text,
            embedding vector(384)) ''')

model = SentenceTransformer('all-MiniLM-L6-v2')
embeddings = model.encode(text_to_encode)

column_names = ", ".join(combined_df.columns)
column_names = column_names + ",embedding"

for data in combined_df.values.tolist():
    conn.execute('INSERT INTO assets (AssetId, AssetTypeId, Class, AssetTypeName, Name, Value, FormattedValue) VALUES (%s, %s, %s, %s, %s, %s, %s)', (data))

for i, embeds in enumerate(embeddings):
    conn.execute('UPDATE assets SET embedding = (%s) WHERE id = (%s)', (embeds, i))

# We see the results of this query with embeddings
query = conn.execute("SELECT * FROM assets WHERE id != 1 ORDER BY embedding <-> (SELECT embedding FROM assets WHERE id = 1) LIMIT 5;")

for res in query:
    print(res)

Now, image I do semantic search as following:

def semantic_search(query):
    global model, conn

    embed = model.encode(query)

    results = conn.execute('SELECT id, Class, AssetTypeName, Name, Value, FormattedValue FROM assets ORDER BY embedding <=> %s LIMIT 5', (embed,))
    return results

When I call the above function semantic_search, it returns:

---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)
Cell In[49], line 1
----> 1 res = semantic_search("Claim or Claim Feature is closed")
      3 for x in res:
      4     print(x)

Cell In[48], line 6, in semantic_search(query)
      2 global model, conn
      4 embed = model.encode(query)
----> 6 results = conn.execute('SELECT id, Class, AssetTypeName, Name, Value, FormattedValue FROM assets ORDER BY embedding <=> %s LIMIT 5', (embed,))
      7 return results

File [~/miniconda3/envs/pgvect/lib/python3.9/site-packages/psycopg/connection.py:892](https://file+.vscode-resource.vscode-cdn.net/Users/abhishek.pawar/Downloads/PG%20Vector/~/miniconda3/envs/pgvect/lib/python3.9/site-packages/psycopg/connection.py:892), in Connection.execute(self, query, params, prepare, binary)
    889     return cur.execute(query, params, prepare=prepare)
    891 except e._NO_TRACEBACK as ex:
--> 892     raise ex.with_traceback(None)

ProgrammingError: cannot adapt type 'ndarray' using placeholder '%s' (format: AUTO)

from pgvector-python.

AbhiPawar5 avatar AbhiPawar5 commented on August 30, 2024

from pgvector-python.

AbhiPawar5 avatar AbhiPawar5 commented on August 30, 2024

Hi @ankane, converting embeddings to np array solved my issue. It was numpy.ndarray before :)

from pgvector-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.