Skip to content

Commit 779b5a8

Browse files
committed
Bedrock Anthropic Options Support
1 parent b0d8d2b commit 779b5a8

File tree

16 files changed

+625
-326
lines changed

16 files changed

+625
-326
lines changed

models/spring-ai-bedrock/README_ANTHROPIC_CHAT.md

Lines changed: 82 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,91 @@
1-
# 1. Bedrock Anthropic
1+
# Bedrock Anthropic
22

33
Provides Bedrock Anthropic Chat API and Spring-AI chat clients.
44

5-
## 1.1 AnthropicChatBedrockApi
5+
## BedrockAnthropicChatClient
6+
7+
The [BedrockAnthropicChatClient](./src/main/java/org/springframework/ai/bedrock/anthropic/BedrockAnthropicChatClient.java) implements the `ChatClient` and `StreamingChatClient` and uses the `AnthropicChatBedrockApi` library to connect to the Bedrock Anthropic service.
8+
9+
Add the `spring-ai-` dependency to your project's Maven `pom.xml` file:
10+
11+
```xml
12+
<dependency>
13+
<groupId>org.springframework.ai</groupId>
14+
<artifactId>spring-ai-bedrock</artifactId>
15+
<version>0.8.0-SNAPSHOT</version>
16+
</dependency>
17+
```
18+
19+
or to your Gradle `build.gradle` build file.
20+
21+
```gradle
22+
dependencies {
23+
implementation 'org.springframework.ai:spring-ai-bedrock:0.8.0-SNAPSHOT'
24+
}
25+
```
26+
27+
Next, create an `BedrockAnthropicChatClient` instance and use it to text generations requests:
28+
29+
```java
30+
AnthropicChatBedrockApi anthropicApi = new AnthropicChatBedrockApi(
31+
AnthropicChatBedrockApi.AnthropicModel.CLAUDE_V2.id(),
32+
EnvironmentVariableCredentialsProvider.create(),
33+
Region.EU_CENTRAL_1.id(),
34+
new ObjectMapper());
35+
36+
BedrockAnthropicChatClient chatClient = new BedrockAnthropicChatClient(anthropicApi,
37+
AnthropicChatOptions.builder()
38+
.withTemperature(0.6f)
39+
.withTopK(10)
40+
.withTopP(0.8f)
41+
.withMaxTokensToSample(100)
42+
.withAnthropicVersion(AnthropicChatBedrockApi.DEFAULT_ANTHROPIC_VERSION)
43+
.build());
44+
45+
ChatResponse response = chatClient.call(
46+
new Prompt("Generate the names of 5 famous pirates."));
47+
48+
// Or with streaming responses
49+
Flux<ChatResponse> response = chatClient.stream(
50+
new Prompt("Generate the names of 5 famous pirates."));
51+
```
52+
53+
or you can leverage the `spring-ai-bedrock-ai-spring-boot-starter` Spring Boot starter:
54+
55+
```xml
56+
<dependency>
57+
<groupId>org.springframework.ai</groupId>
58+
<artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
59+
<version>0.8.0-SNAPSHOT</version>
60+
</dependency>
61+
```
62+
63+
And set `spring.ai.bedrock.anthropic.chat.enabled=true`.
64+
By default the client is disabled.
65+
66+
Use the `BedrockAnthropicChatProperties` to configure the Bedrock Anthropic Chat client:
67+
68+
| Property | Description | Default |
69+
| ------------- | ------------- | ------------- |
70+
| spring.ai.bedrock.aws.region | AWS region to use. | us-east-1 |
71+
| spring.ai.bedrock.aws.accessKey | AWS credentials access key. | |
72+
| spring.ai.bedrock.aws.secretKey | AWS credentials secret key. | |
73+
| spring.ai.bedrock.anthropic.chat.enable | Enable Bedrock Anthropic chat client. Disabled by default | false |
74+
| spring.ai.bedrock.anthropic.chat.model | The model id to use. See the `AnthropicChatModel` for the supported models. | anthropic.claude-v2 |
75+
| spring.ai.bedrock.anthropic.chat.options.temperature | Controls the randomness of the output. Values can range over [0.0,1.0] | 0.8 |
76+
| spring.ai.bedrock.anthropic.chat.options.topP | The maximum cumulative probability of tokens to consider when sampling. | AWS Bedrock default |
77+
| spring.ai.bedrock.anthropic.chat.options.topK | Specify the number of token choices the generative uses to generate the next token. | AWS Bedrock default |
78+
| spring.ai.bedrock.anthropic.chat.options.stopSequences | Configure up to four sequences that the generative recognizes. After a stop sequence, the generative stops generating further tokens. The returned text doesn't contain the stop sequence. | 10 |
79+
| spring.ai.bedrock.anthropic.chat.options.anthropicVersion | The version of the generative to use. | bedrock-2023-05-31 |
80+
| spring.ai.bedrock.anthropic.chat.options.maxTokensToSample | Specify the maximum number of tokens to use in the generated response. Note that the models may stop before reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate. We recommend a limit of 4,000 tokens for optimal performance. | 500 |
81+
82+
## Appendices
83+
84+
## Using low-level AnthropicChatBedrockApi Library
685

786
[AnthropicChatBedrockApi](./src/main/java/org/springframework/ai/bedrock/anthropic/api/AnthropicChatBedrockApi.java) provides is lightweight Java client on top of AWS Bedrock [Anthropic Claude models](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html).
887

9-
Following class diagram illustrates the Llama2ChatBedrockApi interface and building blocks:
88+
Following class diagram illustrates the AnthropicChatBedrockApi interface and building blocks:
1089

1190
![AnthropicChatBedrockApi Class Diagram](./src/test/resources/doc/Bedrock-Anthropic-Chat-API.jpg)
1291

@@ -42,51 +121,3 @@ System.out.println(responses);
42121
```
43122

44123
Follow the [AnthropicChatBedrockApi.java](./src/main/java/org/springframework/ai/bedrock/anthropic/api/AnthropicChatBedrockApi.java)'s JavaDoc for further information.
45-
46-
## 1.2 BedrockAnthropicChatClient
47-
48-
[BedrockAnthropicChatClient](./src/main/java/org/springframework/ai/bedrock/anthropic/BedrockAnthropicChatClient.java) implements the Spring-Ai `ChatClient` and `StreamingChatClient` on top of the `AnthropicChatBedrockApi`.
49-
50-
You can use like this:
51-
52-
```java
53-
@Bean
54-
public AnthropicChatBedrockApi anthropicApi() {
55-
return new AnthropicChatBedrockApi(
56-
AnthropicChatBedrockApi.AnthropicModel.CLAUDE_V2.id(),
57-
EnvironmentVariableCredentialsProvider.create(),
58-
Region.EU_CENTRAL_1.id(),
59-
new ObjectMapper());
60-
}
61-
62-
@Bean
63-
public BedrockAnthropicChatClient anthropicChatClient(AnthropicChatBedrockApi anthropicApi) {
64-
return new BedrockAnthropicChatClient(anthropicApi);
65-
}
66-
```
67-
68-
or you can leverage the `spring-ai-bedrock-ai-spring-boot-starter` Spring Boot starter:
69-
70-
```xml
71-
<dependency>
72-
<artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
73-
<groupId>org.springframework.ai</groupId>
74-
<version>0.8.0-SNAPSHOT</version>
75-
</dependency>
76-
```
77-
78-
And set `spring.ai.bedrock.anthropic.chat.enabled=true`.
79-
By default the client is disabled.
80-
81-
Use the `BedrockAnthropicChatProperties` to configure the Bedrock Llama2 Chat client:
82-
83-
| Property | Description | Default |
84-
| ------------- | ------------- | ------------- |
85-
| spring.ai.bedrock.aws.region | AWS region to use. | us-east-1 |
86-
| spring.ai.bedrock.aws.accessKey | AWS credentials access key. | |
87-
| spring.ai.bedrock.aws.secretKey | AWS credentials secret key. | |
88-
| spring.ai.bedrock.anthropic.chat.enable | Enable Bedrock Llama2 chat client. Disabled by default | false |
89-
| spring.ai.bedrock.anthropic.chat.temperature | Controls the randomness of the output. Values can range over [0.0,1.0] | 0.8 |
90-
| spring.ai.bedrock.anthropic.chat.topP | The maximum cumulative probability of tokens to consider when sampling. | AWS Bedrock default |
91-
| spring.ai.bedrock.anthropic.chat.maxGenLen | Specify the maximum number of tokens to use in the generated response. | 300 |
92-
| spring.ai.bedrock.anthropic.chat.model | The model id to use. See the `Llama2ChatModel` for the supported models. | meta.llama2-70b-chat-v1 |
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.bedrock.anthropic;
18+
19+
import java.util.List;
20+
21+
import com.fasterxml.jackson.annotation.JsonInclude;
22+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
23+
24+
import org.springframework.ai.chat.ChatOptions;
25+
26+
import com.fasterxml.jackson.annotation.JsonProperty;
27+
28+
/**
29+
* @author Christian Tzolov
30+
*/
31+
@JsonInclude(Include.NON_NULL)
32+
public class AnthropicChatOptions implements ChatOptions {
33+
34+
// @formatter:off
35+
/**
36+
* Controls the randomness of the output. Values can range over [0.0,1.0], inclusive. A value closer to 1.0 will
37+
* produce responses that are more varied, while a value closer to 0.0 will typically result in less surprising
38+
* responses from the generative. This value specifies default to be used by the backend while making the call to
39+
* the generative.
40+
*/
41+
private @JsonProperty("temperature") Float temperature;
42+
43+
/**
44+
* Specify the maximum number of tokens to use in the generated response. Note that the models may stop before
45+
* reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate. We
46+
* recommend a limit of 4,000 tokens for optimal performance.
47+
*/
48+
private @JsonProperty("max_tokens_to_sample") Integer maxTokensToSample;
49+
50+
/**
51+
* Specify the number of token choices the generative uses to generate the next token.
52+
*/
53+
private @JsonProperty("top_k") Integer topK;
54+
55+
/**
56+
* The maximum cumulative probability of tokens to consider when sampling. The generative uses combined Top-k and
57+
* nucleus sampling. Nucleus sampling considers the smallest set of tokens whose probability sum is at least topP.
58+
*/
59+
private @JsonProperty("top_p") Float topP;
60+
61+
/**
62+
* Configure up to four sequences that the generative recognizes. After a stop sequence, the generative stops
63+
* generating further tokens. The returned text doesn't contain the stop sequence.
64+
*/
65+
private @JsonProperty("stop_sequences") List<String> stopSequences;
66+
67+
/**
68+
* The version of the generative to use. The default value is bedrock-2023-05-31.
69+
*/
70+
private @JsonProperty("anthropic_version") String anthropicVersion;
71+
// @formatter:on
72+
73+
public static Builder builder() {
74+
return new Builder();
75+
}
76+
77+
public static class Builder {
78+
79+
private final AnthropicChatOptions options = new AnthropicChatOptions();
80+
81+
public Builder withTemperature(Float temperature) {
82+
this.options.setTemperature(temperature);
83+
return this;
84+
}
85+
86+
public Builder withMaxTokensToSample(Integer maxTokensToSample) {
87+
this.options.setMaxTokensToSample(maxTokensToSample);
88+
return this;
89+
}
90+
91+
public Builder withTopK(Integer topK) {
92+
this.options.setTopK(topK);
93+
return this;
94+
}
95+
96+
public Builder withTopP(Float topP) {
97+
this.options.setTopP(topP);
98+
return this;
99+
}
100+
101+
public Builder withStopSequences(List<String> stopSequences) {
102+
this.options.setStopSequences(stopSequences);
103+
return this;
104+
}
105+
106+
public Builder withAnthropicVersion(String anthropicVersion) {
107+
this.options.setAnthropicVersion(anthropicVersion);
108+
return this;
109+
}
110+
111+
public AnthropicChatOptions build() {
112+
return this.options;
113+
}
114+
115+
}
116+
117+
@Override
118+
public Float getTemperature() {
119+
return this.temperature;
120+
}
121+
122+
@Override
123+
public void setTemperature(Float temperature) {
124+
this.temperature = temperature;
125+
}
126+
127+
public Integer getMaxTokensToSample() {
128+
return this.maxTokensToSample;
129+
}
130+
131+
public void setMaxTokensToSample(Integer maxTokensToSample) {
132+
this.maxTokensToSample = maxTokensToSample;
133+
}
134+
135+
@Override
136+
public Integer getTopK() {
137+
return this.topK;
138+
}
139+
140+
@Override
141+
public void setTopK(Integer topK) {
142+
this.topK = topK;
143+
}
144+
145+
@Override
146+
public Float getTopP() {
147+
return this.topP;
148+
}
149+
150+
@Override
151+
public void setTopP(Float topP) {
152+
this.topP = topP;
153+
}
154+
155+
public List<String> getStopSequences() {
156+
return this.stopSequences;
157+
}
158+
159+
public void setStopSequences(List<String> stopSequences) {
160+
this.stopSequences = stopSequences;
161+
}
162+
163+
public String getAnthropicVersion() {
164+
return this.anthropicVersion;
165+
}
166+
167+
public void setAnthropicVersion(String anthropicVersion) {
168+
this.anthropicVersion = anthropicVersion;
169+
}
170+
171+
}

0 commit comments

Comments
 (0)