函式呼叫可讓您將模型連結至外部工具和 API。模型不會產生文字回應,而是會瞭解何時呼叫特定函式,並提供執行實際動作所需的參數。這可讓模型在自然語言與實際動作和資料之間搭起橋樑。函式呼叫有 3 個主要用途:
- 擴充知識:存取資料庫、API 和知識庫等外部來源的資訊。
- 擴充功能:使用外部工具執行運算,並擴充模型的限制,例如使用計算機或建立圖表。
- 執行動作:使用 API 與外部系統互動,例如安排預約、建立帳單、傳送電子郵件或控制智慧住宅裝置
函式呼叫的運作方式
函式呼叫涉及應用程式、模型和外部函式之間的結構化互動。以下是這項程序的詳細說明:
- 定義函式宣告:在應用程式程式碼中定義函式宣告。函式宣告會說明函式的名稱、參數和用途。
- 使用函式宣告呼叫 LLM:將使用者提示連同函式宣告傳送至模型。它會分析要求,並判斷是否有助於函式呼叫。如果是,則會以結構化 JSON 物件回應。
- 執行函式程式碼 (您的責任):模型不會執行函式本身。如果有回應,應用程式必須負責處理回應並檢查是否有函式呼叫。
- 是:擷取函式的名稱和引數,並在應用程式中執行對應的函式。
- 否:模型已針對提示提供直接的文字回應 (這個流程在示例中較不強調,但仍是可能的結果)。
- 建立使用者友善回應:如果執行函式,請擷取結果,並在後續對話輪次中將結果傳回模型。系統會使用結果產生最終的使用者友善回應,其中會納入函式呼叫的資訊。
這個程序可在多個回合中重複執行,以便進行複雜的互動和工作流程。模型也支援在單一回合 (平行函式呼叫) 和依序 (組合函式呼叫) 中呼叫多個函式。
步驟 1:定義函式宣告
在應用程式程式碼中定義函式及其宣告,讓使用者設定燈光值並提出 API 要求。這個函式可以呼叫外部服務或 API。
from google.genai import types
# Define a function that the model can call to control smart lights
set_light_values_declaration = {
"name": "set_light_values",
"description": "Sets the brightness and color temperature of a light.",
"parameters": {
"type": "object",
"properties": {
"brightness": {
"type": "integer",
"description": "Light level from 0 to 100. Zero is off and 100 is full brightness",
},
"color_temp": {
"type": "string",
"enum": ["daylight", "cool", "warm"],
"description": "Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.",
},
},
"required": ["brightness", "color_temp"],
},
}
# This is the actual function that would be called based on the model's suggestion
def set_light_values(brightness: int, color_temp: str) -> dict[str, int | str]:
"""Set the brightness and color temperature of a room light. (mock API).
Args:
brightness: Light level from 0 to 100. Zero is off and 100 is full brightness
color_temp: Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.
Returns:
A dictionary containing the set brightness and color temperature.
"""
return {"brightness": brightness, "colorTemperature": color_temp}
import { Type } from '@google/genai';
// Define a function that the model can call to control smart lights
const setLightValuesFunctionDeclaration = {
name: 'set_light_values',
description: 'Sets the brightness and color temperature of a light.',
parameters: {
type: Type.OBJECT,
properties: {
brightness: {
type: Type.NUMBER,
description: 'Light level from 0 to 100. Zero is off and 100 is full brightness',
},
color_temp: {
type: Type.STRING,
enum: ['daylight', 'cool', 'warm'],
description: 'Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.',
},
},
required: ['brightness', 'color_temp'],
},
};
/**
* Set the brightness and color temperature of a room light. (mock API)
* @param {number} brightness - Light level from 0 to 100. Zero is off and 100 is full brightness
* @param {string} color_temp - Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.
* @return {Object} A dictionary containing the set brightness and color temperature.
*/
function setLightValues(brightness, color_temp) {
return {
brightness: brightness,
colorTemperature: color_temp
};
}
步驟 2:使用函式宣告呼叫模型
定義函式宣告後,您可以提示模型使用該函式。它會分析提示和函式宣告,並決定要直接回應或呼叫函式。如果呼叫函式,回應物件會包含函式呼叫建議。
from google import genai
# Generation Config with Function Declaration
tools = types.Tool(function_declarations=[set_light_values_declaration])
config = types.GenerateContentConfig(tools=[tools])
# Configure the client
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
# Define user prompt
contents = [
types.Content(
role="user", parts=[types.Part(text="Turn the lights down to a romantic level")]
)
]
# Send request with function declarations
response = client.models.generate_content(
model="gemini-2.0-flash", config=config, contents=contents
)
print(response.candidates[0].content.parts[0].function_call)
import { GoogleGenAI } from '@google/genai';
// Generation Config with Function Declaration
const config = {
tools: [{
functionDeclarations: [setLightValuesFunctionDeclaration]
}]
};
// Configure the client
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
// Define user prompt
const contents = [
{
role: 'user',
parts: [{ text: 'Turn the lights down to a romantic level' }]
}
];
// Send request with function declarations
const response = await ai.models.generateContent({
model: 'gemini-2.0-flash',
contents: contents,
config: config
});
console.log(response.functionCalls[0]);
接著,模型會在 OpenAPI 相容的結構定義中傳回 functionCall
物件,指定如何呼叫一或多個已宣告的函式,以回應使用者的問題。
id=None args={'color_temp': 'warm', 'brightness': 25} name='set_light_values'
{
name: 'set_light_values',
args: { brightness: 25, color_temp: 'warm' }
}
步驟 3:執行 set_light_values 函式程式碼
從模型回應中擷取函式呼叫詳細資料、剖析引數,然後在程式碼中執行 set_light_values
函式。
# Extract tool call details
tool_call = response.candidates[0].content.parts[0].function_call
if tool_call.name == "set_light_values":
result = set_light_values(**tool_call.args)
print(f"Function execution result: {result}")
// Extract tool call details
const tool_call = response.functionCalls[0]
let result;
if (tool_call.name === 'set_light_values') {
result = setLightValues(tool_call.args.brightness, tool_call.args.color_temp);
console.log(`Function execution result: ${JSON.stringify(result)}`);
}
步驟 4:使用函式結果建立使用者友善回應,並再次呼叫模型
最後,將函式執行結果傳回模型,以便將這項資訊納入最終回應,傳送給使用者。
# Create a function response part
function_response_part = types.Part.from_function_response(
name=tool_call.name,
response={"result": result},
)
# Append function call and result of the function execution to contents
contents.append(types.Content(role="model", parts=[types.Part(function_call=tool_call)])) # Append the model's function call message
contents.append(types.Content(role="user", parts=[function_response_part])) # Append the function response
final_response = client.models.generate_content(
model="gemini-2.0-flash",
config=config,
contents=contents,
)
print(final_response.text)
// Create a function response part
const function_response_part = {
name: tool_call.name,
response: { result }
}
// Append function call and result of the function execution to contents
contents.push({ role: 'model', parts: [{ functionCall: tool_call }] });
contents.push({ role: 'user', parts: [{ functionResponse: function_response_part }] });
// Get the final response from the model
const final_response = await ai.models.generateContent({
model: 'gemini-2.0-flash',
contents: contents,
config: config
});
console.log(final_response.text);
這樣就完成函式呼叫流程。模型成功使用 set_light_values
函式執行使用者的要求動作。
函式宣告
在提示中實作函式呼叫時,您會建立 tools
物件,其中包含一或多個 function declarations
。您可以使用 JSON 定義函式,具體來說,就是使用 OpenAPI 結構定義格式的選取子集。單一函式宣告可包含下列參數:
name
(字串):函式的不重複名稱 (get_weather_forecast
、send_email
)。請使用描述性名稱,且不得包含空格或特殊字元 (請使用底線或駝峰式大小寫)。description
(字串):清楚且詳細說明功能的用途和功能。這對於模型瞭解何時使用函式至關重要。請具體說明,並提供相關範例 (例如:「根據位置和目前正在電影院上映的電影標題,找出電影院」。)parameters
(物件):定義函式預期的輸入參數。type
(字串):指定整體資料類型,例如object
。properties
(物件):列出個別參數,每個參數包含:type
(字串):參數的資料類型,例如string
、integer
、boolean, array
。description
(字串):說明參數的用途和格式。提供示例和限制條件 (「城市和州,例如'San Francisco, CA' 或郵遞區號 (例如:'95616'。")。enum
(陣列,選用):如果參數值來自固定集合,請使用「enum」列出允許的值,而非只在說明中描述這些值。這有助於提升準確度 ("enum": ["daylight", "cool", "warm"]).
required
(陣列):字串陣列,列出函式運作時必須使用的參數名稱。
並行函式呼叫
除了單一回合函式呼叫之外,您也可以一次呼叫多個函式。並行函式呼叫可讓您一次執行多個函式,並在函式不相互依賴時使用。這在從多個獨立來源收集資料的情況下非常實用,例如從不同資料庫擷取顧客詳細資料、檢查各倉庫的庫存水準,或執行多項動作,例如將公寓轉換為迪斯科舞廳。
power_disco_ball = {
"name": "power_disco_ball",
"description": "Powers the spinning disco ball.",
"parameters": {
"type": "object",
"properties": {
"power": {
"type": "boolean",
"description": "Whether to turn the disco ball on or off.",
}
},
"required": ["power"],
},
}
start_music = {
"name": "start_music",
"description": "Play some music matching the specified parameters.",
"parameters": {
"type": "object",
"properties": {
"energetic": {
"type": "boolean",
"description": "Whether the music is energetic or not.",
},
"loud": {
"type": "boolean",
"description": "Whether the music is loud or not.",
},
},
"required": ["energetic", "loud"],
},
}
dim_lights = {
"name": "dim_lights",
"description": "Dim the lights.",
"parameters": {
"type": "object",
"properties": {
"brightness": {
"type": "number",
"description": "The brightness of the lights, 0.0 is off, 1.0 is full.",
}
},
"required": ["brightness"],
},
}
import { Type } from '@google/genai';
const powerDiscoBall = {
name: 'power_disco_ball',
description: 'Powers the spinning disco ball.',
parameters: {
type: Type.OBJECT,
properties: {
power: {
type: Type.BOOLEAN,
description: 'Whether to turn the disco ball on or off.'
}
},
required: ['power']
}
};
const startMusic = {
name: 'start_music',
description: 'Play some music matching the specified parameters.',
parameters: {
type: Type.OBJECT,
properties: {
energetic: {
type: Type.BOOLEAN,
description: 'Whether the music is energetic or not.'
},
loud: {
type: Type.BOOLEAN,
description: 'Whether the music is loud or not.'
}
},
required: ['energetic', 'loud']
}
};
const dimLights = {
name: 'dim_lights',
description: 'Dim the lights.',
parameters: {
type: Type.OBJECT,
properties: {
brightness: {
type: Type.NUMBER,
description: 'The brightness of the lights, 0.0 is off, 1.0 is full.'
}
},
required: ['brightness']
}
};
使用可使用所有指定工具的指令呼叫模型。本範例使用 tool_config
。如要進一步瞭解,請參閱設定函式呼叫功能。
from google import genai
from google.genai import types
# Set up function declarations
house_tools = [
types.Tool(function_declarations=[power_disco_ball, start_music, dim_lights])
]
config = {
"tools": house_tools,
"automatic_function_calling": {"disable": True},
# Force the model to call 'any' function, instead of chatting.
"tool_config": {"function_calling_config": {"mode": "any"}},
}
# Configure the client
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
chat = client.chats.create(model="gemini-2.0-flash", config=config)
response = chat.send_message("Turn this place into a party!")
# Print out each of the function calls requested from this single call
print("Example 1: Forced function calling")
for fn in response.function_calls:
args = ", ".join(f"{key}={val}" for key, val in fn.args.items())
print(f"{fn.name}({args})")
import { GoogleGenAI } from '@google/genai';
// Set up function declarations
const houseFns = [powerDiscoBall, startMusic, dimLights];
const config = {
tools: [{
functionDeclarations: houseFns
}],
// Force the model to call 'any' function, instead of chatting.
toolConfig: {
functionCallingConfig: {
mode: 'any'
}
}
};
// Configure the client
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
// Create a chat session
const chat = ai.chats.create({
model: 'gemini-2.0-flash',
config: config
});
const response = await chat.sendMessage({message: 'Turn this place into a party!'});
// Print out each of the function calls requested from this single call
console.log("Example 1: Forced function calling");
for (const fn of response.functionCalls) {
const args = Object.entries(fn.args)
.map(([key, val]) => `${key}=${val}`)
.join(', ');
console.log(`${fn.name}(${args})`);
}
每個顯示的結果都代表模型要求的單一函式呼叫。如要傳回結果,請按照要求的順序加入回應。
Python SDK 支援「自動函式呼叫」功能,可將 Python 函式轉換為宣告,並為您處理函式呼叫執行作業和回應週期。以下是我們的 Disco 用途範例。
from google import genai
from google.genai import types
# Actual implementation functions
def power_disco_ball_impl(power: bool) -> dict:
"""Powers the spinning disco ball.
Args:
power: Whether to turn the disco ball on or off.
Returns:
A status dictionary indicating the current state.
"""
return {"status": f"Disco ball powered {'on' if power else 'off'}"}
def start_music_impl(energetic: bool, loud: bool) -> dict:
"""Play some music matching the specified parameters.
Args:
energetic: Whether the music is energetic or not.
loud: Whether the music is loud or not.
Returns:
A dictionary containing the music settings.
"""
music_type = "energetic" if energetic else "chill"
volume = "loud" if loud else "quiet"
return {"music_type": music_type, "volume": volume}
def dim_lights_impl(brightness: float) -> dict:
"""Dim the lights.
Args:
brightness: The brightness of the lights, 0.0 is off, 1.0 is full.
Returns:
A dictionary containing the new brightness setting.
"""
return {"brightness": brightness}
config = {
"tools": [power_disco_ball_impl, start_music_impl, dim_lights_impl],
}
chat = client.chats.create(model="gemini-2.0-flash", config=config)
response = chat.send_message("Do everything you need to this place into party!")
print("\nExample 2: Automatic function calling")
print(response.text)
# I've turned on the disco ball, started playing loud and energetic music, and dimmed the lights to 50% brightness. Let's get this party started!
組合函式呼叫
Gemini 2.0 支援組合函式呼叫,也就是說模型可以將多個函式呼叫鏈結在一起。舉例來說,如果要回答「取得我目前所在位置的溫度」,Gemini API 可能會同時叫用 get_current_location()
函式和 get_weather()
函式,這兩個函式都會將位置做為參數。
# Light control schemas
turn_on_the_lights_schema = {'name': 'turn_on_the_lights'}
turn_off_the_lights_schema = {'name': 'turn_off_the_lights'}
prompt = """
Hey, can you write run some python code to turn on the lights, wait 10s and then turn off the lights?
"""
tools = [
{'code_execution': {}},
{'function_declarations': [turn_on_the_lights_schema, turn_off_the_lights_schema]}
]
await run(prompt, tools=tools, modality="AUDIO")
// Light control schemas
const turnOnTheLightsSchema = { name: 'turn_on_the_lights' };
const turnOffTheLightsSchema = { name: 'turn_off_the_lights' };
const prompt = `
Hey, can you write run some python code to turn on the lights, wait 10s and then turn off the lights?
`;
const tools = [
{ codeExecution: {} },
{ functionDeclarations: [turnOnTheLightsSchema, turnOffTheLightsSchema] }
];
await run(prompt, tools=tools, modality="AUDIO")
函式呼叫模式
您可以透過 Gemini API 控制模型如何使用提供的工具 (函式宣告)。具體來說,您可以在 function_calling_config
中設定模式。
AUTO (Default)
:模型會根據提示和內容決定是否要產生自然語言回應,或建議呼叫函式。這是最具彈性的模式,建議在大多數情況下使用。ANY
:模型會受到限制,一律預測函式呼叫,並保證函式架構符合規定。如果未指定allowed_function_names
,模型可以從提供的任何函式宣告中選擇。如果allowed_function_names
是以清單形式提供,模型只能從該清單中的函式中選擇。如要針對每個提示 (如適用) 呼叫函式,請使用這個模式。NONE
:模型「禁止」呼叫函式。這等同於傳送不含任何函式宣告的要求。可用於暫時停用函式呼叫,而不必移除工具定義。
from google.genai import types
# Configure function calling mode
tool_config = types.ToolConfig(
function_calling_config=types.FunctionCallingConfig(
mode="ANY", allowed_function_names=["get_current_temperature"]
)
)
# Create the generation config
config = types.GenerateContentConfig(
temperature=0,
tools=[tools], # not defined here.
tool_config=tool_config,
)
import { FunctionCallingConfigMode } from '@google/genai';
// Configure function calling mode
const toolConfig = {
functionCallingConfig: {
mode: FunctionCallingConfigMode.ANY,
allowedFunctionNames: ['get_current_temperature']
}
};
// Create the generation config
const config = {
temperature: 0,
tools: tools, // not defined here.
toolConfig: toolConfig,
};
自動函式呼叫 (僅限 Python)
使用 Python SDK 時,您可以直接將 Python 函式做為工具提供。SDK 會自動將 Python 函式轉換為宣告,並為您處理函式呼叫執行作業和回應週期。接著,Python SDK 會自動執行以下操作:
- 偵測模型的函式呼叫回應。
- 在程式碼中呼叫對應的 Python 函式。
- 將函式回應傳回模型。
- 傳回模型的最終文字回覆。
如要使用此功能,請使用型別提示和 docstring 定義函式,然後將函式本身 (而非 JSON 宣告) 做為工具傳遞:
from google import genai
from google.genai import types
# Define the function with type hints and docstring
def get_current_temperature(location: str) -> dict:
"""Gets the current temperature for a given location.
Args:
location: The city and state, e.g. San Francisco, CA
Returns:
A dictionary containing the temperature and unit.
"""
# ... (implementation) ...
return {"temperature": 25, "unit": "Celsius"}
# Configure the client and model
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY")) # Replace with your actual API key setup
config = types.GenerateContentConfig(
tools=[get_current_temperature]
) # Pass the function itself
# Make the request
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="What's the temperature in London?",
config=config,
)
print(response.text) # The SDK handles the function call and returns the final text
您可以使用下列方式停用自動函式呼叫:
# To disable automatic function calling:
config = types.GenerateContentConfig(
tools=[get_current_temperature],
automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=True)
)
自動函式結構定義宣告
從 Python 函式自動擷取結構定義並非在所有情況下都有效。舉例來說,它無法處理您描述巢狀字典物件的欄位時的情況。API 可描述下列任一類型:
AllowedType = (int | float | bool | str | list['AllowedType'] | dict[str, AllowedType])
如要查看推斷的結構定義,您可以使用 from_callable
進行轉換:
def multiply(a: float, b: float):
"""Returns a * b."""
return a * b
fn_decl = types.FunctionDeclaration.from_callable(callable=multiply, client=client)
# to_json_dict() provides a clean JSON representation.
print(fn_decl.to_json_dict())
多工具使用方式:結合原生工具與函式呼叫
有了 Gemini 2.0,您可以同時啟用多個工具,並結合原生工具和函式呼叫功能。以下範例說明如何在使用 Live API 的請求中啟用兩個工具:使用 Google 搜尋進行定位和程式碼執行。
# Multiple tasks example - combining lights, code execution, and search
prompt = """
Hey, I need you to do three things for me.
1. Turn on the lights.
2. Then compute the largest prime palindrome under 100000.
3. Then use Google Search to look up information about the largest earthquake in California the week of Dec 5 2024.
Thanks!
"""
tools = [
{'google_search': {}},
{'code_execution': {}},
{'function_declarations': [turn_on_the_lights_schema, turn_off_the_lights_schema]} # not defined here.
]
# Execute the prompt with specified tools in audio modality
await run(prompt, tools=tools, modality="AUDIO")
// Multiple tasks example - combining lights, code execution, and search
const prompt = `
Hey, I need you to do three things for me.
1. Turn on the lights.
2. Then compute the largest prime palindrome under 100000.
3. Then use Google Search to look up information about the largest earthquake in California the week of Dec 5 2024.
Thanks!
`;
const tools = [
{ googleSearch: {} },
{ codeExecution: {} },
{ functionDeclarations: [turnOnTheLightsSchema, turnOffTheLightsSchema] } // not defined here.
];
// Execute the prompt with specified tools in audio modality
await run(prompt, {tools: tools, modality: "AUDIO"});
Python 開發人員可以在 Live API Tool Use notebook 中試用這項功能。
支援的型號
不包含實驗模型。您可以在「模型總覽」頁面中查看這些功能。
型號 | 函式呼叫 | 並行函式呼叫 | 組合函式呼叫 (僅限 Live API) |
---|---|---|---|
Gemini 2.0 Flash | ✔️ | ✔️ | ✔️ |
Gemini 2.0 Flash-Lite | X | X | X |
Gemini 1.5 Flash | ✔️ | ✔️ | ✔️ |
Gemini 1.5 Pro | ✔️ | ✔️ | ✔️ |
最佳做法
- 函式和參數說明:說明必須非常清楚且具體。模型會依據這些資訊選擇正確的函式,並提供適當的引數。
- 命名:使用描述性函式名稱 (不含空格、句點或破折號)。
- 強型別:請為參數使用特定類型 (整數、字串、列舉),以減少錯誤。如果參數的有效值有限,請使用列舉。
- 提示工程:
- 提供脈絡:告訴模型其角色 (例如「你是實用的天氣助理。」)。
- 提供指示:指定使用函式的時機和方式 (例如「不要猜測日期,請一律使用未來日期進行預測。」)。
- 鼓勵使用者釐清問題:視需要指示模型提出釐清問題。
- 溫度:請使用低溫 (例如 0) 以便更確定性且可靠的函式呼叫。
- 驗證:如果函式呼叫會造成重大後果 (例如下單),請先向使用者驗證呼叫,再執行呼叫。
- 錯誤處理:在函式中實作可靠的錯誤處理機制,以便妥善處理未預期的輸入內容或 API 失敗。傳回資訊性錯誤訊息,讓模型可用於為使用者產生實用的回覆。
- 安全性:呼叫外部 API 時,請留意安全性。使用適當的驗證和授權機制。避免在函式呼叫中公開機密資料。
- 符記限制:函式說明和參數會計入輸入符記限制。如果您達到符記限制,請考慮限制函式數量或說明長度,將複雜的工作細分為較小、更專注的函式組合。
注意事項和限制
- 僅支援 OpenAPI 架構的子集。
- Python 支援的參數類型有限。
- 自動函式呼叫功能僅適用於 Python SDK。