Llama2 Chat

Meta’s Llama 2 Chat is part of the Llama 2 collection of large language models. It excels in dialogue-based applications with a parameter scale ranging from 7 billion to 70 billion. Leveraging public datasets and over 1 million human annotations, Llama Chat offers context-aware dialogues.

Trained on 2 trillion tokens from public data sources, Llama-2-Chat provides extensive knowledge for insightful conversations. Rigorous testing, including over 1,000 hours of red-teaming and annotation, ensures both performance and safety, making it a reliable choice for AI-driven dialogues.

The AWS Llama 2 Model Page and Amazon Bedrock User Guide contains detailed information on how to use the AWS hosted model.


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.


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


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 Llama2 Chat Support

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


Chat Properties

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

Property Description Default


AWS region to use.



AWS access key.



AWS secret key.


The prefix spring.ai.bedrock.llama2.chat is the property prefix that configures the chat client implementation for Llama2.

Property Description Default


Enable or disable support for Llama2



The model id to use (See Below)



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.



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


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


Look at Llama2ChatBedrockApi#Llama2ChatModel for other model IDs. The other value supported is meta.llama2-13b-chat-v1. Model ID values can also be found in the AWS Bedrock documentation for base model IDs.

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

Chat Options

The BedrockLlama2ChatOptions.java provides model configurations, such as temperature, topK, topP, etc.

On start-up, the default options can be configured with the BedrockLlama2ChatClient(api, options) constructor or the spring.ai.bedrock.llama2.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 = chatClient.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
In addition to the model specific BedrockLlama2ChatOptions you can use a portable ChatOptions instance, created with the ChatOptionsBuilder#builder().

Sample Controller (Auto-configuration)

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 Anthropic Chat client:


replace the regions, access-key and secret-key with your AWS credentials.

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

public class ChatController {

    private final BedrockLlama2ChatClient chatClient;

    public ChatController(BedrockLlama2ChatClient chatClient) {
        this.chatClient = chatClient;

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

	public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return chatClient.stream(prompt);

Manual Configuration

The BedrockLlama2ChatClient implements the ChatClient and StreamingChatClient and uses the Low-level Llama2ChatBedrockApi Client to connect to the Bedrock Anthropic service.

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


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 BedrockLlama2ChatClient and use it for text generations:

Llama2ChatBedrockApi api = new Llama2ChatBedrockApi(Llama2ChatModel.LLAMA2_70B_CHAT_V1.id(),
	EnvironmentVariableCredentialsProvider.create(), Region.US_EAST_1.id(), new ObjectMapper());

BedrockLlama2ChatClient chatClient = new BedrockLlama2ChatClient(api,

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

// Or with streaming responses
Flux<ChatResponse> response = chatClient.stream(
    new Prompt("Generate the names of 5 famous pirates."));

Low-level Llama2ChatBedrockApi Client

Llama2ChatBedrockApi provides is lightweight Java client on top of AWS Bedrock Meta Llama 2 and Llama 2 Chat models.

Following class diagram illustrates the Llama2ChatBedrockApi interface and building blocks:

Llama2ChatBedrockApi Class Diagram

The Llama2ChatBedrockApi supports the meta.llama2-13b-chat-v1 and meta.llama2-70b-chat-v1 models for both synchronous (e.g. chatCompletion()) and streaming (e.g. chatCompletionStream()) responses.

Here is a simple snippet how to use the api programmatically:

Llama2ChatBedrockApi llama2ChatApi = new Llama2ChatBedrockApi(

Llama2ChatRequest request = Llama2ChatRequest.builder("Hello, my name is")

Llama2ChatResponse response = llama2ChatApi.chatCompletion(request);

// Streaming response
Flux<Llama2ChatResponse> responseStream = llama2ChatApi.chatCompletionStream(request);
List<Llama2ChatResponse> responses = responseStream.collectList().block();

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