การกำหนด node และ edge สำหรับการเรียกใช้ฟังก์ชันแบบยืดหยุ่น

การออกแบบระบบ Agentic ด้วย LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

สร้างเวิร์กโฟลว์แบบหลายเครื่องมือ

   

  • เครื่องมือที่ใช้งานได้หลายรายการ

    • Palindrome
    • เหตุการณ์ทางประวัติศาสตร์
    • Wikipedia

เวิร์กโฟลว์ chatbot แบบครบวงจร

การออกแบบระบบ Agentic ด้วย LangChain

กำหนดฟังก์ชันสำหรับเวิร์กโฟลว์

   

  • สร้างฟังก์ชันหยุดการทำงาน

Node chatbot และ tools ในเวิร์กโฟลว์

การออกแบบระบบ Agentic ด้วย LangChain

กำหนดฟังก์ชันสำหรับเวิร์กโฟลว์

   

  • สร้างฟังก์ชันหยุดการทำงาน

    • ตรวจสอบการเรียกใช้เครื่องมือ

เน้นกฎของฟังก์ชันหยุดการทำงาน

การออกแบบระบบ Agentic ด้วย LangChain

กำหนดฟังก์ชันสำหรับเวิร์กโฟลว์

   

  • สร้างฟังก์ชันหยุดการทำงาน

    • ตรวจสอบการเรียกใช้เครื่องมือ
    • จบการสนทนาหากไม่มีการเรียก

เน้นกฎ END ในเวิร์กโฟลว์

การออกแบบระบบ Agentic ด้วย LangChain

กำหนดฟังก์ชันสำหรับเวิร์กโฟลว์

   

  • สร้างฟังก์ชันหยุดการทำงาน

    • ตรวจสอบการเรียกใช้เครื่องมือ
    • จบการสนทนาหากไม่มีการเรียก    
  • สร้างตัวเรียกใช้เครื่องมือแบบไดนามิก

    • คืนค่าผลลัพธ์จากเครื่องมือหากมีการเรียก

เน้นกฎของ call_model

การออกแบบระบบ Agentic ด้วย LangChain

กำหนดฟังก์ชันสำหรับเวิร์กโฟลว์

   

  • สร้างฟังก์ชันหยุดการทำงาน

    • ตรวจสอบการเรียกใช้เครื่องมือ
    • จบการสนทนาหากไม่มีการเรียก    
  • สร้างตัวเรียกใช้เครื่องมือแบบไดนามิก

    • คืนค่าผลลัพธ์จากเครื่องมือหากมีการเรียก
    • เรียกใช้ LLM ด้วย เฉพาะ node chatbot หากไม่มีการเรียกเครื่องมือ

เน้นกฎของ call_model

การออกแบบระบบ Agentic ด้วย LangChain

กำหนดฟังก์ชันสำหรับเวิร์กโฟลว์

   

  • สร้างฟังก์ชันหยุดการทำงาน

    • ตรวจสอบการเรียกใช้เครื่องมือ
    • จบการสนทนาหากไม่มีการเรียก    
  • สร้างตัวเรียกใช้เครื่องมือแบบไดนามิก

    • คืนค่าผลลัพธ์จากเครื่องมือหากมีการเรียก
    • เรียกใช้ LLM ด้วย เฉพาะ node chatbot หากไม่มีการเรียกเครื่องมือ
  • คอมไพล์กราฟทั้งหมด

เน้นกฎของ call_model

การออกแบบระบบ Agentic ด้วย 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
การออกแบบระบบ Agentic ด้วย 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"])]}
การออกแบบระบบ Agentic ด้วย LangChain

สร้างกราฟ

workflow = StateGraph(MessagesState)












การออกแบบระบบ Agentic ด้วย LangChain

สร้างกราฟ

workflow = StateGraph(MessagesState)


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

Node chatbot และ tools

การออกแบบระบบ Agentic ด้วย 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")

เพิ่ม node START เชื่อมกับ chatbot

การออกแบบระบบ Agentic ด้วย 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])

กราฟที่เพิ่มเงื่อนไขแล้ว

การออกแบบระบบ Agentic ด้วย 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")

เวิร์กโฟลว์กราฟครบวงจร

การออกแบบระบบ Agentic ด้วย LangChain

การเพิ่มหน่วยความจำ

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

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

เวิร์กโฟลว์กราฟครบวงจร

การออกแบบระบบ Agentic ด้วย LangChain

มาฝึกกันเถอะ!

การออกแบบระบบ Agentic ด้วย LangChain

Preparing Video For Download...