Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use jsonb instead of json for pgvector #11897

Closed

Conversation

devinburnette
Copy link

@devinburnette devinburnette commented Oct 17, 2023

Description

When trying to integrate pgvector as a vector store with langchain, I got the following error:

2023-10-16 18:27:54.326 EDT [87858] ERROR:  operator does not exist: json @> unknown at character 106
2023-10-16 18:27:54.326 EDT [87858] HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
2023-10-16 18:27:54.326 EDT [87858] STATEMENT:
              SELECT *, embedding <=> $1 as "_distance"
              FROM langchain_pg_embedding
              WHERE cmetadata @> $2
              ORDER BY "_distance" ASC
              LIMIT $3;

The problem

From my brief research, it appears that the @> operator only works with jsonb. So I'm trying to figure out how this ever worked in the first place, but I'm not a postgres expert.

Testing / Reproduced

To test this, I ran the following against my database after langchain created it and this seemed to fix my issue:

ALTER TABLE langchain_pg_embedding ALTER COLUMN cmetadata SET DATA TYPE jsonb;

I'm using this python version of langchain for running my document loaders and storing my embeddings in PG, but then I have a chat client that's using the langchain-js lib to query it, so I happened to then go see if pgvector had the same problem over there, and it doesn't.
https://github.com/langchain-ai/langchainjs/blob/aa9b618786757c301e93caeb3253c56b56d05602/langchain/src/vectorstores/pgvector.ts#L244C38-L244C43

Let me know if the fix isn't as simple as this or if there's a better solution that should be considered like type casting or something else?

cc:
@baskaryan, @eyurtsev, @hwchase17

@vercel
Copy link

vercel bot commented Oct 17, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Oct 17, 2023 2:30pm

@dosubot dosubot bot added Ɑ: vector store Related to vector store module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Oct 17, 2023
@@ -132,7 +132,7 @@ def __post_init__(
Initialize the store.
"""
self._conn = self.connect()
# self.create_vector_extension()
self.create_vector_extension()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not sure why this was commented out, but seems like it should be part of the setup as well /cc @hwchase17

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without this, i get the following error:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedObject) type "vector" does not exist
LINE 4:  embedding VECTOR,
                   ^

[SQL:
CREATE TABLE langchain_pg_embedding (
	collection_id UUID,
	embedding VECTOR,
	document VARCHAR,
	cmetadata JSON,
	custom_id VARCHAR,
	uuid UUID NOT NULL,
	PRIMARY KEY (uuid),
	FOREIGN KEY(collection_id) REFERENCES langchain_pg_collection (uuid) ON DELETE CASCADE
)

]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The creation query doesn't raise an exception if it fails but rather a warning, proposed fix in #11930

@eyurtsev
Copy link
Collaborator

Closing in favor of: #18992

@eyurtsev eyurtsev closed this Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: vector store Related to vector store module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants