生成式 AI 模型会将文本数据细分为单元(称为“词元”)以进行处理。文本数据转换为词元的方式取决于所使用的词元化器。词元可以是字符、字词或短语。每个模型具有在提示和响应中可以处理的词元数上限。本页面介绍了如何获取提示的词元数和计费字符数。
支持的模型
以下基础模型支持获取提示词元数:
text-bison
chat-bison
code-bison
codechat-bison
code-gecko
textembedding-gecko
如需了解如何获取 Gemini 模型的提示词元数,请参阅 Vertex AI Gemini API 获取词元数说明。
获取提示的词元数
您可以使用 countTokens
API 获取提示的词元数和计费字符数。countTokens
的输入格式取决于您使用的模型。每种输入格式都与 predict
输入格式相同。
text-bison
REST
如需使用 Vertex AI API 获取提示的词元数和计费字符数,请向发布者模型端点发送 POST 请求。
在使用任何请求数据之前,请先进行以下替换:
- LOCATION:输入支持的区域。如需查看支持的区域的完整列表,请参阅可用位置。
- PROJECT_ID:您的项目 ID。
- PROMPT:要获取其词元数和计费字符数的提示。(请勿为此处的提示添加引号。)
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/text-bison:countTokens
请求 JSON 正文:
{ "instances": [ { "prompt": "PROMPT" } ] }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF' { "instances": [ { "prompt": "PROMPT" } ] } EOF
然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/text-bison:countTokens"
PowerShell
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@' { "instances": [ { "prompt": "PROMPT" } ] } '@ | Out-File -FilePath request.json -Encoding utf8
然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/text-bison:countTokens" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
{ "totalTokens": 12, "totalBillableCharacters": 54 }
示例 curl 命令
PROJECT_ID="PROJECT_ID" curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/text-bison:countTokens -d \ $'{ "instances": [ { "prompt": "Give me ten interview questions for the role of program manager." }, { "prompt": "List some good qualities for a program manager." } ] }'
chat-bison
REST
如需使用 Vertex AI API 获取提示的词元数和计费字符数,请向发布者模型端点发送 POST 请求。
在使用任何请求数据之前,请先进行以下替换:
- LOCATION:输入支持的区域。如需查看支持的区域的完整列表,请参阅可用位置。
- PROJECT_ID:您的项目 ID。
- CONTEXT:可选。 上下文可以是您为模型提供的响应方式的说明,也可以是模型使用或引用来生成响应的信息。当您需要向模型提供信息时,请在提示中添加上下文信息,或者将响应的边界限制为仅上下文中的内容。
- EXAMPLE_AUTHOR_1:
EXAMPLE_INPUT
的作者(用户)。 - EXAMPLE_INPUT:消息的样本。
- EXAMPLE_AUTHOR_2:
EXAMPLE_OUTPUT
的作者(聊天机器人)。 - EXAMPLE_OUTPUT:理想响应的样本。
- AUTHOR_1:消息的作者(用户)。
- CONTENT:消息的内容。
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/chat-bison:countTokens
请求 JSON 正文:
{ "instances": [ { "context": "CONTEXT", "examples": [ { "input": { "author": "EXAMPLE_AUTHOR_1", "content": "EXAMPLE_INPUT" }, "output": { "author": "EXAMPLE_AUTHOR_2", "content": "EXAMPLE_OUTPUT" } } ], "messages": [ { "author": "AUTHOR_1", "content": "CONTENT" } ] } ] }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF' { "instances": [ { "context": "CONTEXT", "examples": [ { "input": { "author": "EXAMPLE_AUTHOR_1", "content": "EXAMPLE_INPUT" }, "output": { "author": "EXAMPLE_AUTHOR_2", "content": "EXAMPLE_OUTPUT" } } ], "messages": [ { "author": "AUTHOR_1", "content": "CONTENT" } ] } ] } EOF
然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/chat-bison:countTokens"
PowerShell
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@' { "instances": [ { "context": "CONTEXT", "examples": [ { "input": { "author": "EXAMPLE_AUTHOR_1", "content": "EXAMPLE_INPUT" }, "output": { "author": "EXAMPLE_AUTHOR_2", "content": "EXAMPLE_OUTPUT" } } ], "messages": [ { "author": "AUTHOR_1", "content": "CONTENT" } ] } ] } '@ | Out-File -FilePath request.json -Encoding utf8
然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/chat-bison:countTokens" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
{ "totalTokens": 43, "totalBillableCharacters": 182 }
示例 curl 命令
PROJECT_ID="PROJECT_ID" curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/chat-bison:countTokens -d \ $'{ "instances": [ { "context": "You are Captain Bartholomew, the most feared pirate dog of the seven seas.", "examples": [ { "input": { "author": "User", "content": "Hello!" }, "output": { "author": "Captain Barktholomew", "content": "Argh! What brings ye to my ship?" } }, { "input": { "author": "User", "content": "Who are you?" }, "output": { "author": "Captain Barktholomew", "content": "I be Captain Barktholomew, the most feared pirate dog of the seven seas." } } ], "messages": [ { "author": "User", "content": "Hello!" }, { "author": "Captain Barktholomew", "content": "Ahoy there, landlubber! What brings ye to me ship?" }, { "author": "User", "content": "Can you tell me a tale of your most recent adventure?" }, { "author": "Captain Barktholomew", "content": "Aye, I\'ll spin ye a tale of me latest adventure....", }, { "author": "User", "content": "I\'m listening." } ] } ] }'
code-bison
REST
如需使用 Vertex AI API 获取提示的词元数和计费字符数,请向发布者模型端点发送 POST 请求。
在使用任何请求数据之前,请先进行以下替换:
- LOCATION:输入支持的区域。如需查看支持的区域的完整列表,请参阅可用位置。
- PROJECT_ID:您的项目 ID。
- PREFIX:对于代码模型,
prefix
表示一条有意义的编程代码的开头,或描述要生成的代码的自然语言提示。
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/code-bison:countTokens
请求 JSON 正文:
{ "instances": [ { "prefix": "PREFIX" } ] }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF' { "instances": [ { "prefix": "PREFIX" } ] } EOF
然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/code-bison:countTokens"
PowerShell
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@' { "instances": [ { "prefix": "PREFIX" } ] } '@ | Out-File -FilePath request.json -Encoding utf8
然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/code-bison:countTokens" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
{ "totalTokens": 43, "totalBillableCharacters": 182 }
示例 curl 命令
PROJECT_ID=PROJECT_ID curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/code-bison:countTokens -d \ $'{ "instances": [ { "prefix": "Write a function that checks if a year is a leap year." } ] }'
codechat-bison
REST
如需使用 Vertex AI API 获取提示的词元数和计费字符数,请向发布者模型端点发送 POST 请求。
在使用任何请求数据之前,请先进行以下替换:
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/codechat-bison:countTokens
请求 JSON 正文:
{ "instances": { "messages": [ { "author": "AUTHOR", "content": "CONTENT" } ] } }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF' { "instances": { "messages": [ { "author": "AUTHOR", "content": "CONTENT" } ] } } EOF
然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/codechat-bison:countTokens"
PowerShell
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@' { "instances": { "messages": [ { "author": "AUTHOR", "content": "CONTENT" } ] } } '@ | Out-File -FilePath request.json -Encoding utf8
然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/codechat-bison:countTokens" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
{ "totalTokens": 43, "totalBillableCharacters": 182 }
示例 curl 命令
PROJECT_ID=PROJECT_ID curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/codechat-bison:countTokens -d \ $'{ "instances": { "messages": [ { "author": "user", "content": "Hi, how are you?" }, { "author": "system", "content": "I am doing good. What Can I help you with in the coding world?" }, { "author": "user", "content": "Please help write a function to calculate the min of two numbers" } ] } }'
code-gecko
REST
如需使用 Vertex AI API 获取提示的词元数和计费字符数,请向发布者模型端点发送 POST 请求。
在使用任何请求数据之前,请先进行以下替换:
- LOCATION:输入支持的区域。如需查看支持的区域的完整列表,请参阅可用位置。
- PROJECT_ID:您的项目 ID。
- PREFIX:对于代码模型,
prefix
表示一条有意义的编程代码的开头,或描述要生成的代码的自然语言提示。模型尝试在prefix
和suffix
之间填充代码。 - SUFFIX:对于代码补全,
suffix
表示一条有意义的编程代码的结尾。模型尝试在prefix
和suffix
之间填充代码。
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/code-gecko:countTokens
请求 JSON 正文:
{ "instances": [ { "prefix": "PREFIX", "suffix": "SUFFIX" } ] }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF' { "instances": [ { "prefix": "PREFIX", "suffix": "SUFFIX" } ] } EOF
然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/code-gecko:countTokens"
PowerShell
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@' { "instances": [ { "prefix": "PREFIX", "suffix": "SUFFIX" } ] } '@ | Out-File -FilePath request.json -Encoding utf8
然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/code-gecko:countTokens" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
{ "totalTokens": 43, "totalBillableCharacters": 182 }
示例 curl 命令
PROJECT_ID=PROJECT_ID curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/code-gecko:countTokens -d \ $'{ "instances": [ { "prefix": "def reverse_string(s):", "suffix": "" } ] }'
textembedding-gecko
REST
如需使用 Vertex AI API 获取提示的词元数和计费字符数,请向发布者模型端点发送 POST 请求。
在使用任何请求数据之前,请先进行以下替换:
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/textembedding-gecko:countTokens
请求 JSON 正文:
{ "instances": [ { "content": "TEXT" } ] }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF' { "instances": [ { "content": "TEXT" } ] } EOF
然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/textembedding-gecko:countTokens"
PowerShell
将请求正文保存在名为 request.json
的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@' { "instances": [ { "content": "TEXT" } ] } '@ | Out-File -FilePath request.json -Encoding utf8
然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/textembedding-gecko:countTokens" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
{ "totalTokens": 43, "totalBillableCharacters": 182 }
示例 curl 命令
PROJECT_ID=PROJECT_ID curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/textembedding-gecko:countTokens -d \ $'{ "instances": [ { "content": "What is life?" } ] }'
价格和配额
CountTokens
API 可免费使用。CountTokens
API 和 ComputeTokens
API 的最大配额为每分钟 3000 个请求。