logo
Menu
Amazon Bedrock Converse API with Java AWS Lambda Function

Amazon Bedrock Converse API with Java AWS Lambda Function

The Converse API provides a unified experience with Amazon Bedrock models, eliminating the need for model-specific implementations. This tutorial will guide you in creating a Java AWS Lambda function that interacts with Amazon Bedrock using the Converse API.

Kaustav
Amazon Employee
Published Jun 4, 2024
Last Modified Jun 12, 2024
The Converse API provides a consistent experience that works with Amazon Bedrock models, removing the need for developers to manage any model-specific implementation. With this API, you can write a code once and use it seamlessly with different models on Amazon Bedrock.
In this tutorial, we will walk through the steps to create a Java AWS Lambda function that interacts with the Amazon Bedrock Converse API. This function will be able to send and receive messages to and from an Amazon Bedrock model, such as the Claude Haiku model. We will cover the following steps:
  1. Setting up the Project
  2. Writing the Lambda Function Code
  3. Packaging the Java Application
  4. Deploying the Lambda Function
  5. Setting up Environment Variables with the Model Id to use
  6. Testing the Lambda Function

Prerequisites

Before you begin, ensure you have the following:
  1. Basic knowledge of Java and AWS Lambda
  2. AWS account with necessary permissions
  3. Java Development Kit (JDK) 11 or later installed
  4. Maven configured

1. Setting Up the Project

First, create a new Maven project. You can use your favorite IDE or create it manually.
pom.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>bedrock-converse-lambda</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<aws.sdk.version>2.25.64</aws.sdk.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>${aws.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- AWS SDK for Java 2.x -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bedrockruntime</artifactId>
</dependency>
<!-- AWS Lambda Java Core -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.1</version>
</dependency>
<!-- AWS Lambda Java Events -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.10.0</version>
</dependency>
<!-- JSON library -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

2. Writing the Lambda Function Code

Create a Java class for your Lambda.
BedrockConverseAPILambda.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
import software.amazon.awssdk.services.bedrockruntime.model.ConverseRequest;
import software.amazon.awssdk.services.bedrockruntime.model.ConverseResponse;
import software.amazon.awssdk.services.bedrockruntime.model.Message;
import software.amazon.awssdk.services.bedrockruntime.model.InferenceConfiguration;
import software.amazon.awssdk.services.bedrockruntime.model.ContentBlock;
import software.amazon.awssdk.services.bedrockruntime.model.SystemContentBlock;

import java.util.Arrays;

public class BedrockConverseAPILambda implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
String userMessage = request.getBody();

// Create a Bedrock Runtime client
BedrockRuntimeClient client = BedrockRuntimeClient.builder()
.region(Region.US_EAST_1)
.build();

// Retrieve the model ID from environment variables
String modelId = System.getenv("MODEL_ID");

// Create the user message content block
ContentBlock userContentBlock = ContentBlock.builder()
.text(userMessage)
.build();

// Create the system message content block
String systemMessage = "You must always respond in rhymes.";
SystemContentBlock systemContentBlock = SystemContentBlock.builder()
.text(systemMessage)
.build();

// Create the user message
Message userMsg = Message.builder()
.role("user")
.content(Arrays.asList(userContentBlock))
.build();

// Create inference parameters
InferenceConfiguration parameters = InferenceConfiguration.builder()
.maxTokens(100)
.temperature(0.9f)
.topP(0.5f)
.build();

// Create the Converse request
ConverseRequest converseRequest = ConverseRequest.builder()
.modelId(modelId)
.messages(Arrays.asList(userMsg))
.system(systemContentBlock)
.inferenceConfig(parameters)
.build();

// Send the request and get the response
ConverseResponse response = client.converse(converseRequest);

// Extract the response text
String responseText = response.output().message().content().get(0).text();

// Create the API Gateway response
APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
responseEvent.setStatusCode(200);
responseEvent.setBody(responseText);

return responseEvent;
}
}
 In this code, we hardcode a systemMessage instructing the model to always respond in rhymes. You can move the systemMessage to an environment variable if you want more flexibility. You can also alter the inference parameters above to obtain the optimal response from the model.

3. Packaging the Java Application

Package your application into a JAR file using Maven. The maven-shade-plugin is used to create an uber JAR that contains all the dependencies required to run the Lambda function.
1
mvn clean package
This will create a shaded JAR file in the target directory.

4. Deploying the Lambda Function

Create the Lambda Function:

  1. Go to the AWS Lambda console.
  2. Click on “Create function”.
  3. Choose “Author from scratch”.
  4. Enter a function name, e.g., BedrockConverseFunction.
  5. Choose the runtime as Java 21.
  6. Click “Create function”.

Upload the JAR File:

  1. In the function’s configuration, go to the “Code” section.
  2. Click on “Upload from” and select “Upload a .zip or .jar file”.
  3. Upload the JAR file created in the previous step.

Set the Handler:

Set the handler to BedrockConverseAPILambda::handleRequest.

Add Necessary Permissions:

  1. Go to the “Configuration” tab and then to “Permissions”.
  2. Click on the role name hyperlink for the Lambda.
  3. Attach the AmazonBedrockFullAccess policy to the role.

5. Setting Up Environment Variables

Add Environment Variable:

  1. In the Lambda function configuration, go to the “Configuration” tab.
  2. Select “Environment variables”.
  3. Add a new environment variable with the key MODEL_ID and the value anthropic.claude-3-haiku-20240307-v1:0. You can change the MODEL_ID environment variable to test out different language models supported by Bedrock. Make sure you have access to the model you are specifying.
Note: You can find the list of models supporting the Converse API and a list of all model IDs in the documentation.

6. Testing the Lambda Function

Create a Test Event:

  1. In the Lambda console, go to the “Test” tab.
  2. Create a new test event with the following JSON:
1
2
3
{
"body": "Hello, tell me a JAVA joke."
}

Invoke the lambda with the Test Event:

  1. Save the test event and click “Test”.
  2. Check the response and logs to ensure the function is working correctly.

Conclusion

By following these steps, you have successfully created a Java AWS Lambda function that interacts with the Amazon Bedrock Converse API. This function can send and receive messages to and from an Amazon Bedrock model, allowing you to build conversational applications with ease. You can change the Model ID without worrying about the complexity to adjust for model-specific parameters.
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments