LangChain入门教程 - 对话历史

一般情况下,聊天是没有状态的。但是很多场景我们是需要上下文记忆的。比如聊天机器人如果回答时不参考上下文信息,很容易出现前言不搭后语后语。如果需要手动维护对话历史信息,这工作量想想头都大,但是做为一个专门的AI开发工具,LangChain肯定是帮你想到了。

首先创建一个对话模型,记得自己设置环境变量QIANFAN_AKQIANFAN_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.
'你到目前为止问了三个问题。首先,你问了好并介绍了自己。然后,你问了今天日元兑美元的汇率是多少。最后,你问了许仙的老婆是谁。如果你还有其他任何问题,欢迎随时问我。'
'''
  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中提到了一个名为history.js的插件,可以兼容各大浏览器,并且在使用Hash值进行兼容时,需要判断是否是兼容模式。而引用中提到的带有打乱数据的重复K折验证是一种用于评估模型的方法,特别适用于数据相对较少的情况。具体做法是多次使用K折验证,在每次将数据划分为K个分区之前都先将数据打乱,最终得到每次K折验证的分数的平均值。这种方法的计算代价较高,需要训练和评估多个模型。至于引用中提到的向量、序列和图像数据的存储和处理方法,可以根据数据的特点选择不同的张量形状和相应的处理层进行处理。根据提供的引用内容,无法确定具体的history.history是指什么。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [浏览器history.js](https://download.csdn.net/download/yangxuecjt/10548795)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [《python深度学习》学习笔记](https://blog.csdn.net/Chloris_/article/details/116177664)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值