跳至内容

如何查看在 Amazon Bedrock 中调用模型时的令牌数量?

2 分钟阅读
0

我想查看在 Amazon Bedrock 中调用模型时的输入和输出令牌数量。

解决方法

**注意:**如果您在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请参阅 AWS CLI 错误故障排除。此外,请确保您使用的是最新版本的 AWS CLI

要查看令牌计数,请使用以下方法之一。

使用 Amazon Bedrock 操场

要使用操场查看调用模型时的输入和输出令牌数量,请参阅使用操场在控制台中生成响应

检查模型调用日志

**先决条件:**您必须创建一个将日志传送到的目标。您可以使用 Amazon Simple Storage Service (Amazon S3) 设置 S3 存储桶,或使用 Amazon CloudWatch Logs 设置日志组。如果您使用 S3 存储桶,请向该存储桶添加存储桶策略,并在其中包含设置 Amazon S3 目标步骤 2 中的策略语句。如果您使用日志组,请创建一个 AWS Identity and Access Management (IAM) 角色,并在其中包含设置 CloudWatch Logs 目标步骤 2 中的信任关系和策略语句。

要检查日志,请完成以下步骤:

  1. 使用 CloudWatch 控制台API 启用所有模型调用的日志记录。
  2. 在日志中,查看每个模型调用的 inputTokensoutputTokens 数量。

日志示例:

{
    "schemaType": "ModelInvocationLog",
    "schemaVersion": "1.0",
    "timestamp": "2025-03-31T21:34:03Z",
    "accountId": "123456789",
    "identity": {
        "arn": "arn:aws:sts::123456789:role/MyRole"
    },
    "region": "us-east-1",
    "requestId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
    "operation": "ConverseStream",
    "modelId": "amazon.nova-pro-v1:0",
    "input": {
        "inputContentType": "application/json",
        "inputBodyJson": {
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "text": "Good morning."
                        }
                    ]
                }
            ],
            "inferenceConfig": {
                "maxTokens": 512,
                "temperature": 0.7,
                "topP": 0.9,
                "stopSequences": []
            },
            "additionalModelRequestFields": {}
        },
        "inputTokenCount": 3,
        "cacheReadInputTokenCount": 0,
        "cacheWriteInputTokenCount": 0
    },
    "output": {
        "outputContentType": "application/json",
        "outputBodyJson": {
            "output": {
                "message": {
                    "role": "assistant",
                    "content": [
                        {
                            "text": "Good morning! How can I assist you today? Whether you have questions, need information, or just want to chat, feel free to ask."
                        }
                    ]
                }
            },
            "stopReason": "end_turn",
            "metrics": {
                "latencyMs": 454
            },
            "usage": {
                "inputTokens": 3,
                "outputTokens": 29,
                "totalTokens": 32
            }
        },
        "outputTokenCount": 29
    }
}

检查 Converse 操作的响应

使用 AWS CLI 获取 Converse API 操作的响应。

运行以下含 converse 命令的 bedrock-runtime 命令:

aws bedrock-runtime converse \
    --region your-region\
    --model-id amazon.nova-pro-v1:0 \
    --messages '{"role": "user", "content": [{"text": "Hello"}]}'

**注意:**上述命令将获取 amazon.nova-pro-v1:0 模型的令牌计数。请将 your-region 替换为您的 AWS 区域,将 model-id 替换为您的模型 ID,并将 text 替换为您自己的消息。

响应示例:

{
    "output": {
        "message": {
            "role": "assistant",
            "content": [
                {
                    "text": "Hello! It's nice to have you here. I'm here to help with whatever you might need. Whether you have a question, need assistance with a topic, or just want to chat, feel free to ask. What can I assist you with today?"
                }
            ]
        }
    },
    "stopReason": "end_turn",
    "usage": {
        "inputTokens": 1,
        "outputTokens": 54,
        "totalTokens": 55
    },
    "metrics": {
        "latencyMs": 852
    }
}