Skip to main content

添加记忆(state)

LangChain

链组件可以使用 Memory 对象进行初始化,该对象将在对链组件的多次调用之间保留数据。这使得链组件具有状态。

入门

from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

conversation = ConversationChain(
llm=chat,
memory=ConversationBufferMemory()
)

conversation.run("Answer briefly. What are the first 3 colors of a rainbow?")
# -> The first three colors of a rainbow are red, orange, and yellow.
conversation.run("And the next 4?")
# -> The next four colors of a rainbow are green, blue, indigo, and violet.
    'The next four colors of a rainbow are green, blue, indigo, and violet.'

基本上,BaseMemory 定义了 langchain 存储内存的接口。它通过 load_memory_variables 方法读取存储的数据,并通过 save_context 方法存储新数据。您可以在 Memory 部分了解更多信息。