LangChain学习记录(二)Memory

        刚接触语言大模型时,对话过程中大模型表现出的“记忆力”令人惊叹。这种记忆力原理是什么,在langchain中是如何实现的,就是本篇笔记的内容了。

一、对话Memory的实现

        与大模型进行多轮对话时,并不是前面的对话真的被大模型记住了,其实还是通过prompt来实现的。也就是在进行多轮对话时,在没有令牌限制或其他限制的情况下,会将前面的对话内容放进目前对话的prompt中。看一下langchain中的实现。

import  os
import openai
from  langchain_community.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import  ConversationBufferMemory

os.environ["OPENAI_API_KEY"] = ''
openai.api_key = os.environ.get("OPENAI_API_KEY")

llm = ChatOpenAI(model_name = 'gpt-3.5-turbo',temperature = 0.0)
memory = ConversationBufferMemory() # 全部存储

conversation = ConversationChain(
    llm = llm,
    memory = memory,
    verbose = True # 开启查看每次prompt内容
)

while 1:
    content = input('user:')
    print(conversation.predict(input=content))

        langchain当中通过memory模块当中的一系列方法来实现多轮对话的记忆存储。其中的ConversationBufferMemory方法,会保存历史对话的所有内容。而启用的方式则是构建一个对话的ConversationChain,将memory的方法作为参数传进去。还可以指定大模型、以及显示prompt的内容等。

user:Hi,my name is Rain.

> 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,my name is Rain.
AI:

> Finished chain.
Hello Rain! It's nice to meet you. How can I assist you today?

user:what is 1+1?

> 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,my name is Rain.
AI: Hello Rain! It's nice to meet you. How can I assist you today?
Human: what is 1+1?
AI:

> Finished chain.
1 + 1 equals 2. Is there anything else you would like to know?

user:what is my name?

> 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,my name is Rain.
AI: Hello Rain! It's nice to meet you. How can I assist you today?
Human: what is 1+1?
AI: 1 + 1 equals 2. Is there anything else you would like to know?
Human: what is my name?
AI:

> Finished chain.
Your name is Rain.

        另外,我们还可以利用memory.save_context方法在对话开始前就主动的预置一些对话内容到记忆中去

memory.save_context({'input':'Hi,my name is Rain'},{'output':'Hello Rain! It\'s nice to meet you. How can I assist you today?'})


>>
user: what is my name?

> 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,my name is Rain
AI: Hello Rain! It's nice to meet you. How can I assist you today?
Human: what is my name?
AI:

> Finished chain.
Your name is Rain.

二、几种其他的memory

1.ConversationBufferWindowMemory

        这个方法不同于上文提到的方法,可以通过参数设置需要记忆的对话轮数

from langchain.memory import  ConversationBufferWindowMemory

memory = ConversationBufferWindowMemory(k = 1) # K参数设置记忆的对话轮数

如设置k=1,就表示多轮对话中当前对话只保存上一轮对话的内容记忆

2.ConversationTokenBufferMemory

        这个方法可以设置最大令牌上限,其实也是限制记忆的数量,毕竟大模型都是按Token数量收费的,当对话轮数很多时,每次对话记忆耗费的Token数量也会变得很大。指定llm参数是因为每种模型的token计算方式不同。

from langchain.memory import  ConversationTokenBufferMemory

memory = ConversationTokenBufferMemory(llm=llm,max_token_limit=30) # 设置令牌上限

3.ConversationSummaryBufferMemory

        这个方法会将之前所有的对话轮数根据你设置的令牌上限进行总结,保证新对话的prompt不会超过这个令牌上限,同时又能最大程度保存一些历史对话信息。

from langchain.memory import  ConversationSummaryBufferMemory

memory = ConversationSummaryBufferMemory(llm=llm,max_token_limit=30) 

...

三、小结

        通过对langchain实现对话记忆方法的学习,了解了大模型获得记忆能力的方法,对大模型的了解又进了一小步。。        

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以在以下地址找到langchain的课程笔记:https://learn.deeplearning.ai/langchain-chat-with-your-data/lesson/1/introduction [1。在开始之前,您需要创建一个名为langchain的虚拟环境,并安装langchain和openai的包。首先,使用以下命令创建虚拟环境:conda create -n langchain python。然后激活该虚拟环境:conda activate langchain。接下来,安装langchain和openai的包:pip install langchain和pip install openai -i https://pypi.tuna.tsinghua.edu.cn/simple [2。在笔记中,您还需要调用llm的api。您可以使用以下代码进行调用: from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.llms import OpenAI from langchain.chat_models import ChatOpenAI from langchain.chains.conversation.memory import ConversationBufferWindowMemory OPENAI_API_KEY = '***********' llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0, model_name="gpt-3.5-turbo") [3。 通过这些步骤,您将能够开始学习langchain课程并使用llm的api进行聊天。祝您学习愉快!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [吴恩达ChatGPT《LangChain Chat with Your Data》笔记](https://blog.csdn.net/weixin_39653948/article/details/131874862)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Langchain学习笔记](https://blog.csdn.net/weixin_38226321/article/details/131062424)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值