AWS Builder Center: Learn, Build and Connect with builders in the AWS community
AWS Builder Center is the official home for builders on AWS. Share and read what others are working on, follow people who inspire you, explore training and workshops, and find tools to support what you're building.
如何在 Amazon Bedrock 中呼叫模型時檢查詞元數量?
我想在 Amazon Bedrock 中調用模型時,檢查輸入和輸出詞元的數量。
解決方法
**注意:**如果您在執行 AWS Command Line Interface (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 的信任關係和政策宣告。
若要檢查日誌,請完成以下步驟:
- 使用 CloudWatch 主控台或 API 啟用所有模型調用的記錄。
- 在日誌中,檢查每次模型調用的 inputTokens 和 outputTokens 的數量。
日誌範例:
{ "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 } }
相關內容
已提問 3 年前

