langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.08k stars 14.97k forks source link

Issue: <How can I extract vector data from pgvector for use as a reference in the next conversation to enable long-term memory functionality for my chatbot?> #15689

Closed yen111445 closed 5 months ago

yen111445 commented 9 months ago

Issue you'd like to raise.

import os from urllib.parse import quote_plus from langchain.vectorstores.pgvector import PGVector from langchain.chat_models import ChatOpenAI from langchain.chains import ConversationalRetrievalChain from langchain.memory import ConversationTokenBufferMemory from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores.pgvector import DistanceStrategy

os.environ['OPENAI_API_KEY'] = "mykey" embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")

CONNECTION_STRING = PGVector.connection_string_from_db_params( driver=os.environ.get("PGVECTOR_DRIVER", "psycopg2"), host="192.168.xxx.xx", port=5432, database="xxxxx", user="xxxxxxxxx", password=quote_plus("xxxx@r"), )

vectordb = PGVector(embedding_function=embeddings, collection_name="tmp06", connection_string=CONNECTION_STRING, distance_strategy=DistanceStrategy.COSINE, )

llm_name = "gpt-3.5-turbo" llm = ChatOpenAI(model_name=llm_name, temperature=0) memory_token_limit = 100 retriever = vectordb.as_retriever() memory = ConversationTokenBufferMemory( llm=llm, max_token_limit=int(memory_token_limit), memory_key="chat_history", return_messages=True ) qa = ConversationalRetrievalChain.from_llm( llm, retriever=retriever, memory=memory, verbose=True, ) chat_history = [] while True: memory.load_memory_variables({}) question = input('ask:') result = qa.run({'question': question, 'chat_history': chat_history}) print(result) chat_history.append([f'User: {question}', f'Ai: {result}']) print(chat_history) embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2") st_history = ' '.join(map(str, chat_history)) res = embeddings.embed_query(st_history) vectordb.add_vector(res) best_solution = vectordb.nearest(res, n=1)

print(f'ok: {res[:4]}...') if question.lower() == 'bye': break Traceback (most recent call last): File "C:\Users\syz\Downloads\ChatBotgpt-3.5-turbo-main\models\1227.py", line 53, in vectordb.add_vector(res) ^^^^^^^^^^^^^^^^^^^ AttributeError: 'PGVector' object has no attribute 'add_vector'

Suggestion:

How can I extract vector data from pgvector for use as a reference in the next conversation to enable long-term memory functionality for my chatbot?

dosubot[bot] commented 9 months ago

🤖

Hello @yen111445! 👋 I'm Dosu, your friendly neighborhood bot. I can assist you in solving bugs, answering questions, and even guide you on how to become a contributor. While we wait for a human maintainer, I'm here to help with your issue. So, let's dive right in! 🚀😄

Based on the context provided, it seems like there's a misunderstanding about the add_vector method. The PGVector class in LangChain does not have an add_vector method, which is why you're seeing the AttributeError: 'PGVector' object has no attribute 'add_vector' error.

To store vector data in the PGVector object, you should use the insert_document method. This method takes a Document object, which includes the text and its corresponding vector representation. Here's an example of how you can use this method:

from langchain.vectorstores import Document

# Assume you have an instance of PGVector named vectordb and an embedding vector named res
document = Document(text=st_history, embedding=res)
vectordb.insert_document(document)

In this example, st_history is the text you want to store and res is its corresponding vector representation.

To retrieve the vector data from the PGVector object, you can use the max_marginal_relevance_search_with_score_by_vector method as explained in the context. This method returns a list of documents and their scores, which can be used as a reference for enabling long-term memory functionality in your chatbot.

Please note that the max_marginal_relevance_search_with_score_by_vector method is not designed to return the exact vector data that was stored. It's used to find documents that are most relevant to a given vector. If you need to retrieve the exact vector data, you might need to implement a custom method for that.

I hope this helps! If you have any other questions, feel free to ask.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.