Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
API für Chat-Abschlüsse auf dem Bedrock-Mantle-Endpunkt
Die OpenAI Chat Completions API generiert Konversationsantworten mithilfe von Amazon Bedrock-Modellen. Für neue Anwendungen empfehlen wir den bedrock-runtime Endpunkt (siehe). API für Chat-Abschlüsse (ältere Referenz) Diese Seite dokumentiert die API auf dem bedrock-mantle Endpunkt, die ebenfalls vollständig unterstützt wird. Vollständige API-Details finden Sie in der Dokumentation zu OpenAI Chat-Abschlüssen.
| Endpunkt |
Basis-URL |
Authentifizierung |
bedrock-mantle (empfohlen) |
https://bedrock-mantle.{region}.api.aws/v1/chat/completions |
Amazon Bedrock API-Schlüssel oder Anmeldeinformationen AWS |
bedrock-runtime |
https://bedrock-runtime.{region}.amazonaws.com/v1/chat/completions |
AWS Anmeldeinformationen (SigV4) oder Amazon Bedrock API-Schlüssel |
Jeder Endpunkt hat seine eigenen Token-Kontingente pro Modell. Einzelheiten zu den Kontingenten, die für den Verkehr auf den einzelnen Endpunkten gelten, finden Sie unter Kontingente für den Endpunkt „Bedrock-Mantle“ undKontingente für den Bedrock-Runtime-Endpunkt.
Abschluss des Chats mit dem Endpunkt Bedrock-Mantle
Der bedrock-mantle Endpunkt unterstützt die Amazon Bedrock API-Schlüsselauthentifizierung und das SDK. OpenAI
Listet die verfügbaren Modelle auf
Um die auf dem bedrock-mantle Endpunkt verfügbaren Modelle aufzulisten, wählen Sie die Registerkarte für Ihre bevorzugte Methode und folgen Sie dann den Schritten:
- OpenAI SDK (Python)
-
# List all available models using the OpenAI SDK
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
from openai import OpenAI
client = OpenAI()
models = client.models.list()
for model in models.data:
print(model.id)
- HTTP request
-
# List all available models
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
curl -X GET $OPENAI_BASE_URL/models \
-H "Authorization: Bearer $OPENAI_API_KEY"
Erstellen einer Chat-Vervollständigung
Wählen Sie die Registerkarte für Ihre bevorzugte Methode aus und befolgen Sie dann die Schritte:
- OpenAI SDK (Python)
-
Konfigurieren Sie den OpenAI Client mithilfe von Umgebungsvariablen:
# Create a chat completion using the OpenAI SDK
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="openai.gpt-oss-120b",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(completion.choices[0].message)
- HTTP request
-
Stellen Sie eine POST-Anfrage an/v1/chat/completions:
# Create a chat completion
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
curl -X POST $OPENAI_BASE_URL/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "openai.gpt-oss-120b",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
}'
Streaming
Um schrittweise Antworten zu erhalten, wählen Sie den Tab für Ihre bevorzugte Methode und folgen Sie dann den Schritten:
- OpenAI SDK (Python)
-
# Stream chat completion responses incrementally using the OpenAI SDK
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
from openai import OpenAI
client = OpenAI()
stream = client.chat.completions.create(
model="openai.gpt-oss-120b",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
- HTTP request
-
Senden Sie eine POST-Anfrage an /v1/chat/completions mit der stream Einstellung auftrue:
# Stream chat completion responses incrementally
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
curl -X POST $OPENAI_BASE_URL/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "openai.gpt-oss-120b",
"messages": [
{"role": "user", "content": "Tell me a story"}
],
"stream": true
}'
Chat-Abschlüsse mit dem Bedrock-Runtime-Endpunkt
Der bedrock-runtime Endpunkt unterstützt die AWS SigV4-Authentifizierung und die Amazon Bedrock API-Schlüsselauthentifizierung.
Liste der verfügbaren Modelle
Um die auf dem bedrock-runtime Endpunkt verfügbaren Modelle aufzulisten, wählen Sie die Registerkarte für Ihre bevorzugte Methode und folgen Sie dann den Schritten:
- OpenAI SDK (Python)
-
from openai import OpenAI
import os
client = OpenAI(
base_url="https://bedrock-runtime.us-east-1.amazonaws.com/v1",
api_key=os.environ.get("AWS_BEARER_TOKEN_BEDROCK")
)
models = client.models.list()
for model in models.data:
print(model.id)
- HTTP request
-
curl -X GET "https://bedrock-runtime.us-east-1.amazonaws.com/v1/models" \
-H "Authorization: Bearer $AWS_BEARER_TOKEN_BEDROCK"
Erstellen einer Chat-Vervollständigung
Wählen Sie die Registerkarte für Ihre bevorzugte Methode aus und befolgen Sie dann die Schritte:
- OpenAI SDK (Python)
-
Konfigurieren Sie den OpenAI Client so, dass er auf den bedrock-runtime Endpunkt zeigt:
from openai import OpenAI
import os
client = OpenAI(
base_url="https://bedrock-runtime.us-east-1.amazonaws.com/v1",
api_key=os.environ.get("AWS_BEARER_TOKEN_BEDROCK")
)
response = client.chat.completions.create(
model="us.anthropic.claude-sonnet-4-6",
messages=[{"role": "user", "content": "Hello"}]
)
print(response.choices[0].message.content)
- HTTP request (API key)
-
curl -X POST "https://bedrock-runtime.us-east-1.amazonaws.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AWS_BEARER_TOKEN_BEDROCK" \
-d '{
"model": "us.anthropic.claude-sonnet-4-6",
"messages": [{"role": "user", "content": "Hello"}]
}'
- HTTP request (SigV4)
-
curl -X POST "https://bedrock-runtime.us-east-1.amazonaws.com/v1/chat/completions" \
-H "Content-Type: application/json" \
--aws-sigv4 "aws:amz:us-east-1:bedrock" \
--user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
-d '{
"model": "us.anthropic.claude-sonnet-4-6",
"messages": [{"role": "user", "content": "Hello"}]
}'
Weitere Informationen zu unterstützten Modellen, Regionen und erweiterten Funktionen des bedrock-runtime Endpunkts finden Sie unterAPI für Chat-Abschlüsse (ältere Referenz).
So fügen Sie einen Integritätsschutz in eine Chat-Vervollständigung ein
Um Sicherheitsvorkehrungen in Modelleingaben und -antworten einzubeziehen, wenden Sie einen Integritätsschutz an, indem Sie die folgenden zusätzlichen Parameter als Felder im Anforderungstext aufnehmen:
-
extra_headers – Ordnet einem Objekt mit den folgenden Felder zu, die zusätzliche Header in der Anfrage angeben:
-
X-Amzn-Bedrock-GuardrailIdentifier (erforderlich) – Die ID des Integritätsschutzes.
-
X-Amzn-Bedrock-GuardrailVersion (erforderlich) – Die Version des Integritätsschutzes.
-
X-Amzn-Bedrock-Trace (optional) – Ob die Ablaufverfolgung für den Integritätsschutz aktiviert werden soll oder nicht.
-
extra_body – Ordnet einem Objekt zu. In dieses Objekt können Sie das amazon-bedrock-guardrailConfig-Feld aufnehmen, das einem Objekt mit den folgenden Feldern zugeordnet ist:
Weitere Informationen zu diesen Parametern in Integritätsschutz für Amazon Bedrock finden Sie unter So testen Sie Ihren Integritätsschutz.
Wenn Sie Beispiele für die Verwendung des Integritätsschutzes mit OpenAI-Chat-Vervollständigungen sehen möchten, wählen Sie die Registerkarte für Ihre bevorzugte Methode aus und führen Sie dann die folgenden Schritte aus:
- 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);