

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.

# Inferenz mithilfe der Chat Completions API
<a name="inference-chat-completions-mantle"></a>

Die OpenAI Chat Completions API generiert Konversationsantworten mithilfe von Amazon Bedrock-Modellen. Sie können die Chat Completions API sowohl auf den Endpunkten als auch auf den Endpunkten verwenden. `bedrock-mantle` `bedrock-runtime` Wir empfehlen, wann immer möglich, den `bedrock-mantle` Endpunkt zu verwenden. Vollständige API-Details finden Sie in der [Dokumentation zu OpenAI Chat-Abschlüssen](https://platform.openai.com/docs/api-reference/chat/create).


| **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 | 

## Chat-Abschlüsse mit dem Bedrock-Mantle-Endpunkt
<a name="inference-chat-completions-mantle-endpoint"></a>

Der `bedrock-mantle` Endpunkt unterstützt die Amazon Bedrock API-Schlüsselauthentifizierung und das OpenAI SDK.

### Verfügbare Modelle auflisten
<a name="inference-chat-completions-mantle-list-models"></a>

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
<a name="inference-chat-completions-mantle-create"></a>

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 die Registerkarte 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 ]

Stellen Sie eine POST-Anfrage an `/v1/chat/completions` mit `stream` folgender Einstellung: `true`

```
# 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
<a name="inference-chat-completions-runtime-endpoint"></a>

Der `bedrock-runtime` Endpunkt unterstützt die AWS SigV4-Authentifizierung und die Amazon Bedrock API-Schlüsselauthentifizierung.

### Verfügbare Modelle auflisten
<a name="inference-chat-completions-runtime-list-models"></a>

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
<a name="inference-chat-completions-runtime-create"></a>

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 den unterstützten Modellen, Regionen und erweiterten Funktionen mit dem `bedrock-runtime` Endpunkt finden Sie unter[API für Chat-Abschlüsse (ältere Referenz)](inference-chat-completions.md).

## So fügen Sie einen Integritätsschutz in eine Chat-Vervollständigung ein
<a name="inference-chat-completions-guardrails"></a>

Um Sicherheitsvorkehrungen in Modelleingaben und -antworten einzubeziehen, wenden Sie einen [Integritätsschutz](guardrails.md) an, indem Sie die folgenden [zusätzlichen Parameter](https://github.com/openai/openai-python#undocumented-request-params) 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:
  + `tagSuffix` (optional) – Fügen Sie dieses Feld für [Eingabemarkierung](guardrails-tagging.md) hinzu.

Weitere Informationen zu diesen Parametern in Integritätsschutz für Amazon Bedrock finden Sie unter [So testen Sie Ihren Integritätsschutz](guardrails-test.md).

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);
```

------