定義一個讓聊天機器人停止的函式
現在你的聊天機器人的狀態已設定好,接下來需要建立能管理整個聊天機器人圖形工作流程的函式。首先,你將建立 should_continue() 函式,用來檢查已為你載入到 state 中的聊天機器人最後一則訊息是否有工具呼叫。如果沒有工具呼叫,聊天機器人就會停止;如果有呼叫工具,聊天機器人就會進行下一個任務。為了管理你的訊息,以下模組已為你匯入。
from langgraph.graph import MessagesState, START, END
本練習屬於課程
Designing Agentic Systems with LangChain
練習說明
- 使用
MessagesState指定state參數的輸入型別。 - 使用
state裡的"messages"來存取最後一則訊息以檢查工具呼叫。 - 檢查最後一則訊息是否包含
tool_calls,若為真,請將回傳值指定為"tools"。 - 當不存在
tool_calls時,將回傳值指定為END以結束對話。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Use MessagesState to define the state of the function
def should_continue(state: ____):
# Get the last message from the state
last_message = ____["____"][____]
# Check if the last message includes tool calls
if ____.____:
return "____"
# End the conversation if no tool calls are present
return ____