LangChain 的代理

Designing Agentic Systems with LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv Ltd.

認識你的講師

    課程講師照片。

   

  • Dilini K. Sumanapala,PhD

  • AI 工程師

  • 認知神經科學

  • 自然語言應用

  • Genverv Ltd. 創辦人

Designing Agentic Systems with LangChain

代理與工具概覽

代表代理的機器人圖示

 

  • 代理(Agents)

    能自主決策並採取行動的系統
  • 工具(Tools)

    代理用來執行特定任務的函式
    • 資料查詢
    • 研究報告
    • 資料分析
Designing Agentic Systems with LangChain

基本概念

聊天機器人頭像

   

  • LLM(如 ChatGPT)

  • 提示(Prompts)

  • 工具(Tools)

  • API

  • LangChain

    • 建構 AI 代理
Designing Agentic Systems with LangChain

課程概覽

    工具、Wikipedia 與人臉拼圖的圖示。

   

  • 數學問題

  • Wikipedia 搜尋

  • 在工具與 LLM 間切換

Designing Agentic Systems with LangChain

大腦與對話框(查詢與推理)的示意圖。

Designing Agentic Systems with LangChain

大腦、動作齒輪與對話框(查詢與推理、行動與回應)的示意圖。

Designing Agentic Systems with LangChain

顯示 ReAct 代理組成(大腦、齒輪與對話框)並於下方標示 ReAct 的示意圖。

Designing Agentic Systems with LangChain

提升回應準確度

    一段早期 ChatGPT 對話中錯誤數學回答的截圖。圖片底部加上正確答案「483」。

   

  • 程式設計

  • 數學

1 https://community.openai.com/t/chatgpt-simple-math-calculation-mistake/62780
Designing Agentic Systems with LangChain

分解問題

  數學運算符號的示意圖。

 

數學運算順序

1. 括號

2. 次方

3. 乘除

4. 加減

Designing Agentic Systems with LangChain

用 LangGraph 擴充代理

      左側通用流程圖,右側為 LangGraph 標誌的示意圖。

Designing Agentic Systems with LangChain

圖形結構

    流程圖,下方三個文件圖示代表「邊」的不同結果。

     

節點(Nodes)

  • 查詢資料庫

  • 回傳文件

     

邊(Edges)

連接節點的規則

Designing Agentic Systems with LangChain

建立 ReAct 代理

# Module imports

from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
import math
# LLM Setup model = ChatOpenAI(openai_api_key="<OPENAI_API_TOKEN">, model="gpt-4o-mini")
Designing Agentic Systems with LangChain

建立 ReAct 代理

# Create the agent
agent = create_react_agent(model, tools)


# Create a query query = "What is (2+8) multiplied by 9?"
# Invoke the agent and print the response response = agent.invoke({"messages": [("human", query)]})
# Print the agent's response print(response['messages'][-1].content)

DNT_CURLLY_TAG_3

<script.py> output:
    The result of (2 + 8) multiplied by 9 is 90.
Designing Agentic Systems with LangChain

一起來練習吧!

Designing Agentic Systems with LangChain

Preparing Video For Download...