

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

# 快速入门
<a name="getting-started"></a>

在本节中，我们将在几分钟内向您展示如何开始使用 Amazon Bedrock。我们将使用兼容 OpenAI 的 APIs：[响应 API 和聊天完成 API](bedrock-mantle.md)[，以及 [In](inference-invoke.md) voke and [Converse API](conversation-inference.md) 来向你展示如何运行推理请求](inference-chat-completions.md)。[构建](build.md)有关完整列表，请参阅 APIs。

**第 1 步-AWS 账户：**如果您已经拥有 AWS 账户，请跳过此步骤进入步骤 2。如果您不熟悉 AWS，请注册 [AWS 账户](https://portal.aws.amazon.com/billing/signup)并按照说明进行操作。

**第 2 步-API 密钥：**拥有 AWS 账户后，您可以创建一个长期 API 密钥来验证您向 Amazon Bedrock 提出的请求。为此，请访问 [AWS 控制台中的 Amazon Bedrock 服务](https://console.aws.amazon.com/bedrock/home#/api-keys/long-term/create)并生成长期密钥。有关更多信息，请参阅 B [uild](build.md) 一章中的 [API 密钥](api-keys.md)部分。

**第 3 步-获取 SDK：**要使用本入门指南，必须已安装 Python。然后根据 APIs 您使用的软件安装相关软件。

------
#### [ Responses/Chat Completions API ]

```
pip install boto3 openai
```

------
#### [ Invoke/Converse API ]

```
pip install boto3
```

------

**步骤 4-设置环境变量：**将您的环境配置为使用 API 密钥进行身份验证。

------
#### [ Responses/Chat Completions API ]

```
OPENAI_API_KEY="<provide your Bedrock API key>"
OPENAI_BASE_URL="https://bedrock-mantle.<your-region>.api.aws/v1"
```

------
#### [ Invoke/Converse API ]

```
AWS_BEARER_TOKEN_BEDROCK="<provide your Bedrock API key>"
```

------

**第 5 步——运行你的第一个推理请求：**Amazon Bedrock 支持 [100 多个](models.md)基础模型。选择一个模型，然后使用以下 Python 代码运行您的第一个推理请求。将文件另存为 `bedrock-first-request.py`

------
#### [ Responses API ]

```
from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="openai.gpt-oss-120b",
    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-oss-120b",
    messages=[{"role": "user", "content": "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='anthropic.claude-opus-4-6-v1',
    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()))
```

------
#### [ Converse API ]

```
import boto3

client = boto3.client('bedrock-runtime', region_name='us-east-1')
response = client.converse(
    modelId='anthropic.claude-opus-4-6-v1',
    messages=[
        {
            'role': 'user',
            'content': [{'text': 'Can you explain the features of Amazon Bedrock?'}]
        }
    ]
)
print(response)
```

------

使用以下命令使用 Python 执行代码：

```
python3 bedrock-first-request.py
```

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

要了解有关使用其他 APIs 和端点的更多信息，请参阅[构建](build.md)。