View a markdown version of this page

快速入门 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

快速入门

几分钟内即可开始使用亚马逊 Bedrock。对于新应用程序,我们推荐使用bedrock-runtime终端节点。以下步骤将引导您使用 Anthropic-native 消息 API 、 OpenAI-compatible 响应 API 聊天完成 API 以及 Converse 和 Invoke API 来运行您的第一个推理请求。有关 API 的完整列表,请参阅构建。完成这些步骤后,您可以向任何支持的基础模型发送推理请求。

运行你的第一个推理请求
  1. 注册一个AWS 账户

    如果您已经有 AWS 账户,请跳过此步骤。

  2. 通过在 AWS 管理控制台中打开亚马逊 Bedrock 服务,生成短期 API 密钥以验证您向亚马逊 Bedrock 发出的请求。

    有关完整过程,请参阅本API 密钥节。

    对于生产应用程序,使用 IAM 角色或临时证书

  3. 为计划使用的 API 安装相关 SDK。必须已经安装了 Python。

    Messages API
    pip install boto3 anthropic
    Responses/Chat Completions API
    pip install boto3 openai
    Invoke/Converse API
    pip install boto3
  4. 设置以下环境变量以使用 API 密钥进行身份验证。

    Messages API
    AWS_BEARER_TOKEN_BEDROCK="<provide your Bedrock API key>"
    Responses/Chat Completions API
    OPENAI_API_KEY="<provide your Bedrock API key>" OPENAI_BASE_URL="https://bedrock-runtime.<your-region>.amazonaws.com/openai/v1"
    Invoke/Converse API
    AWS_BEARER_TOKEN_BEDROCK="<provide your Bedrock API key>"
  5. 选择一个模型并运行您的第一个推理请求。

    1. 选择一个型号。亚马逊 Bedrock 支持 100 多个基础模型。

    2. 使用以下 Python 代码运行您的第一个推理请求。

      Messages API
      from anthropic import AnthropicBedrock client = AnthropicBedrock(aws_region="us-east-1") response = client.messages.create( model="global.anthropic.claude-opus-5", max_tokens=1024, messages=[{"role": "user", "content": "Can you explain the features of Amazon Bedrock?"}] ) print(response)
      Responses API
      from openai import OpenAI client = OpenAI() response = client.responses.create( model="openai.gpt-5.6-sol", input="Can you explain the features of Amazon Bedrock?" ) print(response)
      Chat Completions API
      from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="openai.gpt-5.6-sol", messages=[{"role": "user", "content": "Can you explain the features of Amazon Bedrock?"}] ) print(response)
      Converse API
      import boto3 client = boto3.client('bedrock-runtime', region_name='us-east-1') response = client.converse( modelId='global.anthropic.claude-opus-5', messages=[ { 'role': 'user', 'content': [{'text': 'Can you explain the features of Amazon Bedrock?'}] } ] ) print(response)
      Invoke API
      import json import boto3 client = boto3.client('bedrock-runtime', region_name='us-east-1') response = client.invoke_model( modelId='global.anthropic.claude-opus-5', body=json.dumps({ 'anthropic_version': 'bedrock-2023-05-31', 'messages': [{ 'role': 'user', 'content': 'Can you explain the features of Amazon Bedrock?'}], 'max_tokens': 1024 }) ) print(json.loads(response['body'].read()))
    3. 将该文件保存为 bedrock-first-request.py

    4. 使用以下命令运行代码:

      python3 bedrock-first-request.py

      你应该看到你的推理请求的输出。

要了解有关使用其他 API 和终端节点的更多信息,请参阅构建

后续步骤

现在您已经运行了第一个请求,请浏览以下资源以使用 Amazon Bedrock 构建更多内容: