如需详细了解使用 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
运行模型的标记生成器,并返回词元数量。如需详细了解令牌,请参阅令牌指南。
端点
帖子
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
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}
。
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 ( |