LLM 默認(rèn)是無狀態(tài)的,即詢問當(dāng)前的問題與上下文無關(guān),當(dāng)我們需要將多輪對話信息給到LLM 時(shí),就需要使用緩存Memory。緩存方式有多種。文章來源地址http://www.zghlxwxcb.cn/news/detail-567772.html
from langchain import OpenAI
from langchain.chains import ConversationChain
# first initialize the large language model
llm = OpenAI(
temperature=0,
openai_api_key="OPENAI_API_KEY",
model_name="text-davinci-003" # 也可用gpt-3.5-turbo
)
# now initialize the conversation chain 默認(rèn)無緩存
conversation = ConversationChain(llm=llm)
# 方式1 ConversationBufferMemory: 會(huì)將之前所有對話都作為輸入送到LLM中,受模型接受token數(shù)量的限制
from langchain.chains.conversation.memory import ConversationBufferMemory
conversation_buf = ConversationChain(
llm=llm,
memory=ConversationBufferMemory()
)
# 方式2 ConversationSummaryMemory:將之前對話總結(jié)Summary后,加上新的詢問query輸入到LLM中
from langchain.chains.conversation.memory import ConversationSummaryMemory
conversation = ConversationChain(
llm=llm,
memory=ConversationSummaryMemory(llm=llm)
)
# 方式3 ConversationBufferWindowMemory:將最近k輪對話,加上新的詢問query輸入到LLM中
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
conversation = ConversationChain(
llm=llm,
memory=ConversationBufferWindowMemory(k=1)
)
# 方式4 ConversationSummaryBufferMemory:將很久之前的對話Summary,最近的對話保存全部送入LLM中
conversation_sum_bufw = ConversationChain(
llm=llm, memory=ConversationSummaryBufferMemory(
llm=llm,
max_token_limit=650
)
# 其它 Memory 類型
# ConversationKnowledgeGraphMemory
# ConversationEntityMemory
文章來源:http://www.zghlxwxcb.cn/news/detail-567772.html
到了這里,關(guān)于LangChain(3)對話緩存方式 Conversational Memory的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!