讓 AI 不只會「說」,還能真正「做事」——透過 Tool 機制,LLM 從一個會聊天的脑袋,變成能操控世界的智能代理。
先說結論
讀完這篇你會理解:
- Tool 是什麼:讓 LLM 能夠執行實際操作的接口
- 為什麼重要:沒有 Tool,AI 只能輸出文字;有 Tool,AI 才能真正「動手」
- 怎麼用:從定義到呼叫的完整流程,搭配可運行的 Python 程式碼範例
從類比開始
想像你有一個超級聰明的實習生:
- 他知道幾乎所有事情(知識廣泛)
- 他能理解你的指令(自然語言理解)
- 但他有個致命缺點:他只能說話,不能動手
你跟他說「幫我查一下明天北京的天氣」,他會回答你一堆關於天氣的描述,但不會真的去查。
這就是純 LLM 的限制。
現在,給這位實習生一個工具箱:
- 🛠️ 天氣查詢工具
- 🛠️ 計算機
- 🛠️ 網頁搜尋工具
- 🛠️ 發送郵件工具
有了工具箱後,他就能:
- 理解你的意圖 → 需要天氣資訊
- 選擇合適的工具 → 天氣查詢工具
- 執行工具 → 真的去查
- 拿回結果 → 收到天氣資料
- 回覆你 → 「明天北京天氣晴,25度」
這就是 AI Agent 的核心運作模式。
核心概念
1. Tool(工具)是什麼?
白話定義:Tool 是一個有名字、有說明、會做事的函數。
當你告訴 LLM:「你有這些工具可以用」,LLM 就能在適當時機主動呼叫這些工具。
標準結構(以 OpenAI Function Calling 為例):
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查詢指定城市的當前天氣",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名稱,例如:北京、上海、台北"
}
},
"required": ["city"]
}
}
}
2. 為什麼 Tool 重要?
| 能力 | 沒有 Tool | 有 Tool |
|---|---|---|
| 知識時效 | 只知道訓練資料截止前的知識 | 可以即時查詢最新資料 |
| 數學計算 | 可能算錯 | 精確執行計算 |
| 操作外部系統 | 無法做到 | 可以讀寫資料庫、發郵件、控制系統 |
| 行動能力 | 只能說 | 能真正「做事」 |
3. Tool 的運作流程
實戰範例
範例一:基礎天氣查詢(OpenAI 原生)
import openai
# 定義工具
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查詢指定城市的當前天氣",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名稱"
}
},
"required": ["city"]
}
}
}
]
# 建立對話
messages = [
{"role": "user", "content": "明天北京天氣怎麼樣?"}
]
# 第一次呼叫
response = openai.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
tool_choice="auto"
)
# 查看 LLM 是否要呼叫工具
assistant_message = response.choices[0].message
if assistant_message.tool_calls:
for tool_call in assistant_message.tool_calls:
print(f"LLM 要呼叫的工具: {tool_call.function.name}")
print(f"參數: {tool_call.function.arguments}")
# 模擬執行工具
if tool_call.function.name == "get_weather":
# 實際應用中這裡會調用真實的天氣 API
tool_result = "晴天,25°C,適合出遊"
# 把結果加回對話
messages.append({
"role": "assistant",
"tool_calls": assistant_message.tool_calls
})
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": tool_result
})
# 第二次呼叫,獲取最終回應
final_response = openai.chat.completions.create(
model="gpt-4",
messages=messages
)
print(final_response.choices[0].message.content)
範例二:用 LangChain 快速建立 Agent
from langchain.agents import create_agent
# 定義工具函數
def get_weather(city: str) -> str:
"""Get weather for a given city."""
# 實際應用中調用天氣 API
return f"北京的天氣是晴天,25°C"
def calculate(expression: str) -> str:
"""Calculate a math expression."""
try:
result = eval(expression)
return str(result)
except:
return "計算錯誤"
# 建立 Agent(不到 10 行!)
agent = create_agent(
model="openai:gpt-4",
tools=[get_weather, calculate],
system_prompt="你是個樂於助人的助手,可以查天氣和計算數學。"
)
# 測試多步驟任務
result = agent.invoke({
"messages": [{
"role": "user",
"content": "請幫我查北京天氣,然後把溫度乘以2是多少?"
}]
})
print(result["messages"][-1].content)
範例三:多工具協作(Agent 進化版)
from langchain.agents import create_agent
# 更複雜的工具
def search_flights(origin: str, destination: str, date: str) -> str:
"""搜尋航班資訊"""
return f"{origin} 到 {destination} 的航班:長榮 BR802,10:00起飛,13:30抵達"
def book_ticket(flight_number: str, passenger_name: str) -> str:
"""訂機票"""
return f"已成功訂購 {flight_number},旅客:{passenger_name}"
def send_email(to: str, subject: str, body: str) -> str:
"""發送郵件"""
return f"郵件已發送至 {to}"
# 建立旅遊助理 Agent
travel_agent = create_agent(
model="openai:gpt-4",
tools=[search_flights, book_ticket, send_email],
system_prompt="你是旅遊助理,幫用戶搜尋、預訂機票,並發送確認郵件。"
)
# 任務:幫用戶訂機票
task = """幫我查詢12月25日台北到東京的航班,
找到後幫我預訂,旅客姓名是王小明,
最後發送確認郵件到 sam@example.com"""
result = travel_agent.invoke({"messages": [{"role": "user", "content": task}]})
print(result["messages"][-1].content)
常見誤解
誤解一:Tool 是 AI 自己執行的
錯! Tool 是由你的程式碼執行,不是 AI 本身。
AI 的角色是:
- 判斷要不要用工具
- 選擇用哪個工具
- 生成正確的參數
實際執行是你伺服器的工作。
誤解二:Tool 越多越好
不一定。 太多工具會讓 LLM 困惑,不知道該用哪個。
最佳實踐:
- 工具數量控制在 5-10 個以內
- 工具名稱和描述要清晰明確
- 相關工具可以分組
誤解三:Tool 一定會被正確呼叫
可能失敗。 LLM 可能:
- 選錯工具
- 填錯參數
- 該用工具時不用
務必做好錯誤處理和驗證機制。
延伸思考
如果這篇文章點燃了你的興趣,以下方向值得繼續探索:
-
Model Context Protocol (MCP)
為什麼各家 AI 的 Tool 定義方式不統一套件?MCP 正試圖建立標準 -
ReAct (Reasoning + Acting)
讓 AI 在執行動作前先「想一想」,大幅提升複雜任務的成功率 -
LangGraph
當你需要更複雜的 Agent 流程控制、迴圈、多 Agent 協作時的進階框架 -
Agent 的安全邊界
Tool 讓 AI 能「做事」,但能做什麼事、誰來把關? -
Human-in-the-Loop
重要操作需要人類確認,設計良好的審核機制