定義節點與邊,靈活呼叫函式

Designing Agentic Systems with LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

建立多工具工作流程

   

  • 多種可用工具

    • Palindrome
    • 歷史事件
    • Wikipedia

完整聊天機器人工作流程

Designing Agentic Systems with LangChain

定義工作流程函式

   

  • 建立停止函式

聊天機器人與工具節點。

Designing Agentic Systems with LangChain

定義工作流程函式

   

  • 建立停止函式

    • 檢查是否呼叫工具

突出顯示停止函式規則。

Designing Agentic Systems with LangChain

定義工作流程函式

   

  • 建立停止函式

    • 檢查是否呼叫工具
    • 若無則結束對話

工作流程中突出顯示 END 規則

Designing Agentic Systems with LangChain

定義工作流程函式

   

  • 建立停止函式

    • 檢查是否呼叫工具
    • 若無則結束對話    
  • 建立動態工具呼叫器

    • 若有工具呼叫則回傳工具回應

突出顯示 Call_model 規則。

Designing Agentic Systems with LangChain

定義工作流程函式

   

  • 建立停止函式

    • 檢查是否呼叫工具
    • 若無則結束對話    
  • 建立動態工具呼叫器

    • 若有工具呼叫則回傳工具回應
    • 若無工具呼叫,僅以聊天機器人節點呼叫 LLM

突出顯示 Call_model 規則。

Designing Agentic Systems with LangChain

定義工作流程函式

   

  • 建立停止函式

    • 檢查是否呼叫工具
    • 若無則結束對話    
  • 建立動態工具呼叫器

    • 若有工具呼叫則回傳工具回應
    • 若無工具呼叫,僅以聊天機器人節點呼叫 LLM
  • 編譯完整圖形

突出顯示 Call_model 規則。

Designing Agentic Systems with LangChain

建立停止條件函式

from langgraph.graph import MessagesState, START, END


# Use MessagesState to define the state of the stopping function def should_continue(state: MessagesState):
# Get the last message from the state last_message = state["messages"][-1]
# Check if the last message includes tool calls if last_message.tool_calls: return "tools"
# End the conversation if no tool calls are present return END
Designing Agentic Systems with LangChain

建立動態工具呼叫器

# Extract the last message from the history
def call_model(state: MessagesState):

last_message = state["messages"][-1]
# If the last message has tool calls, return the tool's response if isinstance(last_message, AIMessage) and last_message.tool_calls:
# Return the messages from the tool call return {"messages": [AIMessage(content=last_message.tool_calls[0]["response"])]]}
# Otherwise, proceed with a regular LLM response return {"messages": [model_with_tools.invoke(state["messages"])]}
Designing Agentic Systems with LangChain

建立圖形

workflow = StateGraph(MessagesState)












Designing Agentic Systems with LangChain

建立圖形

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)

聊天機器人與工具節點。

Designing Agentic Systems with LangChain

建立圖形

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)
# Connect the START node to the chatbot workflow.add_edge(START, "chatbot")

將 START 節點連到聊天機器人。

Designing Agentic Systems with LangChain

建立圖形

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)
# Connect the START node to the chatbot workflow.add_edge(START, "chatbot")
# Define conditions, then loop back to chatbot workflow.add_conditional_edges("chatbot", should_continue, ["tools", END])

已加入條件的圖形。

Designing Agentic Systems with LangChain

建立圖形

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)
# Connect the START node to the chatbot workflow.add_edge(START, "chatbot")
# Define conditions, then loop back to chatbot workflow.add_conditional_edges("chatbot", should_continue, ["tools", END])
workflow.add_edge("tools", "chatbot")

完整圖形工作流程。

Designing Agentic Systems with LangChain

加入記憶體

# Set up memory and compile the workflow
memory = MemorySaver()

app = workflow.compile( checkpointer=memory)
display(Image(app.get_graph() .draw_mermaid_png()))

完整圖形工作流程。

Designing Agentic Systems with LangChain

一起來練習吧!

Designing Agentic Systems with LangChain

Preparing Video For Download...