mirror of
https://github.com/kennethreitz/langchain.git
synced 2026-06-05 23:00:18 +00:00
acd86d33bc
Provide shared memory capability for the Agent. Inspired by #1293 . ## Problem If both Agent and Tools (i.e., LLMChain) use the same memory, both of them will save the context. It can be annoying in some cases. ## Solution Create a memory wrapper that ignores the save and clear, thereby preventing updates from Agent or Tools.
27 lines
789 B
Python
27 lines
789 B
Python
from typing import Any, Dict, List
|
|
|
|
from langchain.schema import BaseMemory
|
|
|
|
|
|
class ReadOnlySharedMemory(BaseMemory):
|
|
"""A memory wrapper that is read-only and cannot be changed."""
|
|
|
|
memory: BaseMemory
|
|
|
|
@property
|
|
def memory_variables(self) -> List[str]:
|
|
"""Return memory variables."""
|
|
return self.memory.memory_variables
|
|
|
|
def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:
|
|
"""Load memory variables from memory."""
|
|
return self.memory.load_memory_variables(inputs)
|
|
|
|
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
|
|
"""Nothing should be saved or changed"""
|
|
pass
|
|
|
|
def clear(self) -> None:
|
|
"""Nothing to clear, got a memory like a vault."""
|
|
pass
|