BaseChatMemory 运行流程及源码

在 LangChain 的 BaseChatMemory 组件中,不同的属性与方法有不同的作用:

  1. chat_memory:用于管理记忆中的历史消息对话。
  2. output_key:定义 AI 内容输出键。
  3. input_key:定义 Human 内容输入键。
  4. return_messages:load_memory_variables 函数是否返回消息列表,默认为 False 代表返回字符串。
  5. save_context:存储上下文到记忆组件中(存储消息对话)。
  6. load_memory_variables:生成加载到链的记忆字典信息。
  7. clear:清除记忆中的对话消息历史。
    ​​BaseChatMemory 运行流程及源码_第1张图片
    资料推荐
  • 大模型中转API推荐
  • ✨中转使用教程
class BaseChatMemory(BaseMemory, ABC):

    chat_memory: BaseChatMessageHistory = Field(
        default_factory=InMemoryChatMessageHistory
    )
    output_key: Optional[str] = None
    input_key: Optional[str] = None
    return_messages: bool = False

    def _get_input_output(
        self, inputs: Dict[str, Any], outputs: Dict[str, str]
    ) -> Tuple[str, str]:
        """从输入和输出字典中提取对应的字符串(人类提问、AI输出)"""
        if self.input_key is None:
            # 如果没有传递input_key则从inputs中提取人类提问的key
            prompt_input_key = get_prompt_input_key(inputs, self.memory_variables)
        else:
            prompt_input_key = self.input_key
        if self.output_key is None:
            if len(outputs) == 1:
                output_key = list(outputs.keys())[0]
            elif "output" in outputs:
                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:
                raise ValueError(
                    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_key
        return inputs[prompt_input_key], outputs[output_key]

    def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
        """保存对话上下文到记忆缓冲区"""
        # 从输入和输出中获取输入字符串、输出字符串
        input_str, output_str = self._get_input_output(inputs, outputs)
        # 存储对应的对话信息
        self.chat_memory.add_messages(
            [HumanMessage(content=input_str), AIMessage(content=output_str)]
        )

    async def asave_context(
        self, inputs: Dict[str, Any], outputs: Dict[str, str]
    ) -> None:
        """异步保存对话上下文到记忆缓冲区"""
        input_str, output_str = self._get_input_output(inputs, outputs)
        await self.chat_memory.aadd_messages(
            [HumanMessage(content=input_str), AIMessage(content=output_str)]
        )

    def clear(self) -> None:
        """清除记忆中的对话历史"""
        self.chat_memory.clear()

    async def aclear(self) -> None:
        """异步清除记忆中的对话历史"""
        await self.chat_memory.aclear()

你可能感兴趣的:(#,langchain实用技巧,langchain,claude,chatgpt,python,中转api,apikey,ai)