Langchain 的 Conversation summary memory

Langchain的ConversationSummaryMemory模块允许创建对话摘要,尤其适用于管理长对话的历史记录。它能压缩信息,保存关键对话点。通过示例代码展示了如何保存和加载对话上下文,以及如何在对话链中使用该内存来构建更有条理的对话提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Langchain 的 Conversation summary memory

现在让我们看一下使用稍微复杂的内存类型 - ConversationSummaryMemory 。这种类型的记忆会随着时间的推移创建对话的摘要。这对于随着时间的推移压缩对话中的信息非常有用。对话摘要内存对发生的对话进行总结,并将当前摘要存储在内存中。然后可以使用该内存将迄今为止的对话摘要注入提示/链中。此内存对于较长的对话最有用,因为在提示中逐字保留过去的消息历史记录会占用太多令牌。

我们首先来探讨一下这种存储器的基本功能。

示例代码,

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

输出结果,

    {'history': '\nThe human greets the AI, to which the AI responds.'}

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

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

输出结果,

    {'history': [SystemMessage(content='\nThe human greets the AI, to which the AI responds.', additional_kwargs={})]}

我们也可以直接使用 predict_new_summary 方法。

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

输出结果,

    '\nThe human greets the AI, to which the AI responds.'

Initializing with messages

如果您有此类之外的消息,您可以使用 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

输出结果,

    '\nThe human greets the AI, to which the AI responds with a friendly greeting.'

Using in a chain

让我们看一下在链中使用它的示例,再次设置 verbose=True 以便我们可以看到提示。

示例代码,

from langchain.llms 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?")

输出结果,

    
    
    > 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, what's up?
    AI:
    
    > Finished chain.





    " 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!")

输出结果,

    
    
    > 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:
    
    The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue.
    Human: Tell me more about it!
    AI:
    
    > Finished chain.





    " 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?")

输出结果,

    
    
    > 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:
    
    The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue where their computer was not connecting to the internet. The AI was troubleshooting the issue and had already tried resetting the router and checking the network settings, but the issue still persisted and they were looking into other possible solutions.
    Human: Very cool -- what is the scope of the project?
    AI:
    
    > Finished chain.





    " 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."

完结!

<think>好的,用户想知道如何配置OllamaLLM的最大回复长度或字符限制。首先,我需要回想一下Ollama的文档和相关配置参数。记得Ollama的API和模型配置中可能有一些参数可以控制生成文本的长度,比如max_tokens、num_predict或者类似的选项。 接下来,应该检查Ollama的官方文档,确认具体的参数名称和用法。比如,是否在运行模型时通过命令行参数设置,或者在创建模型实例时通过环境变量配置。另外,用户可能在使用REST API调用,这时候需要在请求体中传递相应的参数。 然后,考虑到用户提到的参考内容中提到了内存组件和提示模板,可能需要结合这些设置来调整回复长度。比如,在自定义提示模板中加入长度限制的指令,或者利用ConversationSummaryMemory来管理上下文,间接影响输出长度。 还需要注意不同模型之间的差异,某些模型可能有自己的默认token限制,用户需要了解如何覆盖这些默认值。此外,token和字符数的关系,可能需要用户进行换算,比如一个token大约对应几个字符,这样设置max_tokens时能更准确地控制回复长度。 最后,要确保回答的步骤清晰,给出具体的配置方法和示例,比如修改配置文件的参数,或者使用命令行选项。同时,提醒用户测试调整后的设置,观察输出是否符合预期,可能需要多次尝试才能找到合适的数值。</think>要配置OllamaLLM的最大回复长度,可通过以下两种方式实现: ### 一、API参数配置 在调用API时添加`num_predict`参数控制生成的token数量: ```bash curl http://localhost:11434/api/generate -d '{ "model": "llama2", "prompt": "请简要说明...", "options": { "num_predict": 200 # 控制最大token数 } }' ``` ### 二、配置文件修改 在模型配置文件(如Modelfile)中设置: ```dockerfile FROM llama2 PARAMETER num_predict 200 ``` ### 三、环境变量设置 启动服务时通过环境变量配置: ```bash OLLAMA_NUM_PREDICT=200 ollama serve ``` ### 四、上下文管理优化 结合ConversationSummaryMemory组件可间接控制输出长度[^1],通过压缩历史对话减少无效内容生成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值