分類目錄:《大模型從入門到應(yīng)用》總目錄
LangChain系列文章:
- 基礎(chǔ)知識
- 快速入門
- 安裝與環(huán)境配置
- 鏈(Chains)、代理(Agent:)和記憶(Memory)
- 快速開發(fā)聊天模型
- 模型(Models)
- 基礎(chǔ)知識
- 大型語言模型(LLMs)
- 基礎(chǔ)知識
- LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(Human Input LLM)
- 緩存LLM的調(diào)用結(jié)果
- 加載與保存LLM類、流式傳輸LLM與Chat Model響應(yīng)和跟蹤tokens使用情況
- 聊天模型(Chat Models)
- 基礎(chǔ)知識
- 使用少量示例和響應(yīng)流式傳輸
- 文本嵌入模型
- Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等
- Embaas、Fake Embeddings、Google Vertex AI PaLM等
- 提示(Prompts)
- 基礎(chǔ)知識
- 提示模板
- 基礎(chǔ)知識
- 連接到特征存儲
- 創(chuàng)建自定義提示模板和含有Few-Shot示例的提示模板
- 部分填充的提示模板和提示合成
- 序列化提示信息
- 示例選擇器(Example Selectors)
- 輸出解析器(Output Parsers)
- 記憶(Memory)
- 基礎(chǔ)知識
- 記憶的類型
- 會話緩存記憶、會話緩存窗口記憶和實體記憶
- 對話知識圖譜記憶、對話摘要記憶和會話摘要緩沖記憶
- 對話令牌緩沖存儲器和基于向量存儲的記憶
- 將記憶添加到LangChain組件中
- 自定義對話記憶與自定義記憶類
- 聊天消息記錄
- 記憶的存儲與應(yīng)用
- 索引(Indexes)
- 基礎(chǔ)知識
- 文檔加載器(Document Loaders)
- 文本分割器(Text Splitters)
- 向量存儲器(Vectorstores)
- 檢索器(Retrievers)
- 鏈(Chains)
- 基礎(chǔ)知識
- 通用功能
- 自定義Chain和Chain的異步API
- LLMChain和RouterChain
- SequentialChain和TransformationChain
- 鏈的保存(序列化)與加載(反序列化)
- 鏈與索引
- 文檔分析和基于文檔的聊天
- 問答的基礎(chǔ)知識
- 圖問答(Graph QA)和帶來源的問答(Q&A with Sources)
- 檢索式問答
- 文本摘要(Summarization)、HyDE和向量數(shù)據(jù)庫的文本生成
- 代理(Agents)
- 基礎(chǔ)知識
- 代理類型
- 自定義代理(Custom Agent)
- 自定義MRKL代理
- 帶有ChatModel的LLM聊天自定義代理和自定義多操作代理(Custom MultiAction Agent)
- 工具
- 基礎(chǔ)知識
- 自定義工具(Custom Tools)
- 多輸入工具和工具輸入模式
- 人工確認(rèn)工具驗證和Tools作為OpenAI函數(shù)
- 工具包(Toolkit)
- 代理執(zhí)行器(Agent Executor)
- 結(jié)合使用Agent和VectorStore
- 使用Agents的異步API和創(chuàng)建ChatGPT克隆
- 處理解析錯誤、訪問中間步驟和限制最大迭代次數(shù)
- 為代理程序設(shè)置超時時間和限制最大迭代次數(shù)和為代理程序和其工具添加共享內(nèi)存
- 計劃與執(zhí)行
- 回調(diào)函數(shù)(Callbacks)
默認(rèn)情況下,鏈(Chains)和代理(Agents)是無狀態(tài)的,這意味著它們將每個傳入的查詢視為獨立的(底層的LLM和聊天模型也是如此)。在某些應(yīng)用程序中(如:聊天機器人),記住先前的交互則非常重要。記憶(Memory)正是為此而設(shè)計的。 LangChain提供兩種形式的記憶組件。首先,LangChain提供了用于管理和操作先前聊天消息的輔助工具,這些工具都被設(shè)計為模塊化的使用方式。其次,LangChain提供了將這些工具輕松整合到鏈中的方法。
記憶涉及了在用戶與語言模型的交互過程中保持狀態(tài)的概念。用戶與語言模型的交互被捕捉在ChatMessage的概念中,因此這涉及到對一系列聊天消息進行攝取、捕捉、轉(zhuǎn)換和提取知識。有許多不同的方法可以實現(xiàn)這一點,每種方法都存在作為自己的記憶類型。通常情況下,對于每種類型的記憶,有兩種使用記憶的方法。一種是獨立的函數(shù),從一系列消息中提取信息,另一種是在鏈中使用這種類型的記憶的方法。記憶可以返回多個信息(如:最近的 N N N條消息和所有先前消息的摘要),返回的信息可以是字符串或消息列表。在本文中,我們將介紹最簡單形式的記憶:"緩沖"記憶。它只涉及保持先前所有消息的緩沖區(qū)。我們將展示如何在這里使用模塊化的實用函數(shù),然后展示它如何在鏈中使用(返回字符串和消息列表兩種形式)。
聊天消息歷史ChatMessageHistory
在大多數(shù)記憶模塊的核心實用類之一是ChatMessageHistory
類。這是一個超輕量級的包裝器,提供了保存人類消息、AI 消息以及獲取所有消息的便捷方法。如果我們在鏈外管理記憶,則可以直接使用此類。
from langchain.memory import ChatMessageHistory
history = ChatMessageHistory()
history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]
ConversationBufferMemory
現(xiàn)在我們展示如何在鏈中使用這個簡單的概念。首先展示ConversationBufferMemory
,它只是一個對ChatMessageHistory
的包裝器,用于提取消息到一個變量中。我們可以首先將其提取為一個字符串:
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("hi!")
memory.chat_memory.add_ai_message("whats up?")
memory.load_memory_variables({})
輸出:
{'history': 'Human: hi!\nAI: whats up?'}
我們還可以將歷史記錄作為消息列表獲取:
memory = ConversationBufferMemory(return_messages=True)
memory.chat_memory.add_user_message("hi!")
memory.chat_memory.add_ai_message("whats up?")
memory.load_memory_variables({})
輸出:
{'history': [HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]}
在鏈中使用
最后,讓我們看看如何在鏈中使用這個模塊,其中我們設(shè)置了verbose=True
以便查看提示。
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory()
)
conversation.predict(input="Hi there!")
日志輸出:
> 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 there!
AI:
> Finished chain.
輸出:
" Hi there! It's nice to meet you. How can I help you today?"
輸入:
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
日志輸出:
> 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 there!
AI: Hi there! It's nice to meet you. How can I help you today?
Human: I'm doing well! Just having a conversation with an AI.
AI: That's great! It's always nice to have a conversation with someone new. What would you like to talk about?
Human: Tell me about yourself.
AI:
> Finished chain.
輸出:
" Sure! I'm an AI created to help people with their everyday tasks. I'm programmed to understand natural language and provide helpful information. I'm also constantly learning and updating my knowledge base so I can provide more accurate and helpful answers."
保存消息記錄
我們可能經(jīng)常需要保存消息,并在以后使用時加載它們。我們可以通過將消息首先轉(zhuǎn)換為普通的Python字典來輕松實現(xiàn)此操作,然后將其保存(如:保存為JSON格式),然后再加載。以下是一個示例:
import json
from langchain.memory import ChatMessageHistory
from langchain.schema import messages_from_dict, messages_to_dict
history = ChatMessageHistory()
history.add_user_message("hi!")
history.add_ai_message("whats up?")
dicts = messages_to_dict(history.messages)
dicts
輸出:
[{'type': 'human',
'data': {'content': 'hi!', 'additional_kwargs': {}, 'example': False}},
{'type': 'ai',
'data': {'content': 'whats up?', 'additional_kwargs': {}, 'example': False}}]
輸入:
new_messages = messages_from_dict(dicts)
new_messages
輸出:文章來源:http://www.zghlxwxcb.cn/news/detail-629852.html
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]
參考文獻:
[1] LangChain官方網(wǎng)站:https://www.langchain.com/
[2] LangChain ????? 中文網(wǎng),跟著LangChain一起學(xué)LLM/GPT開發(fā):https://www.langchain.com.cn/
[3] LangChain中文網(wǎng) - LangChain 是一個用于開發(fā)由語言模型驅(qū)動的應(yīng)用程序的框架:http://www.cnlangchain.com/文章來源地址http://www.zghlxwxcb.cn/news/detail-629852.html
到了這里,關(guān)于自然語言處理從入門到應(yīng)用——LangChain:記憶(Memory)-[基礎(chǔ)知識]的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!