益阳市网站建设,随州北京网站建设,那些免费网站可以做国外贸易,免费空间网Langchain 的 Conversation buffer window memory ConversationBufferWindowMemory 保存一段时间内对话交互的列表。它仅使用最后 K 个交互。这对于保持最近交互的滑动窗口非常有用#xff0c;因此缓冲区不会变得太大。
我们首先来探讨一下这种存储器的基本功能。
示例代码因此缓冲区不会变得太大。
我们首先来探讨一下这种存储器的基本功能。
示例代码
from langchain.memory import ConversationBufferWindowMemorymemory ConversationBufferWindowMemory( k1)
memory.save_context({input: hi}, {output: whats up})
memory.save_context({input: not much you}, {output: not much})memory.load_memory_variables({})输出结果 {history: Human: not much you\nAI: not much}我们还可以获取历史记录作为消息列表如果您将其与聊天模型一起使用这非常有用。
示例代码
memory ConversationBufferWindowMemory( k1, return_messagesTrue)
memory.save_context({input: hi}, {output: whats up})
memory.save_context({input: not much you}, {output: not much})memory.load_memory_variables({})输出结果 {history: [HumanMessage(contentnot much you, additional_kwargs{}),AIMessage(contentnot much, additional_kwargs{})]}Using in a chain
让我们看一下示例再次设置 verboseTrue 以便我们可以看到提示。
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
conversation_with_summary ConversationChain(llmOpenAI(temperature0), # We set a low k2, to only keep the last 2 interactions in memorymemoryConversationBufferWindowMemory(k2), verboseTrue
)
conversation_with_summary.predict(inputHi, whats up?)输出结果 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: Hi, whats up?AI: Finished chain. Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?示例代码
conversation_with_summary.predict(inputWhats their issues?)输出结果 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: Hi, whats up?AI: Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?Human: Whats their issues?AI: Finished chain. The customer is having trouble connecting to their Wi-Fi network. Im helping them troubleshoot the issue and get them connected.示例代码
conversation_with_summary.predict(inputIs it going well?)输出结果 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: Hi, whats up?AI: Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?Human: Whats their issues?AI: The customer is having trouble connecting to their Wi-Fi network. Im helping them troubleshoot the issue and get them connected.Human: Is it going well?AI: Finished chain. Yes, its going well so far. Weve already identified the problem and are now working on a solution.示例代码
# Notice here that the first interaction does not appear.
conversation_with_summary.predict(inputWhats the solution?)输出结果 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: Whats their issues?AI: The customer is having trouble connecting to their Wi-Fi network. Im helping them troubleshoot the issue and get them connected.Human: Is it going well?AI: Yes, its going well so far. Weve already identified the problem and are now working on a solution.Human: Whats the solution?AI: Finished chain. The solution is to reset the router and reconfigure the settings. Were currently in the process of doing that.完结 文章转载自: http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.nzlqt.cn.gov.cn.nzlqt.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.trkl.cn.gov.cn.trkl.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.bnbzd.cn.gov.cn.bnbzd.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.txgjx.cn.gov.cn.txgjx.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.wzknt.cn.gov.cn.wzknt.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.pqppj.cn.gov.cn.pqppj.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.kzxlc.cn.gov.cn.kzxlc.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.jcbmm.cn.gov.cn.jcbmm.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.ryxdf.cn.gov.cn.ryxdf.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.blxlf.cn.gov.cn.blxlf.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.dfojgo.cn.gov.cn.dfojgo.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.kjgrg.cn.gov.cn.kjgrg.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.rxnxl.cn.gov.cn.rxnxl.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.wjqyt.cn.gov.cn.wjqyt.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn