使用生成式人工智能 (AI) 模型(例如 Gemma)时,您可能需要使用该模型操作编程接口,以便完成任务或回答问题。通过定义编程接口来指示模型,然后发出使用该接口的请求,称为函数调用。
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 模型的方法: