刚接触语言大模型时,对话过程中大模型表现出的“记忆力”令人惊叹。这种记忆力原理是什么,在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实现对话记忆方法的学习,了解了大模型获得记忆能力的方法,对大模型的了解又进了一小步。。