為聊天機器人建立圖形

Designing Agentic Systems with LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

使用 LangGraph 的聊天機器人

聊天機器人頭像

 

  • 使用 LangGraph 自訂代理

     
    • 工作流程管理
      • 圖形狀態
      • 代理狀態
    • 聊天機器人建置
      • 節點
Designing Agentic Systems with LangChain

圖形與代理狀態

LangGraph 圖示

   

圖形狀態

  • 組織不同任務
    • 工具使用
    • LLM 呼叫
  • 任務順序 = 工作流程

代理狀態

  • 追蹤代理進度
  • 紀錄任務完成
Designing Agentic Systems with LangChain

用 LangGraph 建立代理

人們坐在大書本上閱讀。

代表資訊的放大鏡。

Designing Agentic Systems with LangChain

節點與邊

聊天機器人示意圖。

   

節點

  • 函式或動作
    • 回應
    • 工具呼叫

  • 連接節點的規則
Designing Agentic Systems with LangChain

節點與邊

高亮第一條邊的聊天機器人示意圖。

   

節點

  • 任務或動作
    • 回應
    • 工具呼叫

  • 連接節點的規則
Designing Agentic Systems with LangChain

節點與邊

高亮第二條邊的聊天機器人示意圖。

   

節點

  • 任務或動作
    • 回應
    • 工具呼叫

  • 連接節點的規則
Designing Agentic Systems with LangChain

節點與邊

完整聊天機器人圖形高亮。

   

節點

  • 任務或動作
    • 回應
    • 工具呼叫

  • 連接節點的規則

預先建置節點

來自 LangGraph 的 STARTEND 節點

Designing Agentic Systems with LangChain

建立圖形與代理狀態

# 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
Designing Agentic Systems with LangChain

建立圖形與代理狀態

# 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)
Designing Agentic Systems with LangChain

加入節點與邊

# 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)

聊天機器人節點。

Designing Agentic Systems with LangChain

加入節點與邊

# 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()

聊天機器人示意圖。

Designing Agentic Systems with LangChain

一起來練習吧!

Designing Agentic Systems with LangChain

Preparing Video For Download...