AI Agent Concepts · 09

Structured Output / Tool Definition

Tool definition 是提供給模型的工具菜單;tool_call 是模型輸出的結構化意圖,不是工具已經執行。

Before:純文字回應

明天台北的天氣如何?
明天台北的天氣預計多雲,最高溫 28°C,最低溫 22°C,降雨機率約 30%。

After:結構化 JSON

{
  "location": "台北",
  "date": "2025-01-16",
  "condition": "多雲",
  "high_temp": 28,
  "low_temp": 22,
  "rain_probability": 0.3
}

工具菜單

查天氣 get_weather
需要:城市名稱、日期;回傳:天氣資訊
搜尋網頁 web_search
需要:搜尋關鍵字、結果數量;回傳:搜尋結果
計算機 calculator
需要:數學表達式;回傳:計算結果

對應 JSON Schema

{
  "type": "function",
  "function": {
    "name": "get_weather",
    "description": "查詢指定城市的天氣預報",
    "parameters": {
      "type": "object",
      "properties": {
        "city": {"type": "string"},
        "date": {"type": "string"}
      },
      "required": ["city"]
    }
  }
}

Tool Call 流程

1. User
明天台北天氣如何?
2. Model
tool_call: get_weather(city="台北")
模型只表達意圖
3. 外部程式
真正呼叫天氣 API
4. tool_result
{"condition":"多雲"}
5. Model
根據工具結果生成最終回覆
Final
明天台北多雲,最高溫 28°C...