Skip to main content

如何自定义对话记忆

本文介绍了几种自定义对话记忆的方法。

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

llm = OpenAI(temperature=0)

API 参考:

AI 前缀

第一种方法是通过更改对话摘要中的 AI 前缀来实现。默认情况下,它设置为 "AI",但您可以将其设置为任何您想要的内容。请注意,如果您更改了这个前缀,您还应该更改链中使用的提示以反映这个命名更改。让我们通过下面的示例来演示。

# 默认情况下设置为 "AI"
conversation = ConversationChain(
llm=llm, verbose=True, memory=ConversationBufferMemory()
)

conversation.predict(input="Hi there!")
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好的对话。AI 喜欢说话,并从上下文中提供大量具体细节。如果 AI 不知道问题的答案,它会诚实地说不知道。
当前对话:
人类:Hi there!
AI:

> 完成 ConversationChain 链。

" Hi there! 很高兴见到你。我今天能帮你什么吗?"
conversation.predict(input="What's the weather?")
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好的对话。AI 喜欢说话,并从上下文中提供大量具体细节。如果 AI 不知道问题的答案,它会诚实地说不知道。
当前对话:
人类:Hi there!
AI: Hi there! 很高兴见到你。我今天能帮你什么吗?
人类:What's the weather?
AI:

> 完成 ConversationChain 链。

' 当前天气是晴朗温暖,温度为华氏 75 度。未来几天的天气预报是晴朗,气温在 70 多度。'
# 现在我们可以覆盖它并将其设置为 "AI 助手"
from langchain.prompts.prompt import PromptTemplate

template = """以下是人类和 AI 之间友好的对话。AI 喜欢说话,并从上下文中提供大量具体细节。如果 AI 不知道问题的答案,它会诚实地说不知道。

当前对话:
{history}
人类:{input}
AI 助手:"""
PROMPT = PromptTemplate(input_variables=["history", "input"], template=template)
conversation = ConversationChain(
prompt=PROMPT,
llm=llm,
verbose=True,
memory=ConversationBufferMemory(ai_prefix="AI 助手"),
)

API 参考:

conversation.predict(input="Hi there!")
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好的对话。AI 喜欢说话,并从上下文中提供大量具体细节。如果 AI 不知道问题的答案,它会诚实地说不知道。

当前对话:
人类:Hi there!
AI 助手:

> 完成 ConversationChain 链。

" Hi there! 很高兴见到你。我今天能帮你什么吗?"
conversation.predict(input="What's the weather?")
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好的对话。AI 喜欢说话,并从上下文中提供大量具体细节。如果 AI 不知道问题的答案,它会诚实地说不知道。

当前对话:
人类:Hi there!
AI 助手: Hi there! 很高兴见到你。我今天能帮你什么吗?
人类:What's the weather?
AI 助手:

> 完成 ConversationChain 链。

' 当前天气是晴朗温暖,温度为华氏 75 度。今天剩下的时间预计大部分是晴朗的,最高温度为 82 度。'

人类前缀

下一种方法是通过更改对话摘要中的人类前缀来实现。默认情况下,它设置为 "Human",但您可以将其设置为任何您想要的内容。请注意,如果您更改了这个前缀,您还应该更改链中使用的提示以反映这个命名更改。让我们通过下面的示例来演示。

# 现在我们可以覆盖它并将其设置为 "Friend"
from langchain.prompts.prompt import PromptTemplate

template = """以下是人类和 AI 之间友好的对话。AI 喜欢说话,并从上下文中提供大量具体细节。如果 AI 不知道问题的答案,它会诚实地说不知道。

当前对话:
{history}
朋友:{input}
AI:"""
PROMPT = PromptTemplate(input_variables=["history", "input"], template=template)
conversation = ConversationChain(
prompt=PROMPT,
llm=llm,
verbose=True,
memory=ConversationBufferMemory(human_prefix="朋友"),
)

API 参考:

conversation.predict(input="Hi there!")
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好的对话。AI 喜欢说话,并从上下文中提供大量具体细节。如果 AI 不知道问题的答案,它会诚实地说不知道。

当前对话:
朋友:Hi there!
AI:

> 完成 ConversationChain 链。

" Hi there! 很高兴见到你。我今天能帮你什么吗?"
conversation.predict(input="What's the weather?")
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好的对话。AI 喜欢说话,并从上下文中提供大量具体细节。如果 AI 不知道问题的答案,它会诚实地说不知道。

当前对话:
朋友:Hi there!
AI: Hi there! 很高兴见到你。我今天能帮你什么吗?
朋友:What's the weather?
AI:

> 完成 ConversationChain 链。

' 当前天气是晴朗温暖,温度为华氏 75 度。今天剩下的时间预计大部分是晴朗的,最高温度为 82 度。'