Skip to content

Commit c82ce2e

Browse files
committed
Refactor Spring AI Bedrock options
- Refactor the options builder methods to remove `with` as the suffix - Update all the models under spring-ai-bedrock - Deprecate the existing methods - Update references and docs
1 parent d3d34c9 commit c82ce2e

File tree

43 files changed

+954
-267
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+954
-267
lines changed

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic/AnthropicChatOptions.java

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @author Christian Tzolov
3232
* @author Thomas Vitale
33+
* @author Ilayaperumal Gopinathan
3334
*/
3435
@JsonInclude(Include.NON_NULL)
3536
public class AnthropicChatOptions implements ChatOptions {
@@ -78,12 +79,12 @@ public static Builder builder() {
7879
}
7980

8081
public static AnthropicChatOptions fromOptions(AnthropicChatOptions fromOptions) {
81-
return builder().withTemperature(fromOptions.getTemperature())
82-
.withMaxTokensToSample(fromOptions.getMaxTokensToSample())
83-
.withTopK(fromOptions.getTopK())
84-
.withTopP(fromOptions.getTopP())
85-
.withStopSequences(fromOptions.getStopSequences())
86-
.withAnthropicVersion(fromOptions.getAnthropicVersion())
82+
return builder().temperature(fromOptions.getTemperature())
83+
.maxTokensToSample(fromOptions.getMaxTokensToSample())
84+
.topK(fromOptions.getTopK())
85+
.topP(fromOptions.getTopP())
86+
.stopSequences(fromOptions.getStopSequences())
87+
.anthropicVersion(fromOptions.getAnthropicVersion())
8788
.build();
8889
}
8990

@@ -177,31 +178,85 @@ public static class Builder {
177178

178179
private final AnthropicChatOptions options = new AnthropicChatOptions();
179180

181+
public Builder temperature(Double temperature) {
182+
this.options.setTemperature(temperature);
183+
return this;
184+
}
185+
186+
public Builder maxTokensToSample(Integer maxTokensToSample) {
187+
this.options.setMaxTokensToSample(maxTokensToSample);
188+
return this;
189+
}
190+
191+
public Builder topK(Integer topK) {
192+
this.options.setTopK(topK);
193+
return this;
194+
}
195+
196+
public Builder topP(Double topP) {
197+
this.options.setTopP(topP);
198+
return this;
199+
}
200+
201+
public Builder stopSequences(List<String> stopSequences) {
202+
this.options.setStopSequences(stopSequences);
203+
return this;
204+
}
205+
206+
public Builder anthropicVersion(String anthropicVersion) {
207+
this.options.setAnthropicVersion(anthropicVersion);
208+
return this;
209+
}
210+
211+
/**
212+
* @deprecated use {@link #temperature(Double)} instead.
213+
*/
214+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
180215
public Builder withTemperature(Double temperature) {
181216
this.options.setTemperature(temperature);
182217
return this;
183218
}
184219

220+
/**
221+
* @deprecated use {@link #maxTokensToSample(Integer)} instead.
222+
*/
223+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
185224
public Builder withMaxTokensToSample(Integer maxTokensToSample) {
186225
this.options.setMaxTokensToSample(maxTokensToSample);
187226
return this;
188227
}
189228

229+
/**
230+
* @deprecated use {@link #topK(Integer)} instead.
231+
*/
232+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
190233
public Builder withTopK(Integer topK) {
191234
this.options.setTopK(topK);
192235
return this;
193236
}
194237

238+
/**
239+
* @deprecated use {@link #topP(Double)} instead.
240+
*/
241+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
195242
public Builder withTopP(Double topP) {
196243
this.options.setTopP(topP);
197244
return this;
198245
}
199246

247+
/**
248+
* @deprecated use {@link #stopSequences(List)} instead.
249+
*/
250+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
200251
public Builder withStopSequences(List<String> stopSequences) {
201252
this.options.setStopSequences(stopSequences);
202253
return this;
203254
}
204255

256+
/**
257+
* @deprecated use {@link #anthropicVersion(String)} instead.
258+
*/
259+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
205260
public Builder withAnthropicVersion(String anthropicVersion) {
206261
this.options.setAnthropicVersion(anthropicVersion);
207262
return this;

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic/BedrockAnthropicChatModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public class BedrockAnthropicChatModel implements ChatModel, StreamingChatModel
5050
public BedrockAnthropicChatModel(AnthropicChatBedrockApi chatApi) {
5151
this(chatApi,
5252
AnthropicChatOptions.builder()
53-
.withTemperature(0.8)
54-
.withMaxTokensToSample(500)
55-
.withTopK(10)
56-
.withAnthropicVersion(AnthropicChatBedrockApi.DEFAULT_ANTHROPIC_VERSION)
53+
.temperature(0.8)
54+
.maxTokensToSample(500)
55+
.topK(10)
56+
.anthropicVersion(AnthropicChatBedrockApi.DEFAULT_ANTHROPIC_VERSION)
5757
.build());
5858
}
5959

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic/api/AnthropicChatBedrockApi.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @author Christian Tzolov
4040
* @author Thomas Vitale
4141
* @author Wei Jiang
42+
* @author Ilayaperumal Gopinathan
4243
* @since 0.8.0
4344
*/
4445
// @formatter:off
@@ -215,31 +216,85 @@ private Builder(String prompt) {
215216
this.prompt = prompt;
216217
}
217218

219+
public Builder temperature(Double temperature) {
220+
this.temperature = temperature;
221+
return this;
222+
}
223+
224+
public Builder maxTokensToSample(Integer maxTokensToSample) {
225+
this.maxTokensToSample = maxTokensToSample;
226+
return this;
227+
}
228+
229+
public Builder topK(Integer topK) {
230+
this.topK = topK;
231+
return this;
232+
}
233+
234+
public Builder topP(Double tpoP) {
235+
this.topP = tpoP;
236+
return this;
237+
}
238+
239+
public Builder stopSequences(List<String> stopSequences) {
240+
this.stopSequences = stopSequences;
241+
return this;
242+
}
243+
244+
public Builder anthropicVersion(String anthropicVersion) {
245+
this.anthropicVersion = anthropicVersion;
246+
return this;
247+
}
248+
249+
/**
250+
* @deprecated use {@link #temperature( Double)} instead.
251+
*/
252+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
218253
public Builder withTemperature(Double temperature) {
219254
this.temperature = temperature;
220255
return this;
221256
}
222257

258+
/**
259+
* @deprecated use {@link #maxTokensToSample( Integer)} instead.
260+
*/
261+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
223262
public Builder withMaxTokensToSample(Integer maxTokensToSample) {
224263
this.maxTokensToSample = maxTokensToSample;
225264
return this;
226265
}
227266

267+
/**
268+
* @deprecated use {@link #topK( Integer)} instead.
269+
*/
270+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
228271
public Builder withTopK(Integer topK) {
229272
this.topK = topK;
230273
return this;
231274
}
232275

276+
/**
277+
* @deprecated use {@link #topP( Double)} instead.
278+
*/
279+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
233280
public Builder withTopP(Double tpoP) {
234281
this.topP = tpoP;
235282
return this;
236283
}
237284

285+
/**
286+
* @deprecated use {@link #stopSequences( List)} instead.
287+
*/
288+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
238289
public Builder withStopSequences(List<String> stopSequences) {
239290
this.stopSequences = stopSequences;
240291
return this;
241292
}
242293

294+
/**
295+
* @deprecated use {@link #anthropicVersion( String)} instead.
296+
*/
297+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
243298
public Builder withAnthropicVersion(String anthropicVersion) {
244299
this.anthropicVersion = anthropicVersion;
245300
return this;

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/Anthropic3ChatOptions.java

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ public static Builder builder() {
9292
* @return a new {@link Anthropic3ChatOptions}
9393
*/
9494
public static Anthropic3ChatOptions fromOptions(Anthropic3ChatOptions fromOptions) {
95-
return builder().withTemperature(fromOptions.getTemperature())
96-
.withMaxTokens(fromOptions.getMaxTokens())
97-
.withTopK(fromOptions.getTopK())
98-
.withTopP(fromOptions.getTopP())
99-
.withStopSequences(fromOptions.getStopSequences())
100-
.withAnthropicVersion(fromOptions.getAnthropicVersion())
95+
return builder().temperature(fromOptions.getTemperature())
96+
.maxTokens(fromOptions.getMaxTokens())
97+
.topK(fromOptions.getTopK())
98+
.topP(fromOptions.getTopP())
99+
.stopSequences(fromOptions.getStopSequences())
100+
.anthropicVersion(fromOptions.getAnthropicVersion())
101101
.build();
102102
}
103103

@@ -256,7 +256,7 @@ private Builder() {
256256
* @param temperature the temperature
257257
* @return this {@link Builder} instance
258258
*/
259-
public Builder withTemperature(Double temperature) {
259+
public Builder temperature(Double temperature) {
260260
this.options.setTemperature(temperature);
261261
return this;
262262
}
@@ -266,7 +266,7 @@ public Builder withTemperature(Double temperature) {
266266
* @param maxTokens the maximum number of tokens
267267
* @return this {@link Builder} instance
268268
*/
269-
public Builder withMaxTokens(Integer maxTokens) {
269+
public Builder maxTokens(Integer maxTokens) {
270270
this.options.setMaxTokens(maxTokens);
271271
return this;
272272
}
@@ -276,7 +276,7 @@ public Builder withMaxTokens(Integer maxTokens) {
276276
* @param topK the top k
277277
* @return this {@link Builder} instance
278278
*/
279-
public Builder withTopK(Integer topK) {
279+
public Builder topK(Integer topK) {
280280
this.options.setTopK(topK);
281281
return this;
282282
}
@@ -286,7 +286,7 @@ public Builder withTopK(Integer topK) {
286286
* @param topP the top p
287287
* @return this {@link Builder} instance
288288
*/
289-
public Builder withTopP(Double topP) {
289+
public Builder topP(Double topP) {
290290
this.options.setTopP(topP);
291291
return this;
292292
}
@@ -296,7 +296,7 @@ public Builder withTopP(Double topP) {
296296
* @param stopSequences the stop sequences
297297
* @return this {@link Builder} instance
298298
*/
299-
public Builder withStopSequences(List<String> stopSequences) {
299+
public Builder stopSequences(List<String> stopSequences) {
300300
this.options.setStopSequences(stopSequences);
301301
return this;
302302
}
@@ -306,6 +306,60 @@ public Builder withStopSequences(List<String> stopSequences) {
306306
* @param anthropicVersion the version of the generative to use
307307
* @return this {@link Builder} instance
308308
*/
309+
public Builder anthropicVersion(String anthropicVersion) {
310+
this.options.setAnthropicVersion(anthropicVersion);
311+
return this;
312+
}
313+
314+
/**
315+
* @deprecated use {@link #temperature(Double)} instead.
316+
*/
317+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
318+
public Builder withTemperature(Double temperature) {
319+
this.options.setTemperature(temperature);
320+
return this;
321+
}
322+
323+
/**
324+
* @deprecated use {@link #maxTokens(Integer)} instead.
325+
*/
326+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
327+
public Builder withMaxTokens(Integer maxTokens) {
328+
this.options.setMaxTokens(maxTokens);
329+
return this;
330+
}
331+
332+
/**
333+
* @deprecated use {@link #topK(Integer)} instead.
334+
*/
335+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
336+
public Builder withTopK(Integer topK) {
337+
this.options.setTopK(topK);
338+
return this;
339+
}
340+
341+
/**
342+
* @deprecated use {@link #topP(Double)} instead.
343+
*/
344+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
345+
public Builder withTopP(Double topP) {
346+
this.options.setTopP(topP);
347+
return this;
348+
}
349+
350+
/**
351+
* @deprecated use {@link #stopSequences(List)} instead.
352+
*/
353+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
354+
public Builder withStopSequences(List<String> stopSequences) {
355+
this.options.setStopSequences(stopSequences);
356+
return this;
357+
}
358+
359+
/**
360+
* @deprecated use {@link #anthropicVersion(String)} instead.
361+
*/
362+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
309363
public Builder withAnthropicVersion(String anthropicVersion) {
310364
this.options.setAnthropicVersion(anthropicVersion);
311365
return this;

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/BedrockAnthropic3ChatModel.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public class BedrockAnthropic3ChatModel implements ChatModel, StreamingChatModel
6666
public BedrockAnthropic3ChatModel(Anthropic3ChatBedrockApi chatApi) {
6767
this(chatApi,
6868
Anthropic3ChatOptions.builder()
69-
.withTemperature(0.8)
70-
.withMaxTokens(500)
71-
.withTopK(10)
72-
.withAnthropicVersion(Anthropic3ChatBedrockApi.DEFAULT_ANTHROPIC_VERSION)
69+
.temperature(0.8)
70+
.maxTokens(500)
71+
.topK(10)
72+
.anthropicVersion(Anthropic3ChatBedrockApi.DEFAULT_ANTHROPIC_VERSION)
7373
.build());
7474
}
7575

@@ -138,7 +138,7 @@ protected Usage extractUsage(AnthropicChatResponse response) {
138138
AnthropicChatRequest createRequest(Prompt prompt) {
139139

140140
AnthropicChatRequest request = AnthropicChatRequest.builder(toAnthropicMessages(prompt))
141-
.withSystem(toAnthropicSystemContext(prompt))
141+
.system(toAnthropicSystemContext(prompt))
142142
.build();
143143

144144
if (this.defaultOptions != null) {

0 commit comments

Comments
 (0)