[docs]@deprecated(since="0.3.1",removal="1.0.0",message=("Please see the migration guide at: ""https://python.langchain.com/docs/versions/migrating_memory/"),)classBaseChatMemory(BaseMemory,ABC):"""Abstract base class for chat memory. **ATTENTION** This abstraction was created prior to when chat models had native tool calling capabilities. It does **NOT** support native tool calling capabilities for chat models and will fail SILENTLY if used with a chat model that has native tool calling. DO NOT USE THIS ABSTRACTION FOR NEW CODE. """chat_memory:BaseChatMessageHistory=Field(default_factory=InMemoryChatMessageHistory)output_key:Optional[str]=Noneinput_key:Optional[str]=Nonereturn_messages:bool=Falsedef_get_input_output(self,inputs:Dict[str,Any],outputs:Dict[str,str])->Tuple[str,str]:ifself.input_keyisNone:prompt_input_key=get_prompt_input_key(inputs,self.memory_variables)else:prompt_input_key=self.input_keyifself.output_keyisNone:iflen(outputs)==1:output_key=list(outputs.keys())[0]elif"output"inoutputs:output_key="output"warnings.warn(f"'{self.__class__.__name__}' got multiple output keys:"f" {outputs.keys()}. The default 'output' key is being used."f" If this is not desired, please manually set 'output_key'.")else:raiseValueError(f"Got multiple output keys: {outputs.keys()}, cannot "f"determine which to store in memory. Please set the "f"'output_key' explicitly.")else:output_key=self.output_keyreturninputs[prompt_input_key],outputs[output_key]
[docs]defsave_context(self,inputs:Dict[str,Any],outputs:Dict[str,str])->None:"""Save context from this conversation to buffer."""input_str,output_str=self._get_input_output(inputs,outputs)self.chat_memory.add_messages([HumanMessage(content=input_str),AIMessage(content=output_str),])
[docs]asyncdefasave_context(self,inputs:Dict[str,Any],outputs:Dict[str,str])->None:"""Save context from this conversation to buffer."""input_str,output_str=self._get_input_output(inputs,outputs)awaitself.chat_memory.aadd_messages([HumanMessage(content=input_str),AIMessage(content=output_str),])