使用 Gemma 等生成式人工智慧 (AI) 模型時,您可能會想使用模型操作程式介面,以便完成工作或回答問題。透過定義程式設計介面,然後提出使用該介面的要求,即可指示模型,這稱為函式呼叫。
Gemma 不會輸出工具專屬的符記。您的架構必須檢查輸出內容的結構是否符合提示函式輸出規格,藉此偵測工具呼叫。
您可以為多個應用程式使用函式呼叫:
- 為程式設計 API 建立自然語言介面,讓非程式設計人員不必編寫程式碼,也能操作程式介面。
- 在 AI 代理程工作流程中產生程式設計呼叫
Gemma 3 支援函式呼叫,但函式呼叫技巧可與先前版本的 Gemma 搭配使用。本指南將說明如何建構使用函式呼叫的 Gemma 提示。如要獲得最佳效能,建議使用 Gemma3 27B;如要兼顧效能和延遲,建議使用 Gemma3 12B。
呼叫程式設計函式
您可以透過建立提示來使用 Gemma 的函式呼叫功能,該提示會提供指示,指定輸出格式並定義可用的函式。
加入使用者提示後,模型會輸出函式呼叫,這是與您指定的輸出格式相符的字串。這會通知模型架構解析要求,以便呼叫定義的函式。
以下提示範例會顯示函式定義區塊,以及函式呼叫語法和模型的函式呼叫輸出內容。以下提示範例可搭配產品目錄的程式設計介面使用:
You have access to functions. If you decide to invoke any of the function(s),
you MUST put it in the format of
[func_name1(params_name1=params_value1, params_name2=params_value2...), func_name2(params)]
You SHOULD NOT include any other text in the response if you call a function
[
{
"name": "get_product_name_by_PID",
"description": "Finds the name of a product by its Product ID",
"parameters": {
"type": "object",
"properties": {
"PID": {
"type": "string"
}
},
"required": [
"PID"
]
}
}
]
While browsing the product catalog, I came across a product that piqued my
interest. The product ID is 807ZPKBL9V. Can you help me find the name of this
product?
這個提示應會產生下列回應:
[get_product_name_by_PID(PID="807ZPKBL9V")]
本範例使用 Python 樣式函式呼叫輸出內容。或者,您也可以指定 JSON 樣式輸出格式,如以下範例所示:
You have access to functions. If you decide to invoke any of the function(s),
you MUST put it in the format of
{"name": function name, "parameters": dictionary of argument name and its value}
You SHOULD NOT include any other text in the response if you call a function
[
{
"name": "get_product_name_by_PID",
"description": "Finds the name of a product by its Product ID",
"parameters": {
"type": "object",
"properties": {
"PID": {
"type": "string"
}
},
"required": [
"PID"
]
}
}
]
While browsing the product catalog, I came across a product that piqued my
interest. The product ID is 807ZPKBL9V. Can you help me find the name of this
product?
這個提示應會產生下列回應:
{"name": "get_product_name_by_PID", "parameters": {"PID": "807ZPKBL9V"}}
函式呼叫提示的元件
使用 Gemma 模型的函式呼叫時,模型提示應遵循以下特定順序和結構:
以下各節將詳細說明每個提示元件。
函式呼叫設定
函式呼叫提示的「設定」部分會設定模型的整體預期行為。您可以在本節中為模型行為新增其他一般指示,例如指定應使用 print 或 console.log 函式顯示輸出內容。使用 Markdown 風格的單引號 (func_name) 表示程式碼語法。
You have access to functions. If you decide to invoke any of the function(s),
you MUST put it in the format of
{"name": function name, "parameters": dictionary of argument name and its value}
You SHOULD NOT include any other text in the response if you call a function
這些操作說明應盡可能簡明扼要。請優先提供最重要的操作說明,並謹慎提供許多一般操作說明。Gemma 模型可能會忽略過於詳細或表達不清晰的指示,尤其是當您使用參數數量較少的模型版本時。
函式定義
提示的「定義」部分會提供函式名稱、參數和輸出內容,並附上各項內容的說明。您可以使用顯示的格式宣告函式。您可以在函式宣告區塊中定義單一或多個函式。
[
{
"name": "get_product_name_by_PID",
"description": "Finds the name of a product by its Product ID",
"parameters": {
"type": "object",
"properties": {
"PID": {
"type": "string"
}
},
"required": [
"PID"
]
}
},
{
"name": "get_product_price_by_PID",
"description": "Finds the price of a product by its Product ID",
"parameters": {
"type": "object",
"properties": {
"PID": {
"type": "string"
}
},
"required": [
"PID"
]
}
}
]
後續步驟
請參閱以下文章,瞭解如何部署及執行 Gemma 模型: