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

Add a clear() method to VectorStoreRetrieverMemory with FAISS vector store #10169

Closed
wants to merge 7 commits into from

Conversation

uray-lu
Copy link

@uray-lu uray-lu commented Sep 4, 2023

- Description: In constructing the VectorStoreRetrieverMemory with InMemoryDocstore() like faiss vectorstore, it may also need to clear the chat_history memory like ConversationBufferMemory. To clear the memory without reconstruct a new memory, I add a clear() method to VectorStoreRetrieverMemory make it reusable in the same session, and can use memory.clear() to empty the memory vectorstore. < Only fot the FAISS vectorstore>

- Issue: the issue #5817 how to clear VectorStoreRetrieverMemory?

- Dependencies: None
- Tag maintainer:
- Twitter handle: @932196e6a7d042d

**- Usecase: **

from langchain.memory import VectorStoreRetrieverMemory
from langchain.prompts import PromptTemplate
from langchain.docstore import InMemoryDocstore
from langchain.vectorstores import FAISS
import faiss
embedding_size = 1536 # Dimensions of the OpenAIEmbeddings
index = faiss.IndexFlatL2(embedding_size)
embedding_fn = OpenAIEmbeddings().embed_query
vectorstore = FAISS(embedding_fn, index, InMemoryDocstore({}), {})
retriever = vectorstore.as_retriever(search_kwargs=dict(k= 3)) #set the vector store args
memory = VectorStoreRetrieverMemory(retriever=retriever)

# When added to an agent, the memory object can save pertinent information from conversations or used tools
memory.save_context({"input": "My favorite food is pizza"}, {"output": "that's good to know"})
memory.save_context({"input": "My favorite sport is soccer"}, {"output": "..."})
memory.save_context({"input": "I don't the Celtics"}, {"output": "ok"}) 

# Query the relevance history
memory.load_memory_variables({"prompt": "How's the weather "})
## return {'history': "input: My favorite food is pizza\noutput: that's good to know"}


#Clear 
memory.clear()
memory.load_memory_variables({"prompt": "How's the weather "})
## return {"history":}

@baskaryan

@vercel
Copy link

vercel bot commented Sep 4, 2023

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

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 6, 2023 2:27am

@dosubot dosubot bot added Ɑ: vector store Related to vector store module 🤖:improvement Medium size change to existing code to handle new use-cases labels Sep 4, 2023
@uray-lu uray-lu marked this pull request as draft September 5, 2023 02:04
@uray-lu uray-lu marked this pull request as draft September 5, 2023 07:42
@uray-lu uray-lu marked this pull request as ready for review September 5, 2023 07:42
@uray-lu uray-lu changed the title Add a clear() method to VectorStoreRetrieverMemory Add a clear() method to VectorStoreRetrieverMemory with FAISS vector store Sep 5, 2023
@levscaut
Copy link

Hi, I was also running to this issue. What happened to this PR next? Why was it closed? Thanks for your help.

@uray-lu
Copy link
Author

uray-lu commented Nov 24, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases Ɑ: vector store Related to vector store module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants