Comments (5)
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.
Hi @AbhiPawar5, please create a minimal reproducible example if you're looking for help (re: pgvector/pgvector#327).
from pgvector-python.
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.
from pgvector-python.
Hi @ankane, converting embeddings to np array solved my issue. It was numpy.ndarray
before :)
from pgvector-python.
Related Issues (20)
- How insert embedding data use sqlalchemy session.execute HOT 1
- Using with psycopg 3 pools throws thread error HOT 6
- Sqlalchemy example not working HOT 2
- Questions on HOT 1
- Django support for empty fields HOT 3
- pgvector-python client support for sparse vectors. HOT 3
- Migrating From `VectorField` -> `HalfVectorField` HOT 1
- SQLAlchemy does not recognize vector column HOT 1
- NameError: name 'psycopg' is not defined when register_vector fails HOT 1
- Can't adapt type 'numpy.ndarray' when used via polars HOT 3
- register_vector() Fails When Only 'vector' Extension is Installed, Missing 'halfvec' and 'sparsevec HOT 2
- Maybe spurious exception (ValueError: missing dimensions) when using sparsevec with django HOT 2
- Have you tought about supporting Piccolo? HOT 1
- Adding type annotations? HOT 2
- register_vector fails with asyncpg (v0.3.1) HOT 2
- Inserting binary vectors using SQLalchemy HOT 2
- Duplicate Search HOT 1
- Having trouble when try to insert OpenAI embedding by sqlalchemy to CloudSQL HOT 6
- Takes more time to get the value from Class django.db.models.query.QuerySet HOT 1
- sqladmin - pgvector field converter HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pgvector-python.