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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain: Add async methods to VectorStoreRetrieverMemory #19408

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 30 additions & 6 deletions libs/langchain/langchain/memory/vectorstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,34 @@ def _get_prompt_input_key(self, inputs: Dict[str, Any]) -> str:
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])
else:
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]:
Expand All @@ -73,5 +87,15 @@ def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
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."""