您可以使用 Gemini API 函式呼叫功能,為模型提供自訂函式定義。模型不會直接叫用這些函式,而是產生結構化輸出內容,指定函式名稱和建議的引數。接著,您可以使用函式名稱和引數呼叫外部 API,並將 API 輸出結果納入對模型的進一步查詢,讓模型提供更全面的回應並採取其他動作。
有了函式呼叫功能,使用者就能與即時資訊和服務互動,例如資料庫、客戶關係管理系統和文件存放區。這項功能還可強化模型提供相關和情境答案的能力。函式呼叫最適合用於與外部系統互動。如果用途需要模型執行運算,但不涉及外部系統或 API,建議改用程式碼執行。
如需函式呼叫的實際範例,請參閱「light bot」notebook。
函式呼叫的運作方式
如要使用函式呼叫功能,請在模型提示中加入描述程式介面的結構化查詢資料,稱為「函式宣告」。函式宣告會提供 API 函式的名稱、說明其用途、支援的任何參數,以及這些參數的說明。在查詢中將函式宣告清單傳遞至模型後,模型會分析函式宣告和查詢的其餘部分,以決定如何使用已宣告的 API 回應要求。
接著,模型會在 OpenAPI 相容的結構定義中傳回物件,指定如何呼叫一或多個已宣告的函式,以便回覆使用者的提問。接著,您可以採用建議的函式呼叫參數、呼叫實際的 API、取得回應,然後將該回應提供給使用者或採取進一步行動。請注意,模型實際上不會呼叫已宣告的函式。請改用傳回的結構定義物件參數來呼叫函式。Gemini API 也支援並行函式呼叫,模型會根據單一要求建議多個 API 函式呼叫。
函式宣告
在提示中實作函式呼叫時,您會建立 tools
物件,其中包含一或多個 function declarations
。您可以使用 JSON 定義函式,具體來說,就是使用 OpenAPI 結構定義格式的選取子集。單一函式宣告可包含下列參數:
name
(字串):API 呼叫中函式的專屬 ID。description
(字串):說明函式的用途和功能。parameters
(物件):定義函式所需的輸入資料。type
(字串):指定整體資料類型,例如object
。properties
(物件):列出個別參數,每個參數包含:type
(字串):參數的資料類型,例如string
、integer
、boolean
。description
(字串):清楚說明參數的用途和預期格式。
required
(陣列):字串陣列,列出函式運作時必須使用的參數名稱。
如需使用 cURL 指令宣告函式的程式碼範例,請參閱函式呼叫範例。如需使用 Gemini API SDK 建立函式宣告的範例,請參閱函式呼叫教學課程。
函式宣告的最佳做法
將函式整合至要求時,必須準確定義函式。每個函式都會依賴特定參數,以引導其行為和與模型的互動。以下清單提供在 functions_declarations
陣列中定義個別函式參數的指引。
name
:請使用清楚且具描述性的名稱,且不得包含空格、句號 (.
) 或破折號 (-
) 字元。請改用底線 (_
) 字元或駝峰式命名法。description
:提供詳細、明確且具體的函式說明,並視需要提供範例。例如,使用find theaters based on location and optionally movie title that is currently playing in theaters.
而非find theaters
。請避免使用過於廣泛或模糊的說明。properties
>type
:使用強型別參數,減少模型錯覺。舉例來說,如果參數值來自有限集合,請使用enum
欄位,而非在說明中列出值 (例如"type": "enum", "values": ["now_playing", "upcoming"]
)。如果參數值一律是整數,請將類型設為integer
,而非number
。properties
>description
:提供具體範例和限制。例如,使用The city and state, e.g. San Francisco, CA or a zip code e.g. 95616
,而不使用the location to search
。
如要進一步瞭解使用函式呼叫的最佳做法,請參閱「最佳做法」一節。
函式呼叫模式
您可以使用呼叫 mode
參數的函式,修改功能的執行行為。共有三種模式可供選擇:
AUTO
:預設模型行為。模型會決定要預測函式呼叫還是自然語言回應。ANY
:模型會受到限制,一律預測函式呼叫。如果未提供allowed_function_names
,模型會從所有可用的函式宣告中挑選。如果提供allowed_function_names
,模型會從允許的函式組合中挑選。NONE
:模型不會預測函式呼叫。在這種情況下,模型行為與您未傳遞任何函式宣告的情況相同。
只有 Gemini 1.5 Pro
和 Gemini 1.5 Flash
機型支援 ANY
模式 (「強制呼叫函式」)。
您也可以傳遞一組 allowed_function_names
,在提供時,會限制模型要呼叫的函式。只有在模式為 ANY
時,才應納入 allowed_function_names
。函式名稱應與函式宣告名稱相符。將模式設為 ANY
並設定 allowed_function_names
後,模型會從提供的函式名稱集合中預測函式呼叫。
以下是示例要求的程式碼片段,說明如何將 mode
設為 ANY
,並指定允許的函式清單:
"tool_config": {
"function_calling_config": {
"mode": "ANY",
"allowed_function_names": ["find_theaters", "get_showtimes"]
},
}
函式呼叫範例
本節提供使用 cURL 指令呼叫函式的提示範例。範例包括單一回合和多回合情境,以及啟用不同的函式呼叫模式。
搭配這項功能使用 cURL 指令時,函式和參數資訊會包含在 tools
元素中。tools
元素中的每個函式宣告都包含函式名稱,您可以使用 OpenAPI 相容的結構定義和函式說明來指定參數。
單輪示例
單輪是指呼叫語言模型一次。在函式呼叫方面,單輪用途的例子可能是您向模型提供自然語言查詢和函式清單。在這種情況下,模型會使用函式宣告 (包含函式名稱、參數和說明) 來預測要呼叫的函式,以及呼叫時要使用的引數。
以下 curl 範例是傳入函式說明的例子,該函式會傳回電影播放位置的相關資訊。這項要求包含多個函式宣告,例如 find_movies
和 find_theaters
。
單回合函式呼叫要求範例
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": { "role": "user", "parts": { "text": "Which theaters in Mountain View show Barbie movie?" } }, "tools": [ { "function_declarations": [ { "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "string", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": [ "description" ] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" } }, "required": [ "location" ] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" }, "theater": { "type": "string", "description": "Name of the theater" }, "date": { "type": "string", "description": "Date for requested showtime" } }, "required": [ "location", "movie", "theater", "date" ] } } ] } ] }'
這個 curl 範例的回應可能會類似以下內容。
單輪函式呼叫 curl 回應範例
[{ "candidates": [ { "content": { "parts": [ { "functionCall": { "name": "find_theaters", "args": { "movie": "Barbie", "location": "Mountain View, CA" } } } ] }, "finishReason": "STOP", "safetyRatings": [ { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } ], "usageMetadata": { "promptTokenCount": 9, "totalTokenCount": 9 } }]
使用 ANY 模式的單回合範例
以下 curl 範例與單轉範例類似,但會將模式設為 ANY
:
"tool_config": {
"function_calling_config": {
"mode": "ANY"
},
}
使用 ANY 模式的單回合函式呼叫 (要求)
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": { "role": "user", "parts": { "text": "What movies are showing in North Seattle tonight?" } }, "tools": [ { "function_declarations": [ { "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "string", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": [ "description" ] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" } }, "required": [ "location" ] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" }, "theater": { "type": "string", "description": "Name of the theater" }, "date": { "type": "string", "description": "Date for requested showtime" } }, "required": [ "location", "movie", "theater", "date" ] } } ] } ], "tool_config": { "function_calling_config": { "mode": "ANY" }, } }'
回應可能會類似以下內容:
使用 ANY 模式的單回合函式呼叫 (回應)
{ "candidates": [ { "content": { "parts": [ { "functionCall": { "name": "find_movies", "args": { "description": "", "location": "North Seattle, WA" } } } ], "role": "model" }, "finishReason": "STOP", "index": 0, "safetyRatings": [ { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" } ] } ], "promptFeedback": { "safetyRatings": [ { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } }
使用任意模式和允許函式的單回合範例
以下 curl 範例與單轉範例類似,但會將模式設為 ANY
,並包含允許的函式清單:
"tool_config": {
"function_calling_config": {
"mode": "ANY",
"allowed_function_names": ["find_theaters", "get_showtimes"]
},
}
使用任意模式和允許的函式進行單回合函式呼叫 (要求)
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": { "role": "user", "parts": { "text": "What movies are showing in North Seattle tonight?" } }, "tools": [ { "function_declarations": [ { "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "string", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": [ "description" ] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" } }, "required": [ "location" ] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" }, "theater": { "type": "string", "description": "Name of the theater" }, "date": { "type": "string", "description": "Date for requested showtime" } }, "required": [ "location", "movie", "theater", "date" ] } } ] } ], "tool_config": { "function_calling_config": { "mode": "ANY", "allowed_function_names": ["find_theaters", "get_showtimes"] }, } }'
由於 find_movies
函式不在允許函式清單中,因此模型無法預測該函式,而是改為預測其他函式。回應可能會類似以下內容:
使用任意模式和允許的函式進行單回合函式呼叫 (回應)
{ "candidates": [ { "content": { "parts": [ { "functionCall": { "name": "find_theaters", "args": { "location": "North Seattle, WA", "movie": null } } } ], "role": "model" }, "finishReason": "STOP", "index": 0, "safetyRatings": [ { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } ], "promptFeedback": { "safetyRatings": [ { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } }
多回合對話範例
如要實作多回合函式呼叫情境,請按照下列步驟操作:
- 呼叫語言模型,取得函式呼叫回應。這是第一個回合。
- 使用第一輪的函式呼叫回應,以及呼叫該函式所取得的函式回應,呼叫語言模型。這是第二輪。
第二輪的回應會總結結果,以便在第一輪回答您的查詢,或是包含第二個可用來取得查詢更多資訊的函式呼叫。
本主題包含兩個多回 curl 範例:
使用先前回合中的回應
以下 curl 範例會呼叫先前單回合範例傳回的函式和引數,以便取得回應。單次回合範例所傳回的方法和參數會顯示在這個 JSON 中。
"functionCall": {
"name": "find_theaters",
"args": {
"movie": "Barbie",
"location": "Mountain View, CA"
}
}
多回合函式呼叫 curl 的範例要求
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": [{ "role": "user", "parts": [{ "text": "Which theaters in Mountain View show Barbie movie?" }] }, { "role": "model", "parts": [{ "functionCall": { "name": "find_theaters", "args": { "location": "Mountain View, CA", "movie": "Barbie" } } }] }, { "role": "user", "parts": [{ "functionResponse": { "name": "find_theaters", "response": { "name": "find_theaters", "content": { "movie": "Barbie", "theaters": [{ "name": "AMC Mountain View 16", "address": "2000 W El Camino Real, Mountain View, CA 94040" }, { "name": "Regal Edwards 14", "address": "245 Castro St, Mountain View, CA 94040" }] } } } }] }], "tools": [{ "functionDeclarations": [{ "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "STRING", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": ["description"] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" } }, "required": ["location"] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" }, "theater": { "type": "STRING", "description": "Name of the theater" }, "date": { "type": "STRING", "description": "Date for requested showtime" } }, "required": ["location", "movie", "theater", "date"] } }] }] }'
這個 curl 範例的回應包含呼叫 find_theaters
方法的結果。回應可能會類似以下內容:
多回合函式呼叫 curl 回應範例
{ "candidates": [ { "content": { "parts": [ { "text": " OK. Barbie is showing in two theaters in Mountain View, CA: AMC Mountain View 16 and Regal Edwards 14." } ] } } ], "usageMetadata": { "promptTokenCount": 9, "candidatesTokenCount": 27, "totalTokenCount": 36 } }
多次呼叫模型
以下 cURL 範例會多次呼叫生成式 AI 模型來呼叫函式。模型每次呼叫函式時,可以使用不同的函式來回答要求中的不同使用者查詢。
多回合函式呼叫 curl 的範例要求
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": [{ "role": "user", "parts": [{ "text": "Which theaters in Mountain View show Barbie movie?" }] }, { "role": "model", "parts": [{ "functionCall": { "name": "find_theaters", "args": { "location": "Mountain View, CA", "movie": "Barbie" } } }] }, { "role": "user", "parts": [{ "functionResponse": { "name": "find_theaters", "response": { "name": "find_theaters", "content": { "movie": "Barbie", "theaters": [{ "name": "AMC Mountain View 16", "address": "2000 W El Camino Real, Mountain View, CA 94040" }, { "name": "Regal Edwards 14", "address": "245 Castro St, Mountain View, CA 94040" }] } } } }] }, { "role": "model", "parts": [{ "text": " OK. Barbie is showing in two theaters in Mountain View, CA: AMC Mountain View 16 and Regal Edwards 14." }] },{ "role": "user", "parts": [{ "text": "Can we recommend some comedy movies on show in Mountain View?" }] }], "tools": [{ "functionDeclarations": [{ "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "STRING", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": ["description"] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" } }, "required": ["location"] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" }, "theater": { "type": "STRING", "description": "Name of the theater" }, "date": { "type": "STRING", "description": "Date for requested showtime" } }, "required": ["location", "movie", "theater", "date"] } }] }] }'
多回合函式呼叫 curl 回應範例
[{ "candidates": [ { "content": { "parts": [ { "functionCall": { "name": "find_movies", "args": { "description": "comedy", "location": "Mountain View, CA" } } } ] }, "finishReason": "STOP", "safetyRatings": [ { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } ], "usageMetadata": { "promptTokenCount": 48, "totalTokenCount": 48 } } ]
最佳做法
請遵循下列最佳做法,提升函式呼叫的精確度和可靠性。
使用者提示
為獲得最佳結果,請在使用者查詢前加上下列詳細資料:
- 模型的其他背景資訊。例如:
You are a movie API assistant to help users find movies and showtimes based on their preferences.
- 說明如何使用函式,以及使用時機。例如:
Don't make assumptions on showtimes. Always use a future date for showtimes.
- 使用者查詢內容不清晰時,請問清楚的說明。例如:
Ask clarifying questions if not enough information is available to complete the request.
取樣參數
請使用 0
或其他偏低的值做為溫度參數。這麼做可指示模型產生更有信心的結果,並減少幻覺。
API 叫用
如果模型建議叫用會傳送訂單、更新資料庫或造成重大後果的函式,請先向使用者驗證函式呼叫,再執行該函式。