Cole
Cole

Reputation: 21

Querying an Existing Table with Vectors Using Langchain in PGVector Database

I've set up a table in a PGVector database and populated it with records, including two vector fields, using psycopg2-binary (version 2.9.9) in Python. The initial table creation was done through SQL in pgAdmin. Now, I'm looking to perform queries on this table utilizing the langchain framework in Python.

While langchain seems to support loading from an index by collection name, I haven't found documentation on how to work with an existing table directly or how to specify which of the two vector fields to query against.

My questions are:

  1. Can langchain be used to query a pre-existing table in a PGVector database, and if so, what is the process? (assuming the table was not created via langchain)
  2. How can I specify which of the two vector fields in my table should be used for the queries?

Upvotes: 1

Views: 900

Answers (1)

Hasee Kumanayake
Hasee Kumanayake

Reputation: 1

db = PGVector.from_existing_index( embedding=embeddings,
documents=splits,
collection_name=COLLECTION_NAME,
connection_string=CONNECTION_STRING)

similar = db.similarity_search_with_score(query, k=2)

for doc in similar:
    print(doc, end="\n\n")

Upvotes: 0

Related Questions