一般情况下,聊天是没有状态的。但是很多场景我们是需要上下文记忆的。比如聊天机器人如果回答时不参考上下文信息,很容易出现前言不搭后语后语。如果需要手动维护对话历史信息,这工作量想想头都大,但是做为一个专门的AI开发工具,LangChain肯定是帮你想到了。
首先创建一个对话模型,记得自己设置环境变量QIANFAN_AK和QIANFAN_SK。
from langchain_community.chat_models import QianfanChatEndpoint
chatModel = QianfanChatEndpoint(
model='ERNIE-Bot',
endpoint='completions'
)
ChatMessageHistory
LC提供了专门的类用来管理对话历史记录,我们可以手工创建一个类试试。
from langchain.memory import ChatMessageHistory
history = ChatMessageHistory()
history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.add_user_message("I'm XiaoMing")
history.add_ai_message("nice to meet you")
history.messages
'''输出
[HumanMessage(content='hi!'),
AIMessage(content='whats up?'),
HumanMessage(content="I'm XiaoMing"),
AIMessage(content='nice to meet you')]
'''
ConversationBufferMemory
为了方便在对话中管理上下文信息,LC提供了ConversationBufferMemory对上下文管理工作进行了封装。先寿佛那个测试一下:
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("hi!")
memory.chat_memory.add_ai_message("whats up?")
memory.chat_memory.add_user_message("I'm XiaoMing")
memory.chat_memory.add_ai_message("nice to meet you")
memory.load_memory_variables({})
'''输出
{'history': "Human: hi!\nAI: whats up?\nHuman: I'm XiaoMing\nAI: nice to meet you"}
'''
功能整合
创建对话
from langchain.memory import ConversationBufferMemory
conversation = ConversationChain(
llm=chatModel,
verbose=True,
memory=ConversationBufferMemory()
)
第一轮对话
conversation.predict(input="你好,我是熊主任!")
'''输出
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: 你好,我是熊主任!
AI:
> Finished chain.
'你好,熊主任!有什么我可以帮助你的吗?'
'''
第二轮对话
conversation.predict(input="你好,请问我是谁?")
'''输出
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: 你好,我是熊主任!
AI: 你好,熊主任!有什么我可以帮助你的吗?
Human: 你好,请问我是谁?
AI:
> Finished chain.
'熊主任,我知道你是熊主任。有什么我可以帮助你的吗?'
'''
第三轮对话
conversation.predict(input="许仙的老婆是谁?")
'''输出
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: 你好,我是熊大毛!
AI: 你好,熊大毛!有什么我可以帮助你的吗?
Human: 你好,请问我是谁?
AI: 你是熊大毛。如果你有其他任何问题,欢迎随时问我。
Human: 今天日元兑美元的汇率是多少?
AI: 很抱歉,我不能提供实时的日元兑美元的汇率信息。建议你查看金融机构的官方网站或使用金融应用程序来获取最新的汇率数据。
Human: 许仙的老婆是谁?
AI:
> Finished chain.
'许仙的老婆是白娘子。白娘子是中国古代神话传说中的一个重要角色,与许仙的故事被广泛地传颂和演绎。'
'''
最后问下AI我问了哪些问题,只有掌握了上下文才能正确回答。
conversation.predict(input="我到现在问了哪些问题?")
'''输出
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: 你好,我是熊大毛!
AI: 你好,熊大毛!有什么我可以帮助你的吗?
Human: 你好,请问我是谁?
AI: 你是熊大毛。如果你有其他任何问题,欢迎随时问我。
Human: 今天日元兑美元的汇率是多少?
AI: 很抱歉,我不能提供实时的日元兑美元的汇率信息。建议你查看金融机构的官方网站或使用金融应用程序来获取最新的汇率数据。
Human: 许仙的老婆是谁?
AI: 许仙的老婆是白娘子。白娘子是中国古代神话传说中的一个重要角色,与许仙的故事被广泛地传颂和演绎。
Human: 我到现在问了哪些问题?
AI:
> Finished chain.
'你到目前为止问了三个问题。首先,你问了好并介绍了自己。然后,你问了今天日元兑美元的汇率是多少。最后,你问了许仙的老婆是谁。如果你还有其他任何问题,欢迎随时问我。'
'''