Code samples
Select a language to see a code sample to call the batch inference API operations.
- Python
-
Create a JSONL file named
abc.jsonl
that contains at least the minimum number of records (see Quotas for Amazon Bedrock). You can use the following contents as your first line and 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: ..." } ] } ] } }
Create an S3 bucket called
batch-input
and upload the file to it. Then create an S3 bucket calledbatch-output
to write your output files to. Run the following code snippet to submit a job and get thejobArn
from the response:import boto3 bedrock = boto3.client(service_name="bedrock") inputDataConfig=({ "s3InputDataConfig": { "s3Uri": "s3://batch-input/abc.jsonl" } }) outputDataConfig=({ "s3OutputDataConfig": { "s3Uri": "s3://batch-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')
Return the
status
of the job.bedrock.get_model_invocation_job(jobIdentifier=jobArn)['status']
List batch inference jobs that
Failed
.bedrock.list_model_invocation_jobs( maxResults=10, statusEquals="Failed", sortOrder="Descending" )
Stop the job that you started.
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") .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); } }
View the results of a job
Prompt engineering concepts