生成式模型會將資料拆解成稱為「符記」的單元,以便處理。每個模型都有詞元數量上限,可在提示和回覆中處理。
本頁面說明如何使用 Count Tokens API,針對對 Gemini 模型提出的要求,取得預估的符號數量和可計費字元數。沒有 API 可在回應中取得符記的預估值。
請注意,Count Tokens API 無法用於 Imagen 模型。
計數提供哪些資訊?
請注意以下關於計算符記和計費字元的事項:
計算總符記數
這項計數有助於確保您的要求不會超過允許的上下文視窗。
符記數量會反映大小,也就是所有要求輸入內容中提供的所有檔案 (例如圖片)。不會計算圖片數量或影片中的秒數。
對於所有 Gemini 模型,符記相當於約 4 個字元。100 個符記約等於 60 到 80 個英文單字。
計算總計費字元數
這項計數有助於瞭解及控管費用,因為對於 Vertex AI,字元數是定價計算的一部分。
可計費的字元數量會反映文字中提供的要求輸入內容字元數量。
對於較舊的 Gemini 模型,符記不是定價計算的一部分;但對於 Gemini 2.0 和 Gemini 2.5 模型,符記會用於定價計算。進一步瞭解每個模型的符記限制和每個模型的價格。
計算符號和計費字元的定價與配額
使用 CountTokens
API 不需付費,也沒有配額限制。CountTokens
API 的最高配額為每分鐘 3000 項要求 (RPM)。
程式碼範例
僅限文字輸入
Swift
let response = try await model.countTokens("Write a story about a magic backpack.")
print("Total Tokens: \(response.totalTokens)")
print("Total Billable Characters: \(response.totalBillableCharacters)")
Kotlin
val response = generativeModel.countTokens("Write a story about a magic backpack.")
println("Total Tokens: ${response.totalTokens}")
println("Total Billable Characters: ${response.totalBillableCharacters}")
Java
Content prompt = new Content.Builder()
.addText("Write a story about a magic backpack.")
.build();
GenerativeModelFutures modelFutures = GenerativeModelFutures.from(model);
ListenableFuture countTokensResponse =
modelFutures.countTokens(prompt);
Futures.addCallback(countTokensResponse, new FutureCallback() {
@Override
public void onSuccess(CountTokensResponse response) {
System.out.println("Total Tokens = " + response.getTotalTokens());
System.out.println("Total Billable Characters: = " +
response.getTotalBillableCharacters());
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
const { totalTokens, totalBillableCharacters } = await model.countTokens("Write a story about a magic backpack.");
console.log(`Total tokens: ${totalTokens}, total billable characters: ${totalBillableCharacters}`);
Dart
final tokenCount = await model.countTokens(Content.text("Write a story about a magic backpack."));
print('Token count: ${tokenCount.totalTokens}, billable characters: ${tokenCount.totalBillableCharacters}');
Unity
var response = await model.CountTokensAsync("Write a story about a magic backpack.");
UnityEngine.Debug.Log($"Total Tokens: {response.TotalTokens}");
UnityEngine.Debug.Log($"Total Billable Characters: {response.TotalBillableCharacters}");
多模態輸入內容
Swift
let response = try await model.countTokens(image, "What's in this picture?")
print("Total Tokens: \(response.totalTokens)")
print("Total Billable Characters: \(response.totalBillableCharacters)")
Kotlin
val prompt = content {
image(bitmap)
text("What's in this picture?")
}
val response = generativeModel.countTokens(prompt)
println("Total Tokens: ${response.totalTokens}")
println("Total Billable Characters: ${response.totalBillableCharacters}")
Java
Content prompt = new Content.Builder()
.addImage(bitmap)
.addText("What's in this picture?")
.build();
GenerativeModelFutures modelFutures = GenerativeModelFutures.from(model);
ListenableFuture countTokensResponse =
modelFutures.countTokens(prompt);
Futures.addCallback(countTokensResponse, new FutureCallback() {
@Override
public void onSuccess(CountTokensResponse response) {
System.out.println("Total Tokens = " + response.getTotalTokens());
System.out.println("Total Billable Characters: = " +
response.getTotalBillableCharacters());
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
const prompt = "What's in this picture?";
const imagePart = { inlineData: { mimeType: 'image/jpeg', data: imageAsBase64 }};
const { totalTokens, totalBillableCharacters } = await model.countTokens([prompt, imagePart]);
console.log(`Total tokens: ${totalTokens}, total billable characters: ${totalBillableCharacters}`);
Dart
final prompt = TextPart("What's in the picture?");
final tokenCount = await model.countTokens([
Content.multi([prompt, imagePart])
]);
print('Token count: ${tokenCount.totalTokens}, billable characters: ${tokenCount.totalBillableCharacters}');
Unity
var response = await model.CountTokensAsync(new [] {
ModelContent.Text("What's in this picture?"),
ModelContent.InlineData("image/png", imageData)
});
UnityEngine.Debug.Log($"Total Tokens: {response.TotalTokens}");
UnityEngine.Debug.Log($"Total Billable Characters: {response.TotalBillableCharacters}");