Gemma を使用した関数呼び出し

Gemma などの生成 AI モデルを使用する場合は、モデルを使用してプログラミング インターフェースを操作し、タスクを完了したり質問に回答したりできます。プログラミング インターフェースを定義し、そのインターフェースを使用するリクエストを送信してモデルに指示することを、関数呼び出しと呼びます。

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 モデルで関数呼び出しを使用する場合、モデルのプロンプトは次の特定の順序と構造に従う必要があります。

  1. 関数呼び出しの設定
  2. 関数の定義

以降のセクションでは、これらのプロンプト コンポーネントについて詳しく説明します。

関数呼び出しの設定

関数呼び出しプロンプトの [setup] セクションでは、モデルの全体的な動作を設定します。このセクションでは、モデルの動作に関する一般的な指示を追加できます。たとえば、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 モデルでは、特にパラメータ数が少ないモデル バージョンを使用している場合、指示が詳細すぎるか明確に表現されていない指示が無視されることがあります。

関数の定義

プロンプトの [定義] セクションには、関数名、パラメータ、出力(各関数の説明を含む)が表示されます。関数は次の形式で宣言できます。関数宣言ブロック内で 1 つまたは複数の関数を定義できます。

[
  {
    "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 モデルをデプロイして実行する方法を確認する。