I want to use VectorStoreRetrieverMemory in langchain with PGVector in python. I didn't get any documents related to it. can someone help me with it.
1 Answers
To use VectorStoreRetrieverMemory
in langchain with PGVector
, you need to first install the following dependencies:
pip install langchain
pip install pgvector
Once the dependencies are installed, you can create a VectorStoreRetrieverMemory
object by passing in the connection string to your PostgreSQL database (or your preferred database) and the name of the collection that contains the vectorized documents.
from langchain.memory import VectorStoreRetrieverMemory
vector_store = VectorStoreRetrieverMemory(
connection_string="postgresql://localhost:5432/my_database",
collection="documents",
)
You can then use the retrieve()
method to retrieve the documents that are most similar to a given query.
documents = vector_store.retrieve(query="What is the capital of France?", k=3)
for document in documents:
print(document["text"])
Here is a complete example of how to use VectorStoreRetrieverMemory
with PGVector
:
import os
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
from langchain.memory import VectorStoreRetrieverMemory
# Create a connection string to your PostgreSQL database.
connection_string = os.environ["PGVECTOR_CONNECTION_STRING"]
# Create a VectorStoreRetrieverMemory object.
vector_store = VectorStoreRetrieverMemory(connection_string, "documents")
# Create a ConversationChain object.
chain = ConversationChain(
llm=OpenAI(),
memory=vector_store,
)
# Start a conversation.
while True:
query = input("")
# Retrieve the most similar documents to the query.
documents = vector_store.retrieve(query, k=3)
# Print the most similar documents.
for document in documents:
print(document["text"])
# Generate a response from the LLM.
response = chain.generate(query)
print(response)
This code will create a ConversationChain
object that uses VectorStoreRetrieverMemory
to retrieve the most similar documents to a user's query. The LLM will then be used to generate a response to the query.

- 12,190
- 6
- 97
- 81
-
The answer is generated by Google Bard. I got very similar answer when copy the question inside the Google Bart. It also failed by bot test https://www.zerogpt.com/ and https://gptzero.me/ – Hongbo Miao Sep 02 '23 at 23:03