加入節點與邊
現在你的 StateGraph() 已就緒,是時候把聊天機器人的節點加入圖形了!預先建好的 START 與 END 節點是直接從 LangGraph 匯入的,所以你只需要建立一個 chatbot 節點。你也會定義邊,以決定聊天機器人從開始到結束的對話流向。當節點與邊都加入後,編譯圖形,讓它準備好接受查詢並執行。
本練習屬於課程
Designing Agentic Systems with LangChain
練習說明
- 定義
chatbot函式:在state目前的"messages"上呼叫llm.invoke(),並回傳其回應。 - 使用
.add_node()將名為"chatbot"、參照chatbot函式的節點加入圖形。 - 使用
.add_edge()將START節點連到"chatbot"節點,並將"chatbot"連到END節點,以定義對話的邊。 - 使用
.compile()編譯圖形,讓它準備好執行。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define chatbot function to respond with the model
def chatbot(state: State):
return {"messages": [llm.____(____["____"])]}
# Add chatbot node to the graph
graph_builder.____("____", ____)
# Define the start and end of the conversation flow
graph_builder.____(____, "____")
graph_builder.____("____", ____)
# Compile the graph to prepare for execution
graph = graph_builder.____()