View a markdown version of this page

快速指南 - Amazon Bedrock

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

快速指南

在幾分鐘內開始使用 Amazon Bedrock。對於新應用程式,我們建議使用 bedrock-runtime端點。下列步驟會逐步引導您使用 Anthropic-native Messages API、OpenAI 相容回應 API聊天完成 API,以及 ConverseInvoke APIs 執行第一個推論請求。如需 APIs的完整清單,請參閱 建置。完成這些步驟後,您可以將推論請求傳送至任何支援的基礎模型。

執行您的第一個推論請求
  1. 註冊 AWS 帳戶

    如果您已有 AWS 帳戶,請略過此步驟。

  2. AWS 管理主控台中開啟 Amazon Bedrock 服務,以產生短期 API 金鑰來驗證您對 Amazon Bedrock請求。

    如需完整程序,請參閱 API 金鑰一節。

    對於生產應用程式,請使用 IAM 角色或暫時登入資料。

  3. 為您計劃使用的 APIs 安裝相關的 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. 選擇模型。Amazon 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

      您應該會看到推論請求的輸出。

若要進一步了解如何使用其他 APIs和端點,請參閱 建置

後續步驟

現在您已執行第一個請求,請探索下列資源,以使用 Amazon Bedrock 建置更多資源: