核心模块(Modules)
记忆(Memory)
类型(Types)
摘要(Summary)

对话总结

现在让我们来看一下如何使用稍微复杂一些的记忆类型 - ConversationSummaryMemory。这种类型的记忆会随着时间对对话进行总结。这对于压缩对话中的信息非常有用。

对话概要记忆会在对话发生时总结并将当前总结存储在记忆中。然后可以使用这个记忆将到目前为止的对话总结注入到提示/链中。这种记忆在长时间对话中非常有用,如果在提示中直接保留过去的消息历史会占用太多的令牌。

让我们首先探索这种记忆类型的基本功能。

from langchain.memory import ConversationSummaryMemory, ChatMessageHistory
from langchain_openai import OpenAI
memory = ConversationSummaryMemory(llm=OpenAI(temperature=0))
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.load_memory_variables({})

    {'history': '\n人类问候AI,AI回应。'}

我们还可以将历史作为消息列表获取(如果您将其与聊天模型一起使用,则这很有用)。

memory = ConversationSummaryMemory(llm=OpenAI(temperature=0), return_messages=True)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.load_memory_variables({})

    {'history': [SystemMessage(content='\n人类问候AI,AI回应。', additional_kwargs={})]}

我们还可以直接利用 predict_new_summary 方法。

messages = memory.chat_memory.messages
previous_summary = ""
memory.predict_new_summary(messages, previous_summary)

    '\n人类问候AI,AI回应。'

使用消息/现有总结进行初始化

如果您有在这个类之外的消息,您可以很容易地使用 ChatMessageHistory 初始化这个类。在加载时,将计算一个总结。

history = ChatMessageHistory()
history.add_user_message("hi")
history.add_ai_message("hi there!")
memory = ConversationSummaryMemory.from_messages(
    llm=OpenAI(temperature=0),
    chat_memory=history,
    return_messages=True
)
memory.buffer

    '\n人类问候AI,AI以友好的问候回应。'

可选地,您可以使用以前生成的总结加快初始化,并通过直接初始化来避免重新生成总结。

memory = ConversationSummaryMemory(
    llm=OpenAI(temperature=0),
    buffer="人类问AI如何看待人工智能。AI认为人工智能是一个好事,因为它将帮助人类发挥他们的全部潜力。",
    chat_memory=history,
    return_messages=True
)

在链中使用

让我们通过一个在链中使用这个的例子,再次设置 verbose=True 以便我们可以看到提示信息。

from langchain_openai import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation_with_summary = ConversationChain(
    llm=llm,
    memory=ConversationSummaryMemory(llm=OpenAI()),
    verbose=True
)
conversation_with_summary.predict(input="Hi, what's up?")



    > 进入新的对话链...
    格式化后的提示信息:
    以下是人类和AI之间友好对话的示例。AI健谈并提供了很多具体的环境细节。如果AI不知道问题的答案,它会诚实地说不知道。

    当前对话:

    人类:Hi, what's up?
    AI:

    > 链结束。





    " Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?"

conversation_with_summary.predict(input="Tell me more about it!")



    > 进入新的对话链...
    格式化后的提示信息:
    以下是人类和AI之间友好对话的示例。AI健谈并提供了很多具体的环境细节。如果AI不知道问题的答案,它会诚实地说不知道。

    当前对话:

    人类问候了AI并询问它的近况。AI回应说自己状况良好,目前正在帮助一位遇到技术问题的顾客。
    人类:Tell me more about it!
    AI:

    > 链结束。





    " Sure! The customer is having trouble with their computer not connecting to the internet. I'm helping them troubleshoot the issue and figure out what the problem is. So far, we've tried resetting the router and checking the network settings, but the issue still persists. We're currently looking into other possible solutions."

conversation_with_summary.predict(input="Very cool -- what is the scope of the project?")



    > 进入新的对话链...
    格式化后的提示信息:
    以下是人类和AI之间友好对话的示例。AI健谈并提供了很多具体的环境细节。如果AI不知道问题的答案,它会诚实地说不知道。

    当前对话:

    人类问候了AI并询问它的近况。AI回应说自己状况良好,目前正在帮助一位遇到技术问题的顾客,顾客的电脑无法连接到互联网。AI正在为问题进行故障排除,并已尝试重置路由器和检查网络设置,但问题仍然存在,他们正在研究其他可能的解决方案。
    人类:Very cool -- what is the scope of the project?
    AI:

    > 链结束。





    " The scope of the project is to troubleshoot the customer's computer issue and find a solution that will allow them to connect to the internet. We are currently exploring different possibilities and have already tried resetting the router and checking the network settings, but the issue still persists."