Esempi di codice per l'inferenza in batch - Amazon Bedrock

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Esempi di codice per l'inferenza in batch

Gli esempi di codice in questo capitolo mostrano come creare un processo di inferenza in batch, visualizzare le relative informazioni e interromperlo. Seleziona una lingua per visualizzarne un esempio di codice:

Python

Crea un JSONL file denominato abc.jsonl che contenga almeno il numero minimo di record (vediQuote per Amazon Bedrock). È possibile utilizzare i seguenti contenuti come prima riga e input:

{ "recordId": "CALL0000001", "modelInput": { "anthropic_version": "bedrock-2023-05-31", "max_tokens": 1024, "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Summarize the following call transcript: ..." } ] } ] } }

Crea un bucket S3 chiamato amzn-s3-demo-bucket-input e carica il file su di esso. Quindi crea un bucket S3 chiamato amzn-s3-demo-bucket-output su cui scrivere i file di output. Esegui il seguente frammento di codice per inviare un lavoro e ottenere il jobArn dalla risposta:

import boto3 bedrock = boto3.client(service_name="bedrock") inputDataConfig=({ "s3InputDataConfig": { "s3Uri": "s3://amzn-s3-demo-bucket-input/abc.jsonl" } }) outputDataConfig=({ "s3OutputDataConfig": { "s3Uri": "s3://amzn-s3-demo-bucket-output/" } }) response=bedrock.create_model_invocation_job( roleArn="arn:aws:iam::123456789012:role/MyBatchInferenceRole", modelId="anthropic.claude-3-haiku-20240307-v1:0", jobName="my-batch-job", inputDataConfig=inputDataConfig, outputDataConfig=outputDataConfig ) jobArn = response.get('jobArn')

Restituisci lo status del processo.

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

Elenca i lavori di inferenza in batch che Failed.

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

Arresta il processo che hai iniziato.

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://batch-input/abc.jsonl") .withS3InputFormat("JSONL"); final InvocationJobInputDataConfig inputDataConfig = new InvocationJobInputDataConfig() .withS3InputDataConfig(invocationJobS3InputDataConfig); final InvocationJobS3OutputDataConfig invocationJobS3OutputDataConfig = new InvocationJobS3OutputDataConfig() .withS3Uri("s3://batch-output/"); final InvocationJobOutputDataConfig invocationJobOutputDataConfig = new InvocationJobOutputDataConfig() .withS3OutputDataConfig(invocationJobS3OutputDataConfig); final CreateModelInvocationJobRequest createModelInvocationJobRequest = new CreateModelInvocationJobRequest() .withModelId("anthropic.claude-3-haiku-20240307-v1:0") .withJobName("unique-job-name") .withRoleArn("arn:aws:iam::123456789:role/bedrock-role") .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); } }