生成式模型會將資料拆解成稱為「詞元」的單元,以便處理。每種模型都有權杖數量上限,可處理提示和回覆。
本頁面說明如何使用 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}");