Skip to main content

如何为 LLMChain 添加记忆

本笔记本演示了如何在 LLMChain 中使用 Memory 类。在本演示中,我们将添加 ConversationBufferMemory 类,但实际上可以使用任何记忆类。

from langchain.memory import ConversationBufferMemory
from langchain import OpenAI, LLMChain, PromptTemplate

最重要的一步是正确设置提示。在下面的提示中,我们有两个输入键:一个用于实际输入,另一个用于来自 Memory 类的输入。重要的是,确保 PromptTemplate 和 ConversationBufferMemory 中的键匹配(chat_history)。


template = """You are a chatbot having a conversation with a human.

{chat_history}

Human: {human_input}
Chatbot:"""

prompt = PromptTemplate(
input_variables=["chat_history", "human_input"], template=template
)

memory = ConversationBufferMemory(memory_key="chat_history")
llm_chain = LLMChain(
llm=OpenAI(),
prompt=prompt,
verbose=True,
memory=memory,
)
llm_chain.predict(human_input="Hi there my friend")
> Entering new LLMChain chain...
Prompt after formatting:
You are a chatbot having a conversation with a human.


Human: Hi there my friend
Chatbot:
> Finished LLMChain chain.



' Hi there, how are you doing today?'
llm_chain.predict(human_input="Not too bad - how are you?")
> Entering new LLMChain chain...
Prompt after formatting:
You are a chatbot having a conversation with a human.


Human: Hi there my friend
AI: Hi there, how are you doing today?
Human: Not too bad - how are you?
Chatbot:
> Finished LLMChain chain.



" I'm doing great, thank you for asking!"