การสร้างกราฟสำหรับแชทบอต

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

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

แชทบอตด้วย LangGraph

หัวแชทบอต

 

  • Custom agents ด้วย LangGraph

     
    • การจัดการ Workflow
      • Graph states
      • Agent states
    • การสร้างแชทบอต
      • Nodes
      • Edges
การออกแบบระบบ Agentic ด้วย LangChain

กราฟและ agent states

ไอคอน LangGraph

   

Graph State

  • จัดระเบียบงานต่าง ๆ
    • การใช้เครื่องมือ
    • การเรียก LLM
  • ลำดับของงาน = workflow

Agent State

  • ติดตามความคืบหน้าของ agent
  • บันทึกการทำงานที่เสร็จสิ้น
การออกแบบระบบ Agentic ด้วย LangChain

การสร้าง agent ด้วย LangGraph

ผู้คนกำลังอ่านหนังสือ นั่งอยู่บนหนังสือเล่มใหญ่

แว่นขยายแทนข้อมูล

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

Nodes และ edges

แผนภาพแชทบอต

   

Nodes

  • ฟังก์ชันหรือการกระทำ
    • การตอบกลับ
    • การเรียกใช้เครื่องมือ

Edges

  • กฎที่เชื่อมต่อ nodes
การออกแบบระบบ Agentic ด้วย LangChain

Nodes และ edges

แผนภาพแชทบอตที่เน้น edge แรก

   

Nodes

  • งานหรือการกระทำ
    • การตอบกลับ
    • การเรียกใช้เครื่องมือ

Edges

  • กฎที่เชื่อมต่อ nodes
การออกแบบระบบ Agentic ด้วย LangChain

Nodes และ edges

แผนภาพแชทบอตที่เน้น edge ที่สอง

   

Nodes

  • งานหรือการกระทำ
    • การตอบกลับ
    • การเรียกใช้เครื่องมือ

Edges

  • กฎที่เชื่อมต่อ nodes
การออกแบบระบบ Agentic ด้วย LangChain

Nodes และ edges

แผนภาพแชทบอตแบบเต็มที่ถูกเน้น

   

Nodes

  • งานหรือการกระทำ
    • การตอบกลับ
    • การเรียกใช้เครื่องมือ

Edges

  • กฎที่เชื่อมต่อ nodes

Pre-built Nodes

Node START และ END จาก LangGraph

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

การสร้างกราฟและ agent states

# Modules for structuring text
from typing import Annotated
from typing_extensions import TypedDict


# LangGraph modules for defining graphs from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_messages
# Module for setting up OpenAI from langchain_openai import ChatOpenAI
การออกแบบระบบ Agentic ด้วย LangChain

การสร้างกราฟและ agent states

# Define the llm
llm = ChatOpenAI(model="gpt-4o-mini", api_key="OPENAI_API_KEY")


# Define the State class State(TypedDict):
# Define messages with metadata messages: Annotated[list, add_messages]
# Initialize StateGraph graph_builder = StateGraph(State)
การออกแบบระบบ Agentic ด้วย LangChain

การเพิ่ม nodes และ edges

# Define chatbot function to respond
# with the model
def chatbot(state: State):
    return {"messages": 
    [llm.invoke(state["messages"])]}


# Add chatbot node to the graph graph_builder.add_node("chatbot", chatbot)

Node แชทบอต

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

การเพิ่ม nodes และ edges

# Define the start and end of the 
# conversation flow
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("chatbot", END)


# Compile the graph to prepare for # execution graph = graph_builder.compile()

แผนภาพแชทบอต

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

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

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

Preparing Video For Download...