Codebeispiele - Amazon Bedrock

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.

Codebeispiele

Anmerkung

Bei der Batch-Inferenz handelt es sich um eine Vorversion, die Änderungen unterliegt. Batch-Inferenz ist derzeit nur über die API verfügbar. Greifen Sie über die folgenden SDKs auf Batch-APIs zu.

Wir empfehlen, dass Sie eine virtuelle Umgebung für die Verwendung des SDK erstellen. Da Batch-Inferenz-APIs in den neuesten SDKs nicht verfügbar sind, empfehlen wir, dass Sie die neueste Version des SDK aus der virtuellen Umgebung deinstallieren, bevor Sie die Version mit den Batch-Inferenz-APIs installieren. Ein Beispiel mit Anleitungen finden Sie unter. Codebeispiele

Wählen Sie eine Sprache aus, um ein Codebeispiel für den Aufruf der Batch-Inferenz-API-Operationen anzuzeigen.

Python

Nachdem Sie das Python-SDK und die CLI-Dateien heruntergeladen haben, die die Batch-Inferenz-API-Operationen enthalten, navigieren Sie zu dem Ordner, der die Dateien enthält, und führen Sie ihn ls in einem Terminal aus. Sie sollten mindestens die folgenden 2 Dateien sehen.

botocore-1.32.4-py3-none-any.whl boto3-1.29.4-py3-none-any.whl

Erstellen und aktivieren Sie eine virtuelle Umgebung für die Batch-Inferenz-APIs, indem Sie die folgenden Befehle in einem Terminal ausführen. Sie können bedrock-batch durch einen Namen Ihrer Wahl für die Umgebung ersetzen.

python3 -m venv bedrock-batch source bedrock-batch/bin/activate

Um sicherzustellen, dass keine Artefakte aus neueren Versionen von boto3 und vorhanden sindbotocore, deinstallieren Sie alle vorhandenen Versionen, indem Sie die folgenden Befehle in einem Terminal ausführen.

python3 -m pip uninstall botocore python3 -m pip uninstall boto3

Installieren Sie das Python-SDK, das die Steuerebene-APIs von Amazon Bedrock enthält, indem Sie die folgenden Befehle in einem Terminal ausführen.

python3 -m pip install botocore-1.32.4-py3-none-any.whl python3 -m pip install boto3-1.29.4-py3-none-any.whl

Führen Sie den gesamten folgenden Code in der von Ihnen erstellten virtuellen Umgebung aus.

Erstellen Sie einen Batch-Inferenzauftrag mit einer Datei namens abc.jsonl, die Sie auf S3 hochgeladen haben. Schreiben Sie die Ausgabe in einen Bucket unter s3://output-bucket/output/. Rufen Sie den jobArn aus der Antwort ab.

import boto3 bedrock = boto3.client(service_name="bedrock") inputDataConfig=({ "s3InputDataConfig": { "s3Uri": "s3://input-bucket/input/abc.jsonl" } }) outputDataConfig=({ "s3OutputDataConfig": { "s3Uri": "s3://output-bucket/output/" } }) response=bedrock.create_model_invocation_job( roleArn="arn:aws:iam::123456789012:role/MyBatchInferenceRole", modelId="amazon.titan-text-express-v1", jobName="my-batch-job", inputDataConfig=inputDataConfig, outputDataConfig=outputDataConfig ) jobArn = response.get('jobArn')

Gibt den Namen status des Auftrags zurück.

bedrock.get_model_invocation_job(jobIdentifier=jobArn)['status']

Listet Batch-Inferenzaufträge auf, die fehlgeschlagen sind.

bedrock.list_model_invocation_jobs( maxResults=10, statusEquals="Failed", sortOrder="Descending" )

Beenden Sie den Auftrag, den Sie gestartet haben.

bedrock.stop_model_invocation_job(jobIdentifier=jobArn)
Java
package com.amazon.aws.sample.bedrock.inference; import com.amazonaws.services.bedrock.AmazonBedrockAsync; import com.amazonaws.services.bedrock.AmazonBedrockAsyncClientBuilder; import com.amazonaws.services.bedrock.model.CreateModelInvocationJobRequest; import com.amazonaws.services.bedrock.model.CreateModelInvocationJobResult; import com.amazonaws.services.bedrock.model.GetModelInvocationJobRequest; import com.amazonaws.services.bedrock.model.GetModelInvocationJobResult; import com.amazonaws.services.bedrock.model.InvocationJobInputDataConfig; import com.amazonaws.services.bedrock.model.InvocationJobOutputDataConfig; import com.amazonaws.services.bedrock.model.InvocationJobS3InputDataConfig; import com.amazonaws.services.bedrock.model.InvocationJobS3OutputDataConfig; import com.amazonaws.services.bedrock.model.ListModelInvocationJobsRequest; import com.amazonaws.services.bedrock.model.ListModelInvocationJobsResult; import com.amazonaws.services.bedrock.model.StopModelInvocationJobRequest; import com.amazonaws.services.bedrock.model.StopModelInvocationJobResult; public class BedrockAsyncInference { private final AmazonBedrockAsync amazonBedrockAsyncClient = AmazonBedrockAsyncClientBuilder.defaultClient(); public void createModelInvokeJobSampleCode() { final InvocationJobS3InputDataConfig invocationJobS3InputDataConfig = new InvocationJobS3InputDataConfig() .withS3Uri("s3://Input-bucket-name/input/abc.jsonl") .withS3InputFormat("JSONL"); final InvocationJobInputDataConfig inputDataConfig = new InvocationJobInputDataConfig() .withS3InputDataConfig(invocationJobS3InputDataConfig); final InvocationJobS3OutputDataConfig invocationJobS3OutputDataConfig = new InvocationJobS3OutputDataConfig() .withS3Uri("s3://output-bucket-name/output/"); final InvocationJobOutputDataConfig invocationJobOutputDataConfig = new InvocationJobOutputDataConfig() .withS3OutputDataConfig(invocationJobS3OutputDataConfig); final CreateModelInvocationJobRequest createModelInvocationJobRequest = new CreateModelInvocationJobRequest() .withModelId("anthropic.claude-v2") .withJobName("unique-job-name") .withClientRequestToken("Client-token") .withInputDataConfig(inputDataConfig) .withOutputDataConfig(invocationJobOutputDataConfig); final CreateModelInvocationJobResult createModelInvocationJobResult = amazonBedrockAsyncClient .createModelInvocationJob(createModelInvocationJobRequest); System.out.println(createModelInvocationJobResult.getJobArn()); } public void getModelInvokeJobSampleCode() { final GetModelInvocationJobRequest getModelInvocationJobRequest = new GetModelInvocationJobRequest() .withJobIdentifier("jobArn"); final GetModelInvocationJobResult getModelInvocationJobResult = amazonBedrockAsyncClient .getModelInvocationJob(getModelInvocationJobRequest); } public void listModelInvokeJobSampleCode() { final ListModelInvocationJobsRequest listModelInvocationJobsRequest = new ListModelInvocationJobsRequest() .withMaxResults(10) .withNameContains("matchin-string"); final ListModelInvocationJobsResult listModelInvocationJobsResult = amazonBedrockAsyncClient .listModelInvocationJobs(listModelInvocationJobsRequest); } public void stopModelInvokeJobSampleCode() { final StopModelInvocationJobRequest stopModelInvocationJobRequest = new StopModelInvocationJobRequest() .withJobIdentifier("jobArn"); final StopModelInvocationJobResult stopModelInvocationJobResult = amazonBedrockAsyncClient .stopModelInvocationJob(stopModelInvocationJobRequest); } }