如需使用 Gemini API 計算符記的詳細指南 (包括圖片、音訊和影片的計算方式),請參閱權杖計算指南及隨附的教戰手冊食譜。
方法:model.countTokens
針對輸入 Content
執行模型的符記化工具,並傳回符記數量。如要進一步瞭解權杖,請參閱權杖指南。
端點
則貼文
https://generativelanguage.googleapis.com/v1beta/{model=models/*}:countTokens
路徑參數
model
string
必要欄位。模型的資源名稱。這可做為模型使用的 ID。
這個名稱應與 models.list
方法傳回的模型名稱相符。
格式:models/{model}
格式為 models/{model}
。
要求主體
要求主體的資料會採用以下結構:
contents[]
object (Content
)
選用設定。以提示的形式提供給模型的輸入內容。設定 generateContentRequest
時,系統會忽略這個欄位。
generateContentRequest
object (GenerateContentRequest
)
要求範例
文字
Python
Node.js
Go
貝殼
Kotlin
Swift
Dart
Java
即時通訊
Python
Node.js
Go
貝殼
Kotlin
Swift
Dart
Java
內嵌媒體
Python
Node.js
Go
貝殼
Kotlin
Swift
Dart
Java
影片
Python
Node.js
Go
貝殼
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}
。
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 ( |