使用 Gemma 进行 Function Calling(函数调用)

当使用像 Gemma 这样的生成式人工智能 (AI) 模型时,你可能希望利用该模型来操作编程接口,从而完成任务或回答问题。通过定义编程接口并发出使用该接口的请求来指示模型,这被称为 function calling(函数调用)。

重要提示: Gemma 模型本身无法执行代码。 当你使用 function calling 生成代码时,你必须自己运行生成的代码,或者将其作为应用程序的一部分运行。在执行任何生成的代码之前,务必采取安全措施来验证它们。

Gemma 不输出工具特定的 token。你的框架必须通过检查输出的结构是否与你提示的函数输出规范匹配来检测工具调用。

你可以将 function calling 用于多种应用场景:

Function calling 在 Gemma 3 中得到支持,但 function calling 技术可以与早期版本的 Gemma 一起使用。 本指南提供有关如何构造使用 function calling 的 Gemma prompts 的说明。 我们推荐 Gemma3 27B 以获得最佳性能,并推荐 Gemma3 12B 以获得平衡的性能和延迟。

调用编程函数

你可以通过构造一个 prompt 来使用 Gemma 的 function calling,该 prompt 提供指定输出格式并定义可用函数的指令。

当包含用户 prompt 时,模型会输出一个函数调用,这是一个与你指定的输出格式匹配的字符串。这表示需要由你的模型框架解析以调用已定义函数的请求。

以下 prompting 示例显示了一个函数定义块,以及一个函数调用语法和一个来自模型的函数调用输出。 以下示例 prompt 旨在与产品目录的编程接口一起使用:

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?

此 prompt 应生成以下响应:

[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?

此 prompt 应生成以下响应:

{"name": "get_product_name_by_PID", "parameters": {"PID": "807ZPKBL9V"}}

Function Calling Prompt 的组成部分

当使用 Gemma 模型进行 function calling 时,你的模型 prompt 应遵循以下特定顺序和结构:

  1. Function calling setup(设置)
  2. Function definitions(定义)

以下部分提供了有关这些 prompting 组件的更多详细信息。

Function Calling Setup(设置)

function calling prompt 的 setup 部分设置了模型的整体预期行为。你可以在本节中为模型的行为添加其他常规指令,例如指定应使用 printconsole.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 模型可能会忽略过于详细或表达不清的说明,尤其是在使用参数数量较低的模型版本时。

Function Definition(函数定义)

prompt 的 definition 部分提供了函数名称、参数和输出,包括每个参数和输出的描述。你可以按照所示格式声明函数。可以在函数声明块中定义单个或多个函数。

[
 {
  "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 模型的方法: