画像、音声、動画のカウント方法など、Gemini API を使用したトークンのカウントに関する詳細なガイドについては、トークン カウントガイドと付属のクックブック レシピをご覧ください。
メソッド: models.countTokens
- エンドポイント
- パスパラメータ
- リクエスト本文
<ph type="x-smartling-placeholder">
- </ph>
- JSON 表現
- レスポンスの本文
<ph type="x-smartling-placeholder">
- </ph>
- JSON 表現
- 認可スコープ
- リクエストの例 <ph type="x-smartling-placeholder">
- GenerateContentRequest
<ph type="x-smartling-placeholder">
- </ph>
- JSON 表現
入力 Content
に対してモデルのトークナイザを実行し、トークン数を返します。トークンの詳細については、トークンガイドをご覧ください。
エンドポイント
<ph type="x-smartling-placeholder"></ph> 投稿
https://generativelanguage.googleapis.com/v1beta/{model=models/*}:countTokens
パスパラメータ
model
string
必須。モデルのリソース名。これは、モデルが使用する ID として機能します。
この名前は、models.list
メソッドから返されるモデル名と一致する必要があります。
形式: models/{model}
。models/{model}
の形式になります。
リクエスト本文
リクエストの本文には、次の構造のデータが含まれます。
<ph type="x-smartling-placeholder">contents[]
object (Content
)
省略可。モデルにプロンプトとして与えられた入力。generateContentRequest
が設定されている場合、このフィールドは無視されます。
generateContentRequest
object (GenerateContentRequest
)
リクエスト例
テキスト
Python
Node.js
Go
Shell
Kotlin
Swift
Dart
Java
チャット
Python
Node.js
Go
Shell
Kotlin
Swift
Dart
Java
インライン メディア
Python
Node.js
Go
Shell
Kotlin
Swift
Dart
Java
動画
Python
Node.js
Go
Shell
Python
キャッシュ
Python
Node.js
Go
システム命令
Python
Node.js
Go
Kotlin
Swift
Dart
Java
ツール
Python
Node.js
Kotlin
Swift
Dart
Java
レスポンスの本文
models.countTokens
からのレスポンス。
prompt
に対するモデルの tokenCount
を返します。
成功した場合、レスポンスの本文には次の構造のデータが含まれます。
totalTokens
integer
Model
が prompt
をトークン化するトークンの数。常に負数ではありません。
JSON 表現 |
---|
{ "totalTokens": integer } |
GenerateContentRequest
モデルから補完を生成するリクエスト。
model
string
必須。コンプリーションの生成に使用する Model
の名前。
形式: name=models/{model}
。
contents[]
object (Content
)
必須。モデルとの現在の会話の内容。
シングルターンのクエリの場合、これは単一のインスタンスです。chat のようなマルチターン クエリの場合、これは会話履歴と最新のリクエストを含む繰り返しフィールドです。
tools[]
object (Tool
)
toolConfig
object (ToolConfig
)
省略可。リクエストで指定された任意の Tool
のツール構成。使用例については、関数呼び出しガイドをご覧ください。
safetySettings[]
object (SafetySetting
)
省略可。安全でないコンテンツをブロックするための一意の SafetySetting
インスタンスのリスト。
これは GenerateContentRequest.contents
と GenerateContentResponse.candidates
に適用されます。SafetyCategory
タイプごとに複数の設定を指定することはできません。API は、これらの設定で設定されたしきい値を満たしていないコンテンツとレスポンスをブロックします。このリストは、safetySettings で指定された各 SafetyCategory
のデフォルト設定をオーバーライドします。指定された SafetyCategory
の SafetySetting
がリストで指定された場合、API はそのカテゴリのデフォルトの安全性設定を使用します。有害カテゴリ HARM_CATEGORY_HATE_SPEECH、HARM_CATEGORY_SEXUALLY_EXPLICIT、HARM_CATEGORY_DANGEROUS_CONTENT、HARM_CATEGORY_HARASSMENT をサポートしています。利用可能な安全性設定について詳しくは、ガイドをご覧ください。また、AI アプリケーションに安全に関する考慮事項を組み込む方法については、安全に関するガイダンスをご覧ください。
generationConfig
object (GenerationConfig
)
省略可。モデルの生成と出力の構成オプション。
cachedContent
string
省略可。予測を提供するコンテキストとして使用するキャッシュに保存されたコンテンツの名前。形式: cachedContents/{cachedContent}
JSON 表現 |
---|
{ "model": string, "contents": [ { object ( |