GenAI
RAG
AI Agents
原理可參考這篇綜論: How Agents for LLM Perform Task Planning。大型語言模型的代理如何進行任務規劃
LanaGraph Components。 LanaGraph組件
回顧上一小節的流程
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 從langchain_community中調用tools
New in LangGraph
- Cyclic Graphs
- Persistence
- Human-in-the-loop
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- LangGraph is an extension of LangChain that supports graphs.
- Single and Multi-agent flows are described and represented as graphs.
- Allows for extremely controlled “flows”
- Built-in persistence allows for human-in-the-loop workflows
Graph 基本組件
- Nodes。節點: 代理或功能
- Edges。邊: 連接節點
- Conditional edges。條件邊: 決定
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 極簡範例
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Data/State
- 代理狀態可被圖的所有部分訪問
Agent state Is accessible to all parts of the graph
- 是圖的本地狀態
it is local to the graph
- 可存儲在持久層中
Can be stored in a persistence layer
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Lab 使用LangGraph進行Agent設計實作
這個範例Agent
類別的主要功能是處理LLM與外部工具的互動。它使用狀態機(State)來管理調用流程,確保在發現錯誤(例如無效的工具名稱)時能夠適當處理並進行重試。整體流程是:
- 使用模型生成訊息。
- 檢查訊息中是否有工具調用。
- 執行工具調用或重新請求模型生成訊息
- 重複此流程直到結束。
- 類別初始化 (
__init__
方法)
-
參數:
model
: LLM模型,用於生成回應。
tools
: 一個工具列表,這些工具可以被LLM調用。
system
: (可選)系統訊息,作為對話的一部分提供給模型。
-
初始化:
- 設定
system
參數為類別屬性。
- 建立
StateGraph
對象來管理代理的狀態和行為。這是一個狀態機,用於控制代理的流程。
- 向狀態圖中添加節點和邊:
- 節點
"llm"
:調用OpenAI模型的函數call_openai
。
- 節點
"action"
:執行工具調用的函數take_action
。
- 條件邊:從
"llm"
節點到"action"
節點,當exists_action
返回True
時,進行此轉換;如果返回False
,則結束(END)。
- 邊:從
"action"
節點回到"llm"
節點。
- 設定進入點為
"llm"
。
- 將狀態圖編譯為可執行形式並儲存至
self.graph
。
- 將工具轉換為字典格式,儲存在
self.tools
中。
- 將模型與工具綁定,儲存在
self.model
中。
- 方法
exists_action
- 用途:檢查模型生成的最後一個訊息中是否存在工具調用。
- 邏輯:
- 取得最後一條訊息
result
。
- 如果
result
中包含工具調用,返回True
,否則返回False
。
- 方法
call_openai
- 用途:調用OpenAI模型並返回其輸出。
- 邏輯:
- 構建訊息列表,將
system
訊息(如果存在)添加到訊息序列的最前面。
- 調用模型,傳入這些訊息,並取得模型回應。
- 返回字典,包含模型生成的新訊息。
- 方法
take_action
- 用途:執行LLM請求的工具調用,並返回結果。
- 邏輯:
- 取得最後一條訊息中的所有工具調用。
- 初始化
results
列表來儲存每個工具調用的結果。
- 對每個工具調用進行迭代:
- 檢查工具名稱是否存在於
self.tools
中。
- 如果名稱無效,回傳"bad tool name, retry",並要求LLM重試。
- 如果名稱有效,調用相應的工具並傳入參數。
- 將每個工具調用的結果儲存為
ToolMessage
對象,並添加到results
列表中。
- 返回包含這些結果訊息的字典。

class Agent的mermaid詳細邏輯流程圖