获取词元数和计费字符数

本页介绍如何获取提示的词元数和计费字符数。

支持的模型

以下多模态模型支持获取提示词元数:

  • gemini-1.0-pro
  • gemini-1.0-pro-vision

获取提示的词元数

您可以使用 Vertex AI API 获取提示的词元数和计费字符数。

Python

如需了解如何安装或更新 Python,请参阅安装 Python 版 Vertex AI SDK。 如需了解详情,请参阅 Python API 参考文档

import vertexai
from vertexai.generative_models import GenerativeModel

def generate_text(project_id: str, location: str) -> str:
    # Initialize Vertex AI
    vertexai.init(project=project_id, location=location)

    # Load the model
    model = GenerativeModel("gemini-1.0-pro")

    # prompt tokens count
    print(model.count_tokens("why is sky blue?"))

    # Load example images
    response = model.generate_content("why is sky blue?")

    # response tokens count
    print(response._raw_response.usage_metadata)
    return response.text

Node.js

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Node.js 设置说明执行操作。如需了解详情,请参阅 Vertex AI Node.js API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

const {VertexAI} = require('@google-cloud/vertexai');

/**
 * TODO(developer): Update these variables before running the sample.
 */
async function countTokens(
  projectId = 'PROJECT_ID',
  location = 'us-central1',
  model = 'gemini-1.0-pro'
) {
  // Initialize Vertex with your Cloud project and location
  const vertexAI = new VertexAI({project: projectId, location: location});

  // Instantiate the model
  const generativeModel = vertexAI.getGenerativeModel({
    model: model,
  });

  const req = {
    contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}],
  };

  const countTokensResp = await generativeModel.countTokens(req);
  console.log('count tokens response: ', countTokensResp);
}

REST

如需使用 Vertex AI API 获取提示的词元数和计费字符数,请向发布者模型端点发送 POST 请求。

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:处理请求的区域。可用的选项包括:

    点击即可展开可用的区域

    • us-central1
    • us-west4
    • northamerica-northeast1
    • us-east4
    • us-west1
    • asia-northeast3
    • asia-southeast1
    • asia-northeast1
  • PROJECT_ID:您的项目 ID
  • MODEL_ID:您要使用的多模态模型 ID。选项包括:
    • gemini-1.0-pro-vision
    • gemini-1.0-pro
  • ROLE:与内容关联的对话中的角色。即使在单轮应用场景中,也需要指定角色。 可接受的值包括:
    • USER:指定由您发送的内容。
  • TEXT:要包含在提示中的文本说明。
  • IMAGE_BYTES:字节序列(而不是字符)。
  • FILE_URI:要包含在提示中的图片或视频的 Cloud Storage URI。存储该文件的存储桶必须位于发送请求的 Google Cloud 项目中。您还必须指定 MIMETYPE
  • MIME_TYPE:在 datafileUri 字段中指定的图片、PDF 或视频的媒体类型。可接受的值包括:

    点击即可展开 MIME 类型

    • application/pdf
    • audio/mpeg
    • audio/mp3
    • audio/wav
    • image/png
    • image/jpeg
    • text/plain
    • video/mov
    • video/mpeg
    • video/mp4
    • video/mpg
    • video/avi
    • video/wmv
    • video/mpegps
    • video/flv

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:countTokens

请求 JSON 正文:

{
  "contents": {
    "role": "ROLE",
    "parts": [
      {
        "inlineData": {
          "mimeType": "MIME_TYPE",
          "data": "IMAGE_BYTES"
        }
      },
      {
        "fileData": {
          "mimeType": "MIME_TYPE",
          "fileUri": "FILE_URI"
        }
      },
      {
        "text": "TEXT"
      }
    ]
  },
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

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/MODEL_ID:countTokens"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$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/MODEL_ID:countTokens" | Select-Object -Expand Content

您应该收到类似以下内容的 JSON 响应。

包含图片或视频的文本的 curl 命令示例:

MODEL_ID="gemini-1.0-pro-vision"
PROJECT_ID="my-project"
PROMPT="Provide a summary with about two sentences for the following article."

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/MODEL_ID:computeTokens-d \
$'{
  "contents": [
    { "prompt": "'"$PROMPT"'" }
    { "inlineData": {"'"$MIME_TYPE"'": "image/jpeg", "data": "'"$IMAGE_BYTES"'" } },
    { "fileData": {"mimeType": "video/avi", "fileUri":"'"$FILE_URI"'" } } ] }}

纯文本的 curl 命令示例:

MODEL_ID="gemini-1.0-pro-vision"
PROJECT_ID="my-project"
PROMPT="Provide a summary with about two sentences for the following article."

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/MODEL_ID:computeTokens-d \
$'{
  "contents": [
    { "prompt": "'"$PROMPT"'"}
  ],
}'

价格和配额

使用 CountTokens API 无需付费或配额限制。CountTokens API 和 ComputeTokens API 的最大配额为每分钟 3000 个请求。

后续步骤