Jurassic-2 Chat

AI21 Labs Jurassic on Amazon Bedrock Jurassic is AI21 Labs’ family of reliable FMs for the enterprise, powering sophisticated language generation tasks – such as question answering, text generation, search, and summarization – across thousands of live applications.

Prerequisites

Refer to the Spring AI documentation on Amazon Bedrock for setting up API access.

Add Repositories and BOM

Spring AI artifacts are published in Spring Milestone and Snapshot repositories. Refer to the Repositories section to add these repositories to your build system.

To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the Dependency Management section to add the Spring AI BOM to your build system.

Auto-configuration

Add the spring-ai-bedrock-ai-spring-boot-starter dependency to your project’s Maven pom.xml file:

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
</dependency>

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock-ai-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Enable Jurassic-2

By default the Bedrock Jurassic-2 model is disabled. To enable it set the spring.ai.bedrock.jurassic2.chat.enabled property to true. Exporting environment variable is one way to set this configuration property:

export SPRING_AI_BEDROCK_JURASSIC2_CHAT_ENABLED=true

Chat Properties

The prefix spring.ai.bedrock.aws is the property prefix to configure the connection to AWS Bedrock.

Property Description Default

spring.ai.bedrock.aws.region

AWS region to use.

us-east-1

spring.ai.bedrock.aws.timeout

AWS timeout to use.

5m

spring.ai.bedrock.aws.access-key

AWS access key.

-

spring.ai.bedrock.aws.secret-key

AWS secret key.

-

The prefix spring.ai.bedrock.jurassic2.chat is the property prefix that configures the chat model implementation for Jurassic-2.

Property Description Default

spring.ai.bedrock.jurassic2.chat.enabled

Enable or disable support for Jurassic-2

false

spring.ai.bedrock.jurassic2.chat.model

The model id to use (See Below)

ai21.j2-mid-v1

spring.ai.bedrock.jurassic2.chat.options.temperature

Controls the randomness of the output. Values can range over [0.0,1.0], inclusive. A value closer to 1.0 will produce responses that are more varied, while a value closer to 0.0 will typically result in less surprising responses from the model. This value specifies default to be used by the backend while making the call to the model.

0.7

spring.ai.bedrock.jurassic2.chat.options.top-p

The maximum cumulative probability of tokens to consider when sampling. The model uses combined Top-k and nucleus sampling. Nucleus sampling considers the smallest set of tokens whose probability sum is at least topP.

AWS Bedrock default

spring.ai.bedrock.jurassic2.chat.options.max-tokens

Specify the maximum number of tokens to use in the generated response. The model truncates the response once the generated text exceeds maxTokens.

500

Look at Ai21Jurassic2ChatBedrockApi#Ai21Jurassic2ChatModel for other model IDs. The other value supported is ai21.j2-ultra-v1. Model ID values can also be found in the AWS Bedrock documentation for base model IDs.

All properties prefixed with spring.ai.bedrock.jurassic2.chat.options can be overridden at runtime by adding a request specific Runtime Options to the Prompt call.

Runtime Options

The BedrockAi21Jurassic2ChatOptions.java provides model configurations, such as temperature, topP, maxTokens, etc.

On start-up, the default options can be configured with the BedrockAi21Jurassic2ChatModel(api, options) constructor or the spring.ai.bedrock.jurassic2.chat.options.* properties.

At run-time you can override the default options by adding new, request specific, options to the Prompt call. For example to override the default temperature for a specific request:

ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        BedrockAi21Jurassic2ChatOptions.builder()
            .withTemperature(0.4)
        .build()
    ));
In addition to the model specific BedrockAi21Jurassic2ChatOptions you can use a portable ChatOptions instance, created with the ChatOptionsBuilder#builder().

Sample Controller

Create a new Spring Boot project and add the spring-ai-bedrock-ai-spring-boot-starter to your pom (or gradle) dependencies.

Add a application.properties file, under the src/main/resources directory, to enable and configure the Jurassic-2 chat model:

spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.timeout=1000ms
spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}

spring.ai.bedrock.jurassic2.chat.enabled=true
spring.ai.bedrock.jurassic2.chat.options.temperature=0.8
replace the regions, access-key and secret-key with your AWS credentials.

This will create a BedrockAi21Jurassic2ChatModel implementation that you can inject into your class. Here is an example of a simple @Controller class that uses the chat model for text generations.

@RestController
public class ChatController {

    private final BedrockAi21Jurassic2ChatModel chatModel;

    @Autowired
    public ChatController(BedrockAi21Jurassic2ChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", chatModel.call(message));
    }

}

Manual Configuration

The BedrockAi21Jurassic2ChatModel implements the ChatModel uses the Low-level Client to connect to the Bedrock Jurassic-2 service.

Add the spring-ai-bedrock dependency to your project’s Maven pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-bedrock</artifactId>
</dependency>

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Next, create an BedrockAi21Jurassic2ChatModel and use it for text generations:

Ai21Jurassic2ChatBedrockApi api = new Ai21Jurassic2ChatBedrockApi(Ai21Jurassic2ChatModel.AI21_J2_MID_V1.id(),
    EnvironmentVariableCredentialsProvider.create(),
    Region.US_EAST_1.id(),
    new ObjectMapper(),
    Duration.ofMillis(1000L));

BedrockAi21Jurassic2ChatModel chatModel = new BedrockAi21Jurassic2ChatModel(api,
    BedrockAi21Jurassic2ChatOptions.builder()
        .withTemperature(0.5f)
        .withMaxTokens(100)
        .withTopP(0.9f).build());

ChatResponse response = chatModel.call(
    new Prompt("Generate the names of 5 famous pirates."));

Low-level Client

Ai21Jurassic2ChatBedrockApi provides a lightweight Java client on top of AWS Bedrock Jurassic-2 and Jurassic-2 Chat models.

The Ai21Jurassic2ChatBedrockApi supports the ai21.j2-mid-v1 and ai21.j2-ultra-v1 models and only support synchronous ( chatCompletion()).

Here is a simple snippet on how to use the API programmatically:

Ai21Jurassic2ChatBedrockApi jurassic2ChatApi = new Ai21Jurassic2ChatBedrockApi(
        Ai21Jurassic2ChatModel.AI21_J2_MID_V1.id(),
        Region.US_EAST_1.id(),
        Duration.ofMillis(1000L));

Ai21Jurassic2ChatRequest request = Ai21Jurassic2ChatRequest.builder("Hello, my name is")
        .withTemperature(0.9f)
        .withTopP(0.9f)
        .withMaxTokens(20)
        .build();

Ai21Jurassic2ChatResponse response = jurassic2ChatApi.chatCompletion(request);

Follow the Ai21Jurassic2ChatBedrockApi.java's JavaDoc for further information.