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?
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?
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
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-03-21 (世界標準時間)。"],[],[],null,["# Function calling with Gemma\n\nWhen using a generative artificial intelligence (AI) model such as Gemma, you\nmay want to use the model to operate programming interfaces in order to complete\ntasks or answer questions. Instructing a model by defining a programming\ninterface and then making a request that uses that interface is called *function\ncalling*.\n| **Important:** *A Gemma model cannot execute code on its own.* When you generate code with function calling, you must run the generated code yourself or run it as part of your application. Always put safeguards in place to validate any generated code before executing it.\n\nGemma does not output a tool specific token.\nYour framework must detect a tool call by checking if the structure of\nthe output matches your prompted function output specification.\n\nYou can use function calling for a number of applications:\n\n- **Create a natural language interface** for a programming API to allow non-programmers to operate a programmatic interface without coding.\n- **Generate programming calls** as part of an AI agent workflow\n\nFunction calling is supported in Gemma 3, but the function calling\ntechnique can be used with prior versions of Gemma. This guide provides\ninstructions on how to construct Gemma prompts that use function calling.\nWe recommend Gemma3 27B for the best performance,\nand Gemma3 12B for balanced performance and latency.\n\nCall programming functions\n--------------------------\n\nYou can use function calling with Gemma by constructing a prompt that provides\ninstructions that specify the output format and\ndefine the available functions.\n\nWhen the user prompt is included, the model outputs a function call,\nwhich is a string that matches your specified output format.\nThat signals a request to be parsed by your model framework to call the defined\nfunctions.\n\nThe following prompting sample\nshows a function definition block, along with a function call syntax,\nand a function call output from the model.\nThe following example prompt is meant to be used with a\nprogramming interface for a product catalog: \n\n```\nYou have access to functions. If you decide to invoke any of the function(s),\n you MUST put it in the format of\n[func_name1(params_name1=params_value1, params_name2=params_value2...), func_name2(params)]\n\nYou SHOULD NOT include any other text in the response if you call a function\n[\n {\n \"name\": \"get_product_name_by_PID\",\n \"description\": \"Finds the name of a product by its Product ID\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"PID\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"PID\"\n ]\n }\n }\n]\nWhile browsing the product catalog, I came across a product that piqued my\ninterest. The product ID is 807ZPKBL9V. Can you help me find the name of this\nproduct?\n```\n\nThis prompt should produce the following response: \n\n```\n[get_product_name_by_PID(PID=\"807ZPKBL9V\")]\n```\n\nThis example uses a Python style function call output.\nAlternatively, you can specify a JSON style output format, as shown in the\nfollowing example: \n\n```\nYou have access to functions. If you decide to invoke any of the function(s),\nyou MUST put it in the format of\n{\"name\": function name, \"parameters\": dictionary of argument name and its value}\n\nYou SHOULD NOT include any other text in the response if you call a function\n[\n {\n \"name\": \"get_product_name_by_PID\",\n \"description\": \"Finds the name of a product by its Product ID\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"PID\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"PID\"\n ]\n }\n }\n]\nWhile browsing the product catalog, I came across a product that piqued my\ninterest. The product ID is 807ZPKBL9V. Can you help me find the name of this\nproduct?\n```\n\nThis prompt should produce the following response: \n\n```\n{\"name\": \"get_product_name_by_PID\", \"parameters\": {\"PID\": \"807ZPKBL9V\"}}\n```\n\nComponents of function calling prompt\n-------------------------------------\n\nWhen using function calling with Gemma models, your prompt of the model should\nfollow this specific order and structure:\n\n1. [Function calling *setup*](#function-calling-setup)\n2. [Function *definitions*](#function-definition)\n\nThe following sections provide more detail on each of these prompting\ncomponents.\n\n### Function calling setup\n\nThe *setup* section of the function calling prompt sets the overall expected\nbehavior of the model. You can add additional, general instructions for the\nmodel's behavior in this section, such as specifying that the output should be\ndisplayed using a `print` or `console.log` function. Use Markdown-style single\nbackticks (`func_name`) to indicate code syntax. \n\n```\nYou have access to functions. If you decide to invoke any of the function(s),\nyou MUST put it in the format of\n{\"name\": function name, \"parameters\": dictionary of argument name and its value}\n\nYou SHOULD NOT include any other text in the response if you call a function\n```\n\nThese instructions should be as clear and brief as possible. Prioritize the\nmost important instructions and be cautious about providing many general\ninstructions. Gemma models may ignore instructions that are too detailed or not\nclearly expressed, particularly when you are using model versions with a lower\nparameter count.\n\n### Function definition\n\nThe *definition* section of the prompt provides the function name, parameters,\nand output, including a description for each.\nYou can declare functions in the format shown.\nSingle or multiple functions can be defined within the function declaration\nblock. \n\n```\n[\n {\n \"name\": \"get_product_name_by_PID\",\n \"description\": \"Finds the name of a product by its Product ID\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"PID\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"PID\"\n ]\n }\n },\n {\n \"name\": \"get_product_price_by_PID\",\n \"description\": \"Finds the price of a product by its Product ID\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"PID\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"PID\"\n ]\n }\n }\n]\n```\n\nNext steps\n----------\n\nCheck out ways to deploy and run Gemma models:\n\n- [Gemma Function Calling Notebook](https://colab.sandbox.google.com/github/google-gemini/gemma-cookbook/blob/main/Gemma/%5BGemma_2%5DAgentic_AI.ipynb)\n- [Run Gemma with Ollama](https://ai.google.dev/gemma/docs/integrations/ollama)\n- [Gemma in PyTorch](https://ai.google.dev/gemma/docs/pytorch_gemma)"]]