OpenAI モデル - Amazon Bedrock

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

OpenAI モデル

OpenAI には、次のオープンウェイトモデルが用意されています。

  • gpt-oss-20b – 低レイテンシーとローカルまたは特殊なユースケース向けに最適化された小さなモデル。

  • gpt-oss-120b – 本番稼働用、汎用、または理由の多いユースケース向けに最適化された大規模なモデル。

次の表は、モデルに関する情報をまとめたものです。

情報 gpt-oss-20b gpt-oss-120b
リリース日 2025 年 8 月 5 日 2025 年 8 月 5 日
モデル ID openai.gpt-oss-20b-1:0 openai.gpt-oss-120b-1:0
製品 ID 該当なし 該当なし
サポートされている入力モダリティ テキスト テキスト
サポートされている出力モダリティ テキスト テキスト
コンテキストウィンドウ 128,000 128,000

OpenAI モデルは以下の機能をサポートしています。

OpenAI リクエスト本文

リクエスト本文のパラメータとその説明については、 OpenAIドキュメントの「チャット完了の作成」を参照してください。

次の方法でリクエスト本文フィールドを使用します。

  • InvokeModel または OpenAI Chat Completions リクエストで、リクエスト本文のフィールドを含めます。

  • Converse リクエストで、次の操作を行います。

    • messages次のようにマッピングします。

      • ロールが であるメッセージごとにdevelopersystem配列に content SystemContentBlock を追加します。

      • ロールが userまたは であるメッセージごとにassistantcontentフィールドの ContentBlock contentに を追加し、 messages 配列roleMessageroleフィールドに を指定します。

    • 次のフィールドの値を inferenceConfig オブジェクトの対応するフィールドにマッピングします。

      OpenAI フィールド Converse フィールド
      max_completion_tokens maxTokens
      停止 stopSequences
      温度 temperature
      top_p topP
    • additionalModelRequestFields オブジェクトに他のフィールドを含めます。

リクエスト本文を作成する際の考慮事項
  • OpenAI モデルは、テキスト入力とテキスト出力のみをサポートします。

  • model フィールドの値は、 ヘッダーの値と一致する必要があります。このフィールドを省略すると、 ヘッダーと同じ値が自動的に入力されます。

  • stream フィールドの値は、使用する API オペレーションと一致する必要があります。このフィールドを省略すると、自動的に正しい値が入力されます。

    • InvokeModel を使用する場合、stream値は である必要がありますfalse

OpenAI レスポンス本文

OpenAI モデルのレスポンス本文は、 によって返されるチャット完了オブジェクトに準拠していますOpenAI。レスポンスフィールドの詳細については、 OpenAIドキュメントの「チャット完了オブジェクト」を参照してください。

注記

を使用する場合InvokeModel、レスポンスのテキストコンテンツの前に<reasoning>、タグで囲まれたモデルの推論が先行します。

OpenAI モデルの使用例

このセクションでは、OpenAIモデルの使用方法の例をいくつか示します。

これらの例を試す前に、前提条件を満たしていることを確認してください。

  • 認証 – AWS 認証情報または Amazon Bedrock API キーを使用して認証できます。

    AWS 認証情報を設定するか、Amazon Bedrock API キーを生成してリクエストを認証します。

    注記

    Chat OpenAI completions API を使用する場合は、Amazon Bedrock API キーでのみ認証できます。

  • エンドポイント – Amazon Bedrock ランタイムエンドポイントとクォータで使用する AWS リージョンに対応するエンドポイントを見つけます。 https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt AWS SDK を使用する場合は、クライアントの設定時にエンドポイント全体ではなく、リージョンコードを指定するだけで済みます。この例で使用されているモデルでサポートされているリージョンに関連付けられたエンドポイントを使用する必要があります。

  • モデルアクセス – OpenAIモデルへのアクセスをリクエストします。詳細については、「Add or remove access to Amazon Bedrock foundation models」を参照してください。

  • (例で SDK を使用している場合) SDK をインストールする – インストール後、デフォルトの認証情報とデフォルトの AWS リージョンを設定します。デフォルトの認証情報またはリージョンを設定しない場合は、関連するコード例で明示的に指定する必要があります。標準化された認証情報プロバイダーの詳細については、AWS SDKs」を参照してください。

    注記

    OpenAI SDK を使用する場合は、Amazon Bedrock API キーでのみ認証でき、Amazon Bedrock エンドポイントを明示的に設定する必要があります。

表示する例のセクションを展開します。

Create chat completion API OpenAI の使用例を確認するには、任意の方法のタブを選択し、ステップに従います。

OpenAI SDK (Python)

次の Python スクリプトは、Python SDK を使用してチャット完了の作成 API OpenAI を呼び出します。

from openai import OpenAI client = OpenAI( base_url="https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1", api_key="$AWS_BEARER_TOKEN_BEDROCK" ) completion = client.chat.completions.create( model="openai.gpt-oss-20b-1:0", messages=[ { "role": "developer", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ] ) print(completion.choices[0].message)
HTTP request using curl

ターミナルで次のコマンドを実行して、curl を使用してチャット完了の作成 API を呼び出すことができます。

curl -X POST https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $AWS_BEARER_TOKEN_BEDROCK" \ -d '{ "model": "openai.gpt-oss-20b-1:0", "messages": [ { "role": "developer", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ] }'

任意の方法のタブを選択し、ステップに従います。

Python
import boto3 import json # Initialize the Bedrock Runtime client client = boto3.client('bedrock-runtime') # Model ID model_id = 'openai.gpt-oss-20b-1:0' # Create the request body native_request = { "model": model_id, # You can omit this field "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "assistant", "content": "Hello! How can I help you today?" }, { "role": "user", "content": "What is the weather like today?" } ], "max_completion_tokens": 150, "temperature": 0.7, "top_p": 0.9, "stream": False # You can omit this field } # Make the InvokeModel request response = client.invoke_model( modelId=model_id, body=json.dumps(native_request) ) # Parse and print the message for each choice in the chat completion response_body = json.loads(response['body'].read().decode('utf-8')) for choice in response_body['choices']: print(choice['message']['content'])

統合 Converse API を使用する場合は、チャット完了OpenAIの作成フィールドを Converse リクエスト本文の対応するフィールドにマッピングする必要があります。

たとえば、次のチャット完了リクエスト本文と対応する Converse リクエスト本文を比較します。

Create chat completion request body
{ "model": "openai.gpt-oss-20b-1:0", "messages": [ { "role": "developer", "content": "You are a helpful assistant." }, { "role": "assistant", "content": "Hello! How can I help you today?" }, { "role": "user", "content": "What is the weather like today?" } ], "max_completion_tokens": 150, "temperature": 0.7 }
Converse request body
{ "messages": [ { "role": "user", "content": [ { "text": "Hello! How can I help you today?" } ] }, { "role": "user", "content": [ { "text": "What is the weather like today?" } ] } ], "system": [ { "text": "You are a helpful assistant." } ], "inferenceConfig": { "maxTokens": 150, "temperature": 0.7 } }

任意の方法のタブを選択し、ステップに従います。

Python
# Use the Conversation API to send a text message to Anthropic Claude. import boto3 from botocore.exceptions import ClientError # Initialize the Bedrock Runtime client client = boto3.client("bedrock-runtime") # Set the model ID model_id = "openai.gpt-oss-20b-1:0" # Set up messages and system message messages = [ { "role": "assistant", "content": [ { "text": "Hello! How can I help you today?" } ] }, { "role": "user", "content": [ { "text": "What is the weather like today?" } ] } ] system = [ { "text": "You are a helpful assistant." } ] try: # Send the message to the model, using a basic inference configuration. response = client.converse( modelId=model_id, messages=messages, system=system, inferenceConfig={ "maxTokens": 150, "temperature": 0.7, "topP": 0.9 }, ) # Extract and print the response text. for content_block in response["output"]["message"]["content"]: print(content_block) except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1)

モデル呼び出しを実行するときは、ガードレール ID、バージョン、およびモデル呼び出しリクエストのヘッダーでガードレールトレースを有効にするかどうかを指定して、ガードレールを適用します。

任意の方法のタブを選択し、ステップに従います。

Python
import boto3 from botocore.exceptions import ClientError import json # Initiate the Amazon Bedrock Runtime client bedrock_runtime = boto3.client("bedrock-runtime") # Model ID model_id = "openai.gpt-oss-20b-1:0" # Replace with actual values from your guardrail guardrail_id = "GR12345" guardrail_version = "DRAFT" # Create the request body native_request = { "model": model_id, # You can omit this field "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "assistant", "content": "Hello! How can I help you today?" }, { "role": "user", "content": "What is the weather like today?" } ], "max_completion_tokens": 150, "temperature": 0.7, "top_p": 0.9, "stream": False # You can omit this field } try: response = bedrock_runtime.invoke_model( modelId=model_id, body=json.dumps(native_request), guardrailIdentifier=guardrail_id, guardrailVersion=guardrail_version, trace='ENABLED', ) response_body = json.loads(response.get('body').read()) print("Received response from InvokeModel API (Request Id: {})".format(response['ResponseMetadata']['RequestId'])) print(json.dumps(response_body, indent=2)) except ClientError as err: print("RequestId = " + err.response['ResponseMetadata']['RequestId']) raise err

OpenAI チャットの完了時にガードレールを使用する例を確認するには、任意の方法のタブを選択し、ステップに従います。

OpenAI SDK (Python)
import openai from openai import OpenAIError # Endpoint for Amazon Bedrock Runtime bedrock_endpoint = "https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1" # Model ID model_id = "openai.gpt-oss-20b-1:0" # Replace with actual values bedrock_api_key = "$AWS_BEARER_TOKEN_BEDROCK" guardrail_id = "GR12345" guardrail_version = "DRAFT" client = openai.OpenAI( api_key=bedrock_api_key, base_url=bedrock_endpoint, ) try: response = client.chat.completions.create( model=model_id, # Specify guardrail information in the header extra_headers={ "X-Amzn-Bedrock-GuardrailIdentifier": guardrail_id, "X-Amzn-Bedrock-GuardrailVersion": guardrail_version, "X-Amzn-Bedrock-Trace": "ENABLED", }, # Additional guardrail information can be specified in the body extra_body={ "amazon-bedrock-guardrailConfig": { "tagSuffix": "xyz" # Used for input tagging } }, messages=[ { "role": "system", "content": "You are a helpful assistant." }, { "role": "assistant", "content": "Hello! How can I help you today?" }, { "role": "user", "content": "What is the weather like today?" } ] ) request_id = response._request_id print(f"Request ID: {request_id}") print(response) except OpenAIError as e: print(f"An error occurred: {e}") if hasattr(e, 'response') and e.response is not None: request_id = e.response.headers.get("x-request-id") print(f"Request ID: {request_id}")
OpenAI SDK (Java)
import com.openai.client.OpenAIClient; import com.openai.client.okhttp.OpenAIOkHttpClient; import com.openai.core.http.HttpResponseFor; import com.openai.models.chat.completions.ChatCompletion; import com.openai.models.chat.completions.ChatCompletionCreateParams; // Endpoint for Amazon Bedrock Runtime String bedrockEndpoint = "http://bedrock-runtime.us-west-2.amazonaws.com/openai/v1" // Model ID String modelId = "openai.gpt-oss-20b-1:0" // Replace with actual values String bedrockApiKey = "$AWS_BEARER_TOKEN_BEDROCK" String guardrailId = "GR12345" String guardrailVersion = "DRAFT" OpenAIClient client = OpenAIOkHttpClient.builder() .apiKey(bedrockApiKey) .baseUrl(bedrockEndpoint) .build() ChatCompletionCreateParams request = ChatCompletionCreateParams.builder() .addUserMessage("What is the temperature in Seattle?") .model(modelId) // Specify additional headers for the guardrail .putAdditionalHeader("X-Amzn-Bedrock-GuardrailIdentifier", guardrailId) .putAdditionalHeader("X-Amzn-Bedrock-GuardrailVersion", guardrailVersion) // Specify additional body parameters for the guardrail .putAdditionalBodyProperty( "amazon-bedrock-guardrailConfig", JsonValue.from(Map.of("tagSuffix", JsonValue.of("xyz"))) // Allows input tagging ) .build(); HttpResponseFor<ChatCompletion> rawChatCompletionResponse = client.chat().completions().withRawResponse().create(request); final ChatCompletion chatCompletion = rawChatCompletionResponse.parse(); System.out.println(chatCompletion);

バッチ推論を使用すると、複数のプロンプトでモデル推論を非同期的に実行できます。OpenAI モデルでバッチ推論を実行するには、以下を実行します。

  1. JSONL ファイルを作成し、少なくとも最小数の JSON オブジェクトを新しい行で区切って入力します。各modelInputオブジェクトは、OpenAIチャット完了リクエストの作成本文の形式に従う必要があります。のリクエスト本文を含む JSONL ファイルの最初の 2 行の例を次に示しますOpenAI。

    { "recordId": "RECORD1", "modelInput": { "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Can you generate a question with a factual answer?" } ], "max_completion_tokens": 1000 } } { "recordId": "RECORD2", "modelInput": { "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "What is the weather like today?" } ], "max_completion_tokens": 1000 } } ...
    注記

    バッチ推論サービスがヘッダーを省略するとそれを挿入するため、 modelフィールドはオプションです。

    「」で説明されているように、JSONL ファイルがバッチ推論クォータに準拠していることを確認しますバッチ推論データをフォーマットしてアップロードする

  2. Amazon S3 バケットにファイルをアップロードします。

  3. フィールドで指定された前のステップの S3 バケットと inputDataConfigフィールドで指定されたOpenAIモデルを使用して、Amazon Bedrock コントロールプレーンエンドポイントCreateModelInvocationJob リクエストを送信しますmodelId

エンドツーエンドのend-to-end「」を参照してくださいバッチ推論のコード例。をOpenAIモデルに適した設定に置き換えます。