函式呼叫功能可提升 LLM 提供相關且符合情境答案的能力。
您可以使用 Function Calling API,為生成式 AI 模型提供自訂函式。模型不會直接呼叫這些函式,而是產生結構化資料輸出內容,指定函式名稱和建議的引數。
這項輸出內容可呼叫外部 API 或資訊系統,例如資料庫、客戶關係管理系統和文件存放區。LLM 可使用產生的 API 輸出內容,提升回覆品質。
如要進一步瞭解函式呼叫概念,請參閱函式呼叫。
支援的模型
- Gemini 2.5 Flash (預先發布版)
- Gemini 2.5 Flash-Lite (預先發布版)
- Gemini 2.5 Flash-Lite
- Gemini 2.5 Flash,搭配 Live API 原生音訊 (預先發布版)
- Gemini 2.0 Flash with Live API (Preview)
- Gemini 2.5 Pro
- Gemini 2.5 Flash
- Gemini 2.0 Flash
- Gemini 2.0 Flash-Lite
限制:
- 要求中提供的函式宣告數量上限為 128 個。
語法範例
傳送函式呼叫 API 要求的語法。
curl
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \ -d '{ "contents": [{ ... }], "tools": [{ "function_declarations": [ { ... } ] }] }'
參數清單
如需實作詳情,請參閱範例。
FunctionDeclaration
根據 OpenAPI 3.0 規格定義函式,模型可為該函式產生 JSON 輸入內容。
| 參數 | |
|---|---|
| 
 | 
 要呼叫的函式名稱。開頭須為英文字母或底線,必須是 a-z、A-Z、0-9,或包含底線、半形句點或破折號,長度上限為 64 個字元。 | 
| 
 | 自由參加: 函式的說明和用途。模型會根據這項資訊,判斷如何呼叫函式,以及是否要呼叫函式。為獲得最佳結果,建議您加入說明。 | 
| 
 | 自由參加: 以 OpenAPI JSON 結構定義物件格式說明函式的參數:OpenAPI 3.0 規格。 | 
| 
 | 自由參加: 以 OpenAPI JSON 結構定義物件格式說明函式的輸出內容:OpenAPI 3.0 規格。 | 
詳情請參閱「函式呼叫」一文。
Schema
根據 OpenAPI 3.0 結構定義規格,定義函式呼叫中輸入和輸出資料的格式。
| 參數 | |
|---|---|
| 類型 | 
 列舉。資料類型。必須是下列其中一項: 
 | 
| description | 自由參加: 資料說明。 | 
| enum | 自由參加: 可能的值為列舉格式的基本類型元素。 | 
| items | 自由參加: 
 | 
| properties | 自由參加: 「 | 
| required | 自由參加: 
 | 
| nullable | 自由參加: 指出值是否可能為  | 
FunctionCallingConfig
FunctionCallingConfig 會控管模型的行為,並決定要呼叫的函式類型。
| 參數 | |
|---|---|
| 
 | 自由參加: 
 | 
| 
 | 自由參加: 要呼叫的函式名稱。只有在  | 
functionCall
模型傳回的預測 functionCall,其中包含代表 functionDeclaration.name 的字串,以及包含參數及其值的結構化 JSON 物件。
| 參數 | |
|---|---|
| 
 | 
 要呼叫的函式名稱。 | 
| 
 | 
 JSON 物件格式的函式參數和值。 如需參數詳細資料,請參閱「函式呼叫」。 | 
functionResponse
FunctionCall 的輸出結果會包含代表 FunctionDeclaration.name 的字串。也包含結構化 JSON 物件,內含函式的輸出內容 (並將其做為模型的背景資訊)。其中應包含根據模型預測結果製作的 FunctionCall。
| 參數 | |
|---|---|
| 
 | 
 要呼叫的函式名稱。 | 
| 
 | 
 JSON 物件格式的函式回應。 | 
範例
傳送函式宣告
以下是將查詢和函式宣告傳送至模型的基本範例。
REST
使用任何要求資料之前,請先替換以下項目:
- PROJECT_ID:您的專案 ID。
- MODEL_ID:正在處理的模型 ID。
- ROLE:建立訊息的實體身分。
- TEXT:要傳送給模型的提示。
- NAME:要呼叫的函式名稱。
- DESCRIPTION:函式的說明和用途。
- 如需其他欄位,請參閱「參數清單」表格。
HTTP 方法和網址:
POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/google/models/MODEL_ID:generateContent
JSON 要求主體:
{
  "contents": [{
    "role": "ROLE",
    "parts": [{
      "text": "TEXT"
    }]
  }],
  "tools": [{
    "function_declarations": [
      {
        "name": "NAME",
        "description": "DESCRIPTION",
        "parameters": {
          "type": "TYPE",
          "properties": {
            "location": {
              "type": "TYPE",
              "description": "DESCRIPTION"
            }
          },
          "required": [
            "location"
          ]
        }
      }
    ]
  }]
}
如要傳送要求,請選擇以下其中一個選項:
curl
      將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/google/models/MODEL_ID:generateContent"
PowerShell
      將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/google/models/MODEL_ID:generateContent" | Select-Object -Expand Content
cURL 指令範例
PROJECT_ID=myproject
LOCATION=us-central1
MODEL_ID=gemini-2.5-flash
curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \
  -d '{
    "contents": [{
      "role": "user",
      "parts": [{
        "text": "What is the weather in Boston?"
      }]
    }],
    "tools": [{
      "functionDeclarations": [
        {
          "name": "get_current_weather",
          "description": "Get the current weather in a given location",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              }
            },
            "required": [
              "location"
            ]
          }
        }
      ]
    }]
  }'
Python 適用的 Gen AI SDK
Node.js
Java
Go
REST (OpenAI)
您可以使用 OpenAI 程式庫呼叫 Function Calling API。詳情請參閱 使用 OpenAI 程式庫呼叫 Vertex AI 模型。
使用任何要求資料之前,請先替換以下項目:
- PROJECT_ID:。
- MODEL_ID:正在處理的模型 ID。
HTTP 方法和網址:
POST https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions
JSON 要求主體:
{
  "model": "google/MODEL_ID",
  "messages": [
    {
      "role": "user",
      "content": "What is the weather in Boston?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "OBJECT",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
            }
           },
          "required": ["location"]
        }
      }
    }
  ]
}
如要傳送要求,請選擇以下其中一個選項:
curl
      將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions"
PowerShell
      將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions" | Select-Object -Expand Content
Python (OpenAI)
您可以使用 OpenAI 程式庫呼叫 Function Calling API。詳情請參閱「 使用 OpenAI 程式庫呼叫 Vertex AI 模型」。
使用 FunctionCallingConfig 傳送函式宣告
以下範例說明如何將 FunctionCallingConfig 傳遞至模型。
functionCallingConfig 可確保模型輸出內容一律為特定函式呼叫。設定方式:
- 將函式呼叫 mode設為ANY。
- 在 - allowed_function_names中指定要使用的函式名稱。 如果- allowed_function_names為空,可以傳回任何提供的函式。
REST
PROJECT_ID=myproject
LOCATION=us-central1
MODEL_ID=gemini-2.5-flash
curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \
  -d '{
    "contents": [{
      "role": "user",
      "parts": [{
        "text": "Do you have the White Pixel 8 Pro 128GB in stock in the US?"
      }]
    }],
    "tools": [{
      "functionDeclarations": [
        {
          "name": "get_product_sku",
          "description": "Get the available inventory for a Google products, e.g: Pixel phones, Pixel Watches, Google Home etc",
          "parameters": {
            "type": "object",
            "properties": {
              "product_name": {"type": "string", "description": "Product name"}
            }
          }
        },
        {
          "name": "get_store_location",
          "description": "Get the location of the closest store",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {"type": "string", "description": "Location"}
            },
          }
        }
      ]
    }],
    "toolConfig": {
        "functionCallingConfig": {
            "mode":"ANY",
            "allowedFunctionNames": ["get_product_sku"]
      }
    },
    "generationConfig": {
      "temperature": 0.95,
      "topP": 1.0,
      "maxOutputTokens": 8192
    }
  }'
Python 適用的 Gen AI SDK
Node.js
Go
REST (OpenAI)
您可以使用 OpenAI 程式庫呼叫 Function Calling API。詳情請參閱 使用 OpenAI 程式庫呼叫 Vertex AI 模型。
使用任何要求資料之前,請先替換以下項目:
- PROJECT_ID:。
- MODEL_ID:正在處理的模型 ID。
HTTP 方法和網址:
POST https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions
JSON 要求主體:
{
  "model": "google/MODEL_ID",
  "messages": [
  {
    "role": "user",
    "content": "What is the weather in Boston?"
  }
],
"tools": [
  {
    "type": "function",
    "function": {
      "name": "get_current_weather",
      "description": "Get the current weather in a given location",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          }
         },
        "required": ["location"]
      }
    }
  }
],
"tool_choice": "auto"
}
如要傳送要求,請選擇以下其中一個選項:
curl
      將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions"
PowerShell
      將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions" | Select-Object -Expand Content
Python (OpenAI)
您可以使用 OpenAI 程式庫呼叫 Function Calling API。詳情請參閱「 使用 OpenAI 程式庫呼叫 Vertex AI 模型」。
後續步驟
如需詳細說明文件,請參閱下列內容: