Amazon Bedrock API Key + HTTP API 调用指南

1. 创建 Bedrock API Key

Amazon Bedrock 提供了 API Key 访问方式以简化与需要基于 API key 进行身份验证的工具和框架的集成,开发人员只需极少的设置即可完成身份验证并通过AWS SDK 或 HTTP Endpoint 直接访问 Amazon Bedrock API。

下图左侧的流程显示了在 IAM AWS IAM Identity Center 或 IAM 中创建身份的默认流程,蓝色部分代表专门向 Amazon Bedrock 进行身份验证。这两个流程都涉及创建一个 Bedrock API Key,用于 Amazon Bedrock 身份验证:

 

API Key 类型

  • 短期密钥(推荐): 12小时有效,继承 IAM 权限,生产环境使用
  • 长期密钥: 可设置过期时间,仅用于开发测试

生成方式

  • 通过 AWS Web console:

    Amazon Bedrock Console > API Keys > Generate Short-term / Long-term API keys

  • 通过AWS CLI:
  • aws iam create-service-specific-credential --user-name your-user --service-name bedrock.amazonaws.com
    

2. 创建 Inference profile

⚠️ 注意: 目前只能通过 API 创建用户自定义的 inference profile ,控制台暂不支持。详见 AWS 官方文档

AWS CLI 创建自定义 inference profile

# 基于基础模型创建
aws bedrock create-inference-profile \
    --inference-profile-name "my-claude-profile" \
    --description "Custom Claude profile for cost tracking" \
    --model-source '{"copyFrom": "anthropic.claude-3-5-sonnet-20241022-v2:0"}' \
    --tags '[{"key": "Environment", "value": "Development"}]' \
    --region us-west-2

# 基于预置的 inference profile 创建
aws bedrock create-inference-profile \
    --inference-profile-name "my-global-claude-profile" \
    --description "Custom profile based on global Claude Sonnet 4" \
    --model-source '{"copyFrom": "global.anthropic.claude-sonnet-4-20250514-v1:0"}' \
    --region us-west-2

查看 inference profile

# 列出所有 inference profile 
aws bedrock list-inference-profiles --region us-west-2

# 查看特定 inference profile 详情
aws bedrock get-inference-profile \
    --inference-profile-identifier "your-profile-id" \
    --region us-west-2

3. HTTP 调用 Bedrock API Endpoint

基础调用格式

POST https://bedrock-runtime.{region}.amazonaws.com/model/{model-id}/converse
Authorization: Bearer {api-key}
Content-Type: application/json

Inference profile 调用格式

# 系统定义 inference profile  - 使用 Profile ID
POST https://bedrock-runtime.{region}.amazonaws.com/model/{profile-id}/converse

# 用户自定义 inference profile  - 使用完整 ARN
POST https://bedrock-runtime.{region}.amazonaws.com/model/{profile-arn}/converse

Inference profile 支持情况

类型 API Endpoint + API Key AWS CLI 标识符格式
基础模型 ✅ 支持 ✅ 支持 使用模型 ID
预定义 inference profile ✅ 支持 ✅ 支持 使用 Profile ID, 例如: global.anthropic.claude-sonnet-4-20250514-v1:0
用户自定义 inference profile ✅ 支持 ✅ 支持 使用 Profile 完整 ARN, 例如: arn:aws:bedrock:us-west-2:123456789012:application-inference-profile/abc123

⚠️ 注意: 使用用户自定义 inference profile 时需要对 ARN 中的斜杠进行 URL 编码(/%2F), 否则会返回 UnknownOperationException

cURL 示例

# 基础模型调用
curl -X POST "https://bedrock-runtime.us-west-2.amazonaws.com/model/anthropic.claude-3-5-sonnet-20241022-v2:0/converse" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{"messages": [{"role": "user", "content": [{"text": "Hello"}]}]}'

# 系统 inference profile 调用
curl -X POST "https://bedrock-runtime.us-west-2.amazonaws.com/model/global.anthropic.claude-sonnet-4-20250514-v1:0/converse" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{"messages": [{"role": "user", "content": [{"text": "Hello"}]}]}'

# 用户自定义 inference profile 调用 (需要 URL 编码)
curl -X POST "https://bedrock-runtime.us-west-2.amazonaws.com/model/arn:aws:bedrock:us-west-2:123456789012:application-inference-profile%2Fabc123/converse" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{"messages": [{"role": "user", "content": [{"text": "Hello"}]}]}'

实测结果对比

正常响应:

# 成功响应 (正常的 Claude 响应):
```json
{
  "metrics": {
    "latencyMs": 1848
  },
  "output": {
    "message": {
      "content": [
        {
          "text": "Hello! How are you doing today? Is there anything I ou with?"
        }
      ],
      "role": "assistant"
    }
  },
  "stopReason": "end_turn",
  "usage": {
    "inputTokens": 8,
    "outputTokens": 20,
    "totalTokens": 28
  }
}

异常响应:
未对用户自定义 inference profile ARN 做 URL编码会收到以下报错:

# 失败响应 (UnknownOperationException): 
{
  "Output": {
    "__type": "com.amazon.coral.service#UnknownOperationException"
  },
  "Version": "1.0"
}

4. 流式调用 (SSE)

端点格式

POST https://bedrock-runtime.{region}.amazonaws.com/model/{model-id}/converse-stream
Accept: text/event-stream

Python 示例

用户自定义 inference profile 调用:

import requests
import urllib.parse

# 用户自定义 inference profile 需要 URL 编码
profile_arn = "arn:aws:bedrock:us-west-2:123456789012:application-inference-profile/abc123"
encoded_arn = urllib.parse.quote(profile_arn, safe=':.-')  # 编码斜杠,保留冒号、点、短横线

url = f"https://bedrock-runtime.us-west-2.amazonaws.com/model/{encoded_arn}/converse"
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}
payload = {"messages": [{"role": "user", "content": [{"text": "Hello"}]}]}

response = requests.post(url, json=payload, headers=headers)
print(response.json())

Python 流式示例

import requests
import json

url = "https://bedrock-runtime.us-west-2.amazonaws.com/model/global.anthropic.claude-sonnet-4-20250514-v1:0/converse-stream"
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json",
    "Accept": "text/event-stream"
}
payload = {"messages": [{"role": "user", "content": [{"text": "Tell me a story"}]}]}

response = requests.post(url, json=payload, headers=headers, stream=True)
for line in response.iter_lines():
    if line and line.startswith(b'data: '):
        data = line[6:].decode('utf-8')
        if data != '[DONE]':
            try:
                event = json.loads(data)
                if 'contentBlockDelta' in event:
                    print(event['contentBlockDelta']['delta'].get('text', ''), end='')
            except json.JSONDecodeError:
                pass

5. Bedrock API Key 限制

支持的操作

  • Amazon Bedrock Runtime (Converse, InvokeModel)
  • 基础模型和系统定义 inference profile

不支持的操作

  • 用户自定义 inference profile (通过 API Endpoint)
  • Agents for Amazon Bedrock
  • Data Automation for Amazon Bedrock
  • InvokeModelWithBidirectionalStream

6. 参考文档

Previous Post
No Comment
Add Comment
comment url