Skip to content

The agent engineering platform.

langchain[patch]: Add async methods to VectorStoreRetrieverMemory (#19408)

Christophe Bornet1b813fe6fe77
diff --git a/libs/langchain/langchain/memory/vectorstore.py b/libs/langchain/langchain/memory/vectorstore.py
index 3f4430ee7c..b288ef57d8 100644
--- a/libs/langchain/langchain/memory/vectorstore.py
+++ b/libs/langchain/langchain/memory/vectorstore.py
@@ -39,13 +39,9 @@ class VectorStoreRetrieverMemory(BaseMemory):
return get_prompt_input_key(inputs, self.memory_variables)
return self.input_key
- def load_memory_variables(
- self, inputs: Dict[str, Any]
+ def _documents_to_memory_variables(
+ self, docs: List[Document]
) -> Dict[str, Union[List[Document], str]]:
- """Return history buffer."""
- input_key = self._get_prompt_input_key(inputs)
- query = inputs[input_key]
- docs = self.retriever.get_relevant_documents(query)
result: Union[List[Document], str]
if not self.return_docs:
result = "\n".join([doc.page_content for doc in docs])
@@ -53,6 +49,24 @@ class VectorStoreRetrieverMemory(BaseMemory):
result = docs
return {self.memory_key: result}
+ def load_memory_variables(
+ self, inputs: Dict[str, Any]
+ ) -> Dict[str, Union[List[Document], str]]:
+ """Return history buffer."""
+ input_key = self._get_prompt_input_key(inputs)
+ query = inputs[input_key]
+ docs = self.retriever.get_relevant_documents(query)
+ return self._documents_to_memory_variables(docs)
+
+ async def aload_memory_variables(
+ self, inputs: Dict[str, Any]
+ ) -> Dict[str, Union[List[Document], str]]:
+ """Return history buffer."""
+ input_key = self._get_prompt_input_key(inputs)
+ query = inputs[input_key]
+ docs = await self.retriever.aget_relevant_documents(query)
+ return self._documents_to_memory_variables(docs)
+
def _form_documents(
self, inputs: Dict[str, Any], outputs: Dict[str, str]
) -> List[Document]:
@@ -73,5 +87,15 @@ class VectorStoreRetrieverMemory(BaseMemory):
documents = self._form_documents(inputs, outputs)
self.retriever.add_documents(documents)
+ async def asave_context(
+ self, inputs: Dict[str, Any], outputs: Dict[str, str]
+ ) -> None:
+ """Save context from this conversation to buffer."""
+ documents = self._form_documents(inputs, outputs)
+ await self.retriever.aadd_documents(documents)
+
def clear(self) -> None:
"""Nothing to clear."""
+
+ async def aclear(self) -> None:
+ """Nothing to clear."""