当前位置:   article > 正文

LangChain入门教程 - 对话历史_langchain history

langchain history

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

首先创建一个对话模型,记得自己设置环境变量QIANFAN_AKQIANFAN_SK

  1. from langchain_community.chat_models import QianfanChatEndpoint
  2. chatModel = QianfanChatEndpoint(
  3. model='ERNIE-Bot',
  4. endpoint='completions'
  5. )

ChatMessageHistory

LC提供了专门的类用来管理对话历史记录,我们可以手工创建一个类试试。

  1. from langchain.memory import ChatMessageHistory
  2. history = ChatMessageHistory()
  3. history.add_user_message("hi!")
  4. history.add_ai_message("whats up?")
  5. history.add_user_message("I'm XiaoMing")
  6. history.add_ai_message("nice to meet you")
  7. history.messages
  8. '''输出
  9. [HumanMessage(content='hi!'),
  10. AIMessage(content='whats up?'),
  11. HumanMessage(content="I'm XiaoMing"),
  12. AIMessage(content='nice to meet you')]
  13. '''

ConversationBufferMemory

为了方便在对话中管理上下文信息,LC提供了ConversationBufferMemory对上下文管理工作进行了封装。先寿佛那个测试一下:

  1. from langchain.memory import ConversationBufferMemory
  2. memory = ConversationBufferMemory()
  3. memory.chat_memory.add_user_message("hi!")
  4. memory.chat_memory.add_ai_message("whats up?")
  5. memory.chat_memory.add_user_message("I'm XiaoMing")
  6. memory.chat_memory.add_ai_message("nice to meet you")
  7. memory.load_memory_variables({})
  8. '''输出
  9. {'history': "Human: hi!\nAI: whats up?\nHuman: I'm XiaoMing\nAI: nice to meet you"}
  10. '''

功能整合

创建对话

  1. from langchain.memory import ConversationBufferMemory
  2. conversation = ConversationChain(
  3. llm=chatModel,
  4. verbose=True,
  5. memory=ConversationBufferMemory()
  6. )

第一轮对话

  1. conversation.predict(input="你好,我是熊主任!")
  2. '''输出
  3. > Entering new ConversationChain chain...
  4. Prompt after formatting:
  5. 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.
  6. Current conversation:
  7. Human: 你好,我是熊主任!
  8. AI:
  9. > Finished chain.
  10. '你好,熊主任!有什么我可以帮助你的吗?'
  11. '''

第二轮对话

  1. conversation.predict(input="你好,请问我是谁?")
  2. '''输出
  3. > Entering new ConversationChain chain...
  4. Prompt after formatting:
  5. 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.
  6. Current conversation:
  7. Human: 你好,我是熊主任!
  8. AI: 你好,熊主任!有什么我可以帮助你的吗?
  9. Human: 你好,请问我是谁?
  10. AI:
  11. > Finished chain.
  12. '熊主任,我知道你是熊主任。有什么我可以帮助你的吗?'
  13. '''

第三轮对话

  1. conversation.predict(input="许仙的老婆是谁?")
  2. '''输出
  3. > Entering new ConversationChain chain...
  4. Prompt after formatting:
  5. 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.
  6. Current conversation:
  7. Human: 你好,我是熊大毛!
  8. AI: 你好,熊大毛!有什么我可以帮助你的吗?
  9. Human: 你好,请问我是谁?
  10. AI: 你是熊大毛。如果你有其他任何问题,欢迎随时问我。
  11. Human: 今天日元兑美元的汇率是多少?
  12. AI: 很抱歉,我不能提供实时的日元兑美元的汇率信息。建议你查看金融机构的官方网站或使用金融应用程序来获取最新的汇率数据。
  13. Human: 许仙的老婆是谁?
  14. AI:
  15. > Finished chain.
  16. '许仙的老婆是白娘子。白娘子是中国古代神话传说中的一个重要角色,与许仙的故事被广泛地传颂和演绎。'
  17. '''

最后问下AI我问了哪些问题,只有掌握了上下文才能正确回答。

  1. conversation.predict(input="我到现在问了哪些问题?")
  2. '''输出
  3. > Entering new ConversationChain chain...
  4. Prompt after formatting:
  5. 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.
  6. Current conversation:
  7. Human: 你好,我是熊大毛!
  8. AI: 你好,熊大毛!有什么我可以帮助你的吗?
  9. Human: 你好,请问我是谁?
  10. AI: 你是熊大毛。如果你有其他任何问题,欢迎随时问我。
  11. Human: 今天日元兑美元的汇率是多少?
  12. AI: 很抱歉,我不能提供实时的日元兑美元的汇率信息。建议你查看金融机构的官方网站或使用金融应用程序来获取最新的汇率数据。
  13. Human: 许仙的老婆是谁?
  14. AI: 许仙的老婆是白娘子。白娘子是中国古代神话传说中的一个重要角色,与许仙的故事被广泛地传颂和演绎。
  15. Human: 我到现在问了哪些问题?
  16. AI:
  17. > Finished chain.
  18. '你到目前为止问了三个问题。首先,你问了好并介绍了自己。然后,你问了今天日元兑美元的汇率是多少。最后,你问了许仙的老婆是谁。如果你还有其他任何问题,欢迎随时问我。'
  19. '''
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/812672
推荐阅读
相关标签
  

闽ICP备14008679号