pgvector / pgvector-python

pgvector support for Python
MIT License
975 stars 63 forks source link

cannot import name 'register_vector' from 'psycopg' #91

Closed dynamicdeploy closed 1 month ago

dynamicdeploy commented 1 month ago

I am getting this for psycopg3. cannot import name 'register_vector' from 'psycopg' Mac OS psycopg 3.2.2 vector extensions is enabled and works from pgAdmin in Postgres

This is the code. I haven't gotten anything to work so far. Tried all options and read all articles.

import psycopg import numpy as np from psycopg import register_vector

with psycopg.connect("postgresql://xxx:xxx$@localhost:5432/xxx") as conn:

    conn.execute('CREATE EXTENSION IF NOT EXISTS vector')

    register_vector(conn)
    conn.execute('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))')
    # https://github.com/pgvector/pgvector-python/issues/4 <-- tried everything from here.

    embedding = np.array([1, 2, 3])
    conn.execute('INSERT INTO items (embedding) VALUES (%s)', (embedding,))
    conn.execute('SELECT * FROM items ORDER BY embedding <-> %s LIMIT 5', (embedding,)).fetchall()
ankane commented 1 month ago

Hi @dynamicdeploy, the correct import is:

from pgvector.psycopg import register_vector

Docs

dynamicdeploy commented 1 month ago

Then the compiler complains "Import "pgvector.psycopg" could not be resolved" pip install pgvector Requirement already satisfied: pgvector in ./forpdf/lib/python3.12/site-packages (0.3.3) Requirement already satisfied: numpy in ./forpdf/lib/python3.12/site-packages (from pgvector) (1.26.4)

ankane commented 1 month ago

It looks like that's a Pylance error (not specific to pgvector). Please use other resources for help.