借助函数调用,您可以定义自定义函数,并为 LLM 提供调用这些函数的能力,以便检索实时信息或与 SQL 数据库或客户服务工具等外部系统进行交互。
如需详细了解函数调用的概念性信息,请参阅函数调用简介。
准备工作
如需将开放模型与 Gemini Enterprise Agent Platform 搭配使用,您必须执行以下步骤。必须启用 Agent Platform API (aiplatform.googleapis.com) 和 Cloud Commerce Consumer Procurement API (cloudcommerceconsumerprocurement.googleapis.com),才能在 Gemini Enterprise Agent Platform 上使用开放模型。如果您已有启用了这些 API 的项目,则可以使用该项目,而无需创建新项目。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Gemini Enterprise Agent Platform and Cloud Commerce Consumer Procurement APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.- 前往要使用的模型的 Model Garden 模型卡片,然后点击启用,以在项目中启用该模型。
所需的角色
如需获得启用和使用开放模型所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:
- Agent Platform User (
roles/aiplatform.user) - Consumer Procurement Entitlement Manager (
roles/consumerprocurement.entitlementManager)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
如需详细了解如何为开放模型授予权限和配置组织政策,请参阅向用户授予开放模型的使用权限。
使用函数调用
以下示例展示了如何使用函数调用。
Python
在尝试此示例之前,请按照《Agent Platform 快速入门:使用客户端库》中的 Python 设置说明执行操作。
如需向 Agent Platform 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
在运行此示例之前,请务必设置 OPENAI_BASE_URL 环境变量。如需了解详情,请参阅身份验证和凭证。
from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="MODEL", messages=[ {"role": "user", "content": "CONTENT"} ], tools=[ { "type": "function", "function": { "name": "FUNCTION_NAME", "description": "FUNCTION_DESCRIPTION", "parameters": PARAMETERS_OBJECT, } } ], tool_choice="auto", )
- MODEL:您要使用的模型名称,例如
zai-org/glm-5.2-maas。 - CONTENT:要发送给模型的用户提示。
- FUNCTION_NAME:要调用的函数名称。
- FUNCTION_DESCRIPTION:函数的说明。
- PARAMETERS_OBJECT:定义函数参数的字典,例如:
{"type": "object", "properties": {"location": {"type": "string", "description": "The city and state"}}, "required": ["location"]}
REST
设置您的环境后,您可以使用 REST 测试文本提示词。以下示例会向发布方模型端点发送请求。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- MODEL:您要使用的模型名称,例如
zai-org/glm-5.2-maas。 - CONTENT:要发送给模型的用户提示。
- FUNCTION_NAME:要调用的函数名称。
- FUNCTION_DESCRIPTION:函数的说明。
- PARAMETERS_OBJECT:定义函数参数的 JSON 架构对象,例如:
{"type": "object", "properties": {"location": {"type": "string", "description": "The city and state"}}, "required": ["location"]}
HTTP 方法和网址:
POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions
请求 JSON 正文:
{
"model": "MODEL",
"messages": [
{
"role": "user",
"content": "CONTENT"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "FUNCTION_NAME",
"description": "FUNCTION_DESCRIPTION",
"parameters": PARAMETERS_OBJECT
}
}
],
"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/v1/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/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions" | Select-Object -Expand Content
您应该会收到一个成功的状态代码 (2xx) 和一个空响应。
示例
下面显示了使用 get_current_weather 函数提取气象信息后可能获得的完整输出。
Python
from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="zai-org/glm-5.2-maas", messages=[ { "role": "user", "content": "Which city has a higher temperature, Boston or new Delhi and by how much in F?" }, { "role": "assistant", "content": "I'll check the current temperatures for Boston and New Delhi in Fahrenheit and compare them. I'll call the weather function for both cities.", "tool_calls": [{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"},{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}] }, { "role": "tool", "content": "The temperature in Boston is 75 degrees Fahrenheit.", "tool_call_id": "get_current_weather" }, { "role": "tool", "content": "The temperature in New Delhi is 50 degrees Fahrenheit.", "tool_call_id": "get_current_weather" } ], 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" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ], tool_choice="auto" )
curl
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://aiplatform.googleapis.com/v1/projects/sample-project/locations/global/endpoints/openapi/chat/completions -d \ '{ "model": "zai-org/glm-5.2-maas", "messages": [ { "role": "user", "content": "Which city has a higher temperature, Boston or new Delhi and by how much in F?" }, { "role": "assistant", "content": "I'll check the current temperatures for Boston and New Delhi in Fahrenheit and compare them. I'll call the weather function for both cities.", "tool_calls": [{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"},{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}] }, { "role": "tool", "content": "The temperature in Boston is 75 degrees Fahrenheit.", "tool_call_id": "get_current_weather" }, { "role": "tool", "content": "The temperature in New Delhi is 50 degrees Fahrenheit.", "tool_call_id": "get_current_weather" } ], "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" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ], "tool_choice": "auto" }'
{ "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null, "message": { "content": "Based on the current weather data:\n\n- **Boston, MA**: 75°F \n- **New Delhi, India**: 50°F \n\n**Comparison**: \nBoston is **25°F warmer** than New Delhi. \n\n**Answer**: \nBoston has a higher temperature than New Delhi by 25 degrees Fahrenheit.", "role": "assistant" } } ], "created": 1750450289, "id": "2025-06-20|13:11:29.240295-07|6.230.75.101|-987540014", "model": "zai-org/glm-5.2-maas", "object": "chat.completion", "system_fingerprint": "", "usage": { "completion_tokens": 66, "prompt_tokens": 217, "total_tokens": 283 } }
特定于模型的指导
以下部分提供了针对特定模型的函数调用指导。
DeepSeek
如果您使用系统提示,DeepSeek 模型在函数调用方面的表现不佳。为获得最佳性能,在使用 DeepSeek 模型进行函数调用时,请省略系统提示。
Llama
meta/llama3-405b-instruct-maas 不支持 tool_choice = 'required'。
OpenAI
使用 openai/gpt-oss-120b-maas 时,请将工具定义放在系统提示中,以获得最佳性能。例如:
{"messages": [ {"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required:\n..."}, {"role": "user", "content": "What's the weather like in Boston?"}, ... ]}
此模型不支持 tool_choice = 'required' 或命名工具调用。