Gemini API 関数呼び出し機能を使用して、モデルにカスタム関数定義を提供できます。モデルは、これらの関数を直接呼び出すのではなく、関数名と推奨される引数を指定する構造化出力を生成します。関数名と引数を使用して外部 API を呼び出し、生成された API 出力をモデルへの次のクエリに組み込むことで、モデルがより包括的なレスポンスを提供して追加のアクションを実行できるようになります。
関数呼び出しにより、ユーザーはリアルタイムの情報やサービス(データベース、顧客管理システム、ドキュメント リポジトリなど)を操作できます。また、関連性の高いコンテキストに沿った回答を提供するモデルの能力も向上します。関数呼び出しは、外部システムとのやり取りに最適です。ユースケースでモデルが計算を実行する必要があるものの、外部システムや API が関与しない場合は、代わりにコード実行の使用を検討してください。
関数呼び出しの動作例については、「light bot」ノートブックをご覧ください。
関数呼び出しの仕組み
関数呼び出し機能を使用するには、プログラミング インターフェースを記述する構造化クエリデータ(関数宣言)をモデル プロンプトに追加します。関数宣言には、API 関数の名前、その目的、サポートされているパラメータ、それらのパラメータの説明が含まれます。クエリで関数宣言のリストをモデルに渡すと、モデルは関数宣言とクエリの残りの部分を分析し、リクエストに応じて宣言された API を使用する方法を決定します。
モデルは、ユーザーの質問に回答するために宣言された関数の 1 つ以上を呼び出す方法を指定する OpenAPI 互換スキーマのオブジェクトを返します。推奨される関数呼び出しパラメータを使用して実際の API を呼び出し、レスポンスを取得してユーザーに提供したり、さらにアクションを実行したりできます。モデルは宣言された関数を実際に呼び出すわけではありません。代わりに、返されたスキーマ オブジェクト パラメータを使用して関数を呼び出します。Gemini API は、モデルが 1 つのリクエストに基づいて複数の API 関数呼び出しを推奨する並列関数呼び出しもサポートしています。
関数宣言
プロンプトで関数呼び出しを実装する場合は、1 つ以上の function declarations
を含む tools
オブジェクトを作成します。関数は JSON を使用して定義します。具体的には、OpenAPI スキーマ形式の一部のサブセットを使用します。1 つの関数宣言には、次のパラメータを含めることができます。
name
(文字列): API 呼び出し内の関数の一意の識別子。description
(文字列): 関数の目的と機能の包括的な説明。parameters
(オブジェクト): 関数に必要な入力データを定義します。type
(文字列): 全体的なデータ型を指定します(object
など)。properties
(オブジェクト): 個々のパラメータと、それぞれに次の情報が一覧表示されます。type
(文字列): パラメータのデータ型(string
、integer
、boolean
など)。description
(文字列): パラメータの目的と想定される形式の明確な記述。
required
(配列): 関数の動作に必須のパラメータ名をリストした文字列の配列。
cURL コマンドを使用した関数宣言のコード例については、関数呼び出しの例をご覧ください。Gemini API SDK を使用して関数宣言を作成する例については、関数呼び出しのチュートリアルをご覧ください。
関数宣言のベスト プラクティス
関数をリクエストに統合する場合は、関数を正確に定義することが重要です。各関数は、モデルの動作とモデルとのインタラクションをガイドする特定のパラメータに依存します。次のリストは、functions_declarations
配列内の個々の関数のパラメータを定義する際のガイダンスを示しています。
name
: スペース、ピリオド(.
)、ダッシュ(-
)文字を使用せず、わかりやすい名前を付けます。代わりに、アンダースコア(_
)文字またはキャメルケースを使用します。description
: 関数の詳細、明確で具体的な説明を入力し、必要に応じて例を入力します。たとえば、find theaters
ではなくfind theaters based on location and optionally movie title that is currently playing in theaters.
を使用します。説明が過度に広範またはあいまいにならないようにしてください。properties
>type
: 厳密に型指定されたパラメータを使用して、モデルのハルシネーションを減らします。たとえば、パラメータ値が有限集合の場合、説明に値を一覧表示するのではなく、enum
フィールドを使用します(例:"type": "enum", "values": ["now_playing", "upcoming"]
)。パラメータ値が常に整数の場合は、型をnumber
ではなくinteger
に設定します。properties
>description
: 具体的な例と制約を指定します。たとえば、the location to search
ではなくThe city and state, e.g. San Francisco, CA or a zip code e.g. 95616
を使用します。
関数呼び出しを使用する際のその他のベスト プラクティスについては、ベスト プラクティスのセクションをご覧ください。
関数呼び出しモード
関数呼び出しの mode
パラメータを使用して、特徴の実行動作を変更できます。次の 3 つのモードがあります。
AUTO
: デフォルトのモデル動作。モデルは、関数呼び出しまたは自然言語によるレスポンスのどちらを予測するかを決定します。ANY
: モデルは常に関数呼び出しを予測するように制約されています。allowed_function_names
が指定されていない場合、モデルは使用可能なすべての関数宣言から選択します。allowed_function_names
が指定されている場合、モデルは許可された関数のセットから選択します。NONE
: モデルは関数呼び出しを予測しません。この場合、モデルの動作は、関数宣言を渡さない場合と同じです。
ANY
モード(「強制関数呼び出し」)の使用は、Gemini 1.5 Pro
モデルと Gemini 1.5 Flash
モデルでのみサポートされています。
allowed_function_names
のセットを指定して、モデルが呼び出す関数を制限することもできます。allowed_function_names
は、モードが ANY
の場合にのみ含める必要があります。関数名は関数宣言名と一致する必要があります。モードを ANY
に設定し、allowed_function_names
を設定すると、モデルは指定された関数名のセットから関数呼び出しを予測します。
サンプル リクエストの次のコード スニペットは、mode
を ANY
に設定し、許可された関数のリストを指定する方法を示しています。
"tool_config": {
"function_calling_config": {
"mode": "ANY",
"allowed_function_names": ["find_theaters", "get_showtimes"]
},
}
コンポジショナル関数呼び出し
Gemini 2.0 は、新しい関数呼び出し機能であるコンポーズ関数呼び出しをサポートしています。コンポジション関数呼び出しを使用すると、Gemini API はレスポンスの生成プロセスで複数のユーザー定義関数を自動的に呼び出します。たとえば、プロンプト "Get the temperature in my current location"
に応答するために、Gemini API は get_current_location()
関数と、位置情報をパラメータとして受け取る get_weather()
関数の両方を呼び出す場合があります。
コード実行によるコンポジション関数呼び出しには双方向ストリーミングが必要であり、新しい Multimodal Live API でのみサポートされています。以下は、コンポジショナル関数呼び出し、コード実行、Multimodal Live API を組み合わせて使用する方法を示した例です。
turn_on_the_lights_schema = {'name': 'turn_on_the_lights'}
turn_off_the_lights_schema = {'name': 'turn_off_the_lights'}
prompt = """
Hey, can you write run some python code to turn on the lights, wait 10s and then turn off the lights?
"""
tools = [
{'code_execution': {}},
{'function_declarations': [turn_on_the_lights_schema, turn_off_the_lights_schema]}
]
await run(prompt, tools=tools, modality="AUDIO")
Python デベロッパーは、Live API Tool Use ノートブックでこれを試すことができます。
マルチツールの使用
Gemini 2.0 では、複数のツールを同時に有効にできます。モデルがツールを呼び出すタイミングを決定します。以下は、Multimodal Live API を使用してリクエストで 2 つのツール(Google 検索によるグラウンドングとコード実行)を有効にする例です。
prompt = """
Hey, I need you to do three things for me.
1. Turn on the lights.
2. Then compute the largest prime palindrome under 100000.
3. Then use Google Search to look up information about the largest earthquake in California the week of Dec 5 2024.
Thanks!
"""
tools = [
{'google_search': {}},
{'code_execution': {}},
{'function_declarations': [turn_on_the_lights_schema, turn_off_the_lights_schema]}
]
await run(prompt, tools=tools, modality="AUDIO")
Python デベロッパーは、Live API Tool Use ノートブックでこれを試すことができます。
関数呼び出しの例
このセクションでは、cURL コマンドを使用した関数呼び出しのプロンプトの例を示します。例には、単一ターンと複数ターンのシナリオ、さまざまな関数呼び出しモードの有効化が含まれています。
この機能で cURL コマンドを使用する場合、関数とパラメータの情報は tools
要素に含まれます。tools
要素内の各関数宣言には関数名が含まれます。パラメータは、OpenAPI 互換スキーマと関数の説明を使用して指定します。
シングルターンの例
シングルターンでは、言語モデルを 1 回呼び出します。関数呼び出しでは、モデルに自然言語クエリと関数のリストを提供する場合にシングルターン手法を採用する場合があります。この場合、モデルは関数名、パラメータ、説明を含む関数宣言を使用して、呼び出す関数と、関数を呼び出すために使用する引数を予測します。
次の curl サンプルは、ある映画が上映されている場所に関する情報を返す関数の説明を渡す例です。リクエストには、find_movies
や find_theaters
などの複数の関数宣言が含まれています。
シングルターン関数呼び出しのサンプル リクエスト
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": { "role": "user", "parts": { "text": "Which theaters in Mountain View show Barbie movie?" } }, "tools": [ { "function_declarations": [ { "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "string", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": [ "description" ] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" } }, "required": [ "location" ] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" }, "theater": { "type": "string", "description": "Name of the theater" }, "date": { "type": "string", "description": "Date for requested showtime" } }, "required": [ "location", "movie", "theater", "date" ] } } ] } ] }'
この curl の例に対するレスポンスは次のようになります。
シングルターン関数呼び出し curl のサンプル レスポンス
[{ "candidates": [ { "content": { "parts": [ { "functionCall": { "name": "find_theaters", "args": { "movie": "Barbie", "location": "Mountain View, CA" } } } ] }, "finishReason": "STOP", "safetyRatings": [ { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } ], "usageMetadata": { "promptTokenCount": 9, "totalTokenCount": 9 } }]
ANY モードを使用した単一ターンの例
次の curl の例は、単一ターンの例と似ていますが、モードを ANY
に設定しています。
"tool_config": {
"function_calling_config": {
"mode": "ANY"
},
}
ANY モード(リクエスト)を使用したシングルターン関数呼び出し
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": { "role": "user", "parts": { "text": "What movies are showing in North Seattle tonight?" } }, "tools": [ { "function_declarations": [ { "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "string", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": [ "description" ] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" } }, "required": [ "location" ] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" }, "theater": { "type": "string", "description": "Name of the theater" }, "date": { "type": "string", "description": "Date for requested showtime" } }, "required": [ "location", "movie", "theater", "date" ] } } ] } ], "tool_config": { "function_calling_config": { "mode": "ANY" }, } }'
レスポンスは次のようになります。
ANY モードを使用したシングルターン関数呼び出し(レスポンス)
{ "candidates": [ { "content": { "parts": [ { "functionCall": { "name": "find_movies", "args": { "description": "", "location": "North Seattle, WA" } } } ], "role": "model" }, "finishReason": "STOP", "index": 0, "safetyRatings": [ { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" } ] } ], "promptFeedback": { "safetyRatings": [ { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } }
ANY モードと許可された関数を使用するシングルターンの例
次の curl の例は、単一ターンの例と似ていますが、モードを ANY
に設定し、許可される関数のリストを含めています。
"tool_config": {
"function_calling_config": {
"mode": "ANY",
"allowed_function_names": ["find_theaters", "get_showtimes"]
},
}
ANY モードと許可された関数を使用したシングルターン関数呼び出し(リクエスト)
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": { "role": "user", "parts": { "text": "What movies are showing in North Seattle tonight?" } }, "tools": [ { "function_declarations": [ { "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "string", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": [ "description" ] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" } }, "required": [ "location" ] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "string", "description": "Any movie title" }, "theater": { "type": "string", "description": "Name of the theater" }, "date": { "type": "string", "description": "Date for requested showtime" } }, "required": [ "location", "movie", "theater", "date" ] } } ] } ], "tool_config": { "function_calling_config": { "mode": "ANY", "allowed_function_names": ["find_theaters", "get_showtimes"] }, } }'
find_movies
関数は許可された関数のリストにないため、モデルは find_movies
関数を予測できません。代わりに、別の関数を予測します。レスポンスは次のようになります。
ANY モードと許可された関数を使用したシングルターン関数呼び出し(レスポンス)
{ "candidates": [ { "content": { "parts": [ { "functionCall": { "name": "find_theaters", "args": { "location": "North Seattle, WA", "movie": null } } } ], "role": "model" }, "finishReason": "STOP", "index": 0, "safetyRatings": [ { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } ], "promptFeedback": { "safetyRatings": [ { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } }
マルチターンの例
マルチターン関数呼び出しのシナリオは、次の手順で実施できます。
- 言語モデルを呼び出して、関数呼び出しのレスポンスを取得します。これが最初のターンです。
- 最初のターンの関数呼び出しレスポンスと、その関数の呼び出しで取得した関数レスポンスを使用して、言語モデルを呼び出します。次は 2 回目のターンです。
2 回目のターンで得られるレスポンスは、最初のターンでクエリに応答するために結果を要約するか、クエリの詳細情報を取得するために使用できる 2 番目の関数呼び出しを格納します。
このトピックには、2 つのマルチターン curl の例が含まれています。
前のターンのレスポンスを使用する
次の curl サンプルは、前述のシングルターンの例で返された関数と引数を呼び出して、レスポンスを取得します。シングルターンの例から返されるメソッドとパラメータは、次の JSON に含まれています。
"functionCall": {
"name": "find_theaters",
"args": {
"movie": "Barbie",
"location": "Mountain View, CA"
}
}
マルチターン関数呼び出し curl のサンプル リクエスト
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": [{ "role": "user", "parts": [{ "text": "Which theaters in Mountain View show Barbie movie?" }] }, { "role": "model", "parts": [{ "functionCall": { "name": "find_theaters", "args": { "location": "Mountain View, CA", "movie": "Barbie" } } }] }, { "role": "user", "parts": [{ "functionResponse": { "name": "find_theaters", "response": { "name": "find_theaters", "content": { "movie": "Barbie", "theaters": [{ "name": "AMC Mountain View 16", "address": "2000 W El Camino Real, Mountain View, CA 94040" }, { "name": "Regal Edwards 14", "address": "245 Castro St, Mountain View, CA 94040" }] } } } }] }], "tools": [{ "functionDeclarations": [{ "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "STRING", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": ["description"] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" } }, "required": ["location"] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" }, "theater": { "type": "STRING", "description": "Name of the theater" }, "date": { "type": "STRING", "description": "Date for requested showtime" } }, "required": ["location", "movie", "theater", "date"] } }] }] }'
この curl の例に対するレスポンスには、find_theaters
メソッドを呼び出した結果が含まれます。レスポンスは次のようになります。
マルチターン関数呼び出し curl のサンプル レスポンス
{ "candidates": [ { "content": { "parts": [ { "text": " OK. Barbie is showing in two theaters in Mountain View, CA: AMC Mountain View 16 and Regal Edwards 14." } ] } } ], "usageMetadata": { "promptTokenCount": 9, "candidatesTokenCount": 27, "totalTokenCount": 36 } }
モデルを複数回呼び出す
次の cURL の例では、生成 AI モデルを複数回呼び出して関数を呼び出します。言語モデルが関数を呼び出すたびに、異なる関数を使用して、リクエスト内の異なるユーザークエリに応答できます。
マルチターン関数呼び出し curl のサンプル リクエスト
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \ -H 'Content-Type: application/json' \ -d '{ "contents": [{ "role": "user", "parts": [{ "text": "Which theaters in Mountain View show Barbie movie?" }] }, { "role": "model", "parts": [{ "functionCall": { "name": "find_theaters", "args": { "location": "Mountain View, CA", "movie": "Barbie" } } }] }, { "role": "user", "parts": [{ "functionResponse": { "name": "find_theaters", "response": { "name": "find_theaters", "content": { "movie": "Barbie", "theaters": [{ "name": "AMC Mountain View 16", "address": "2000 W El Camino Real, Mountain View, CA 94040" }, { "name": "Regal Edwards 14", "address": "245 Castro St, Mountain View, CA 94040" }] } } } }] }, { "role": "model", "parts": [{ "text": " OK. Barbie is showing in two theaters in Mountain View, CA: AMC Mountain View 16 and Regal Edwards 14." }] },{ "role": "user", "parts": [{ "text": "Can we recommend some comedy movies on show in Mountain View?" }] }], "tools": [{ "functionDeclarations": [{ "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "STRING", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": ["description"] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which is currently playing in theaters", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" } }, "required": ["location"] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" }, "theater": { "type": "STRING", "description": "Name of the theater" }, "date": { "type": "STRING", "description": "Date for requested showtime" } }, "required": ["location", "movie", "theater", "date"] } }] }] }'
マルチターン関数呼び出し curl のサンプル レスポンス
[{ "candidates": [ { "content": { "parts": [ { "functionCall": { "name": "find_movies", "args": { "description": "comedy", "location": "Mountain View, CA" } } } ] }, "finishReason": "STOP", "safetyRatings": [ { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } ], "usageMetadata": { "promptTokenCount": 48, "totalTokenCount": 48 } } ]
ベスト プラクティス
関数呼び出しの精度と信頼性を高めるには、次のベスト プラクティスに従ってください。
ユーザーによるプロンプト
最適な結果を得るには、ユーザークエリの先頭に次の詳細を付加します。
- モデルの追加コンテキスト。例:
You are a movie API assistant to help users find movies and showtimes based on their preferences.
- 関数の使用方法と使用のタイミングに関する詳細や手順。例:
Don't make assumptions on showtimes. Always use a future date for showtimes.
- ユーザーからの質問があいまいな場合に、質問を明確にするための指示。例:
Ask clarifying questions if not enough information is available to complete the request.
サンプリング パラメータ
Temperature パラメータには、0
または別の小さい値を使用します。これにより、より確信度の高い結果を生成してハルシネーションを減らすようにモデルに指示します。
API 呼び出し
注文の送信やデータベースの更新など、重大な結果をもたらすような関数の呼び出しをモデルが提案する場合は、それを実行する前にユーザーにその関数呼び出しの妥当性を確認してください。