Skip to content

Commit eccf33a

Browse files
apappascsilayaperumalg
authored andcommitted
Refactor WatsonxAiChatOptions builder methods
- Refactor the builder methods to remove `with` as the prefix. - Introduce new methods with updated naming conventions. - Deprecate the existing `with*` methods to maintain backward compatibility. - Update WatsonxAi documentation
1 parent 1d38fd1 commit eccf33a

File tree

6 files changed

+208
-107
lines changed

6 files changed

+208
-107
lines changed

models/spring-ai-watsonx-ai/src/main/java/org/springframework/ai/watsonx/WatsonxAiChatModel.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Pablo Sanchidrian Herrera
5050
* @author John Jario Moreno Rojas
5151
* @author Christian Tzolov
52+
* @author Alexandros Pappas
5253
* @since 1.0.0
5354
*/
5455
public class WatsonxAiChatModel implements ChatModel, StreamingChatModel {
@@ -60,14 +61,14 @@ public class WatsonxAiChatModel implements ChatModel, StreamingChatModel {
6061
public WatsonxAiChatModel(WatsonxAiApi watsonxAiApi) {
6162
this(watsonxAiApi,
6263
WatsonxAiChatOptions.builder()
63-
.withTemperature(0.7)
64-
.withTopP(1.0)
65-
.withTopK(50)
66-
.withDecodingMethod("greedy")
67-
.withMaxNewTokens(20)
68-
.withMinNewTokens(0)
69-
.withRepetitionPenalty(1.0)
70-
.withStopSequences(List.of())
64+
.temperature(0.7)
65+
.topP(1.0)
66+
.topK(50)
67+
.decodingMethod("greedy")
68+
.maxNewTokens(20)
69+
.minNewTokens(0)
70+
.repetitionPenalty(1.0)
71+
.stopSequences(List.of())
7172
.build());
7273
}
7374

models/spring-ai-watsonx-ai/src/main/java/org/springframework/ai/watsonx/WatsonxAiChatOptions.java

Lines changed: 120 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Pablo Sanchidrian Herrera
3838
* @author John Jairo Moreno Rojas
3939
* @author Thomas Vitale
40+
* @author Alexandros Pappas
4041
* @since 1.0.0
4142
* @see <a href=
4243
* "https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-model-parameters.html?context=wx&audience=wdp">watsonx.ai
@@ -155,17 +156,17 @@ public static Map<String, Object> filterNonSupportedFields(Map<String, Object> o
155156

156157
public static WatsonxAiChatOptions fromOptions(WatsonxAiChatOptions fromOptions) {
157158
return WatsonxAiChatOptions.builder()
158-
.withTemperature(fromOptions.getTemperature())
159-
.withTopP(fromOptions.getTopP())
160-
.withTopK(fromOptions.getTopK())
161-
.withDecodingMethod(fromOptions.getDecodingMethod())
162-
.withMaxNewTokens(fromOptions.getMaxNewTokens())
163-
.withMinNewTokens(fromOptions.getMinNewTokens())
164-
.withStopSequences(fromOptions.getStopSequences())
165-
.withRepetitionPenalty(fromOptions.getRepetitionPenalty())
166-
.withRandomSeed(fromOptions.getRandomSeed())
167-
.withModel(fromOptions.getModel())
168-
.withAdditionalProperties(fromOptions.getAdditionalProperties())
159+
.temperature(fromOptions.getTemperature())
160+
.topP(fromOptions.getTopP())
161+
.topK(fromOptions.getTopK())
162+
.decodingMethod(fromOptions.getDecodingMethod())
163+
.maxNewTokens(fromOptions.getMaxNewTokens())
164+
.minNewTokens(fromOptions.getMinNewTokens())
165+
.stopSequences(fromOptions.getStopSequences())
166+
.repetitionPenalty(fromOptions.getRepetitionPenalty())
167+
.randomSeed(fromOptions.getRandomSeed())
168+
.model(fromOptions.getModel())
169+
.additionalProperties(fromOptions.getAdditionalProperties())
169170
.build();
170171
}
171172

@@ -326,66 +327,162 @@ public static class Builder {
326327

327328
WatsonxAiChatOptions options = new WatsonxAiChatOptions();
328329

329-
public Builder withTemperature(Double temperature) {
330+
public Builder temperature(Double temperature) {
330331
this.options.temperature = temperature;
331332
return this;
332333
}
333334

334-
public Builder withTopP(Double topP) {
335+
public Builder topP(Double topP) {
335336
this.options.topP = topP;
336337
return this;
337338
}
338339

339-
public Builder withTopK(Integer topK) {
340+
public Builder topK(Integer topK) {
340341
this.options.topK = topK;
341342
return this;
342343
}
343344

344-
public Builder withDecodingMethod(String decodingMethod) {
345+
public Builder decodingMethod(String decodingMethod) {
345346
this.options.decodingMethod = decodingMethod;
346347
return this;
347348
}
348349

349-
public Builder withMaxNewTokens(Integer maxNewTokens) {
350+
public Builder maxNewTokens(Integer maxNewTokens) {
350351
this.options.maxNewTokens = maxNewTokens;
351352
return this;
352353
}
353354

354-
public Builder withMinNewTokens(Integer minNewTokens) {
355+
public Builder minNewTokens(Integer minNewTokens) {
355356
this.options.minNewTokens = minNewTokens;
356357
return this;
357358
}
358359

359-
public Builder withStopSequences(List<String> stopSequences) {
360+
public Builder stopSequences(List<String> stopSequences) {
360361
this.options.stopSequences = stopSequences;
361362
return this;
362363
}
363364

364-
public Builder withRepetitionPenalty(Double repetitionPenalty) {
365+
public Builder repetitionPenalty(Double repetitionPenalty) {
365366
this.options.repetitionPenalty = repetitionPenalty;
366367
return this;
367368
}
368369

369-
public Builder withRandomSeed(Integer randomSeed) {
370+
public Builder randomSeed(Integer randomSeed) {
370371
this.options.randomSeed = randomSeed;
371372
return this;
372373
}
373374

374-
public Builder withModel(String model) {
375+
public Builder model(String model) {
375376
this.options.model = model;
376377
return this;
377378
}
378379

379-
public Builder withAdditionalProperty(String key, Object value) {
380+
public Builder additionalProperty(String key, Object value) {
380381
this.options.additional.put(key, value);
381382
return this;
382383
}
383384

384-
public Builder withAdditionalProperties(Map<String, Object> properties) {
385+
public Builder additionalProperties(Map<String, Object> properties) {
385386
this.options.additional.putAll(properties);
386387
return this;
387388
}
388389

390+
/**
391+
* @deprecated use {@link #temperature(Double)} instead.
392+
*/
393+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
394+
public Builder withTemperature(Double temperature) {
395+
return temperature(temperature);
396+
}
397+
398+
/**
399+
* @deprecated use {@link #topP(Double)} instead.
400+
*/
401+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
402+
public Builder withTopP(Double topP) {
403+
return topP(topP);
404+
}
405+
406+
/**
407+
* @deprecated use {@link #topK(Integer)} instead.
408+
*/
409+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
410+
public Builder withTopK(Integer topK) {
411+
return topK(topK);
412+
}
413+
414+
/**
415+
* @deprecated use {@link #decodingMethod(String)} instead.
416+
*/
417+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
418+
public Builder withDecodingMethod(String decodingMethod) {
419+
return decodingMethod(decodingMethod);
420+
}
421+
422+
/**
423+
* @deprecated use {@link #maxNewTokens(Integer)} instead.
424+
*/
425+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
426+
public Builder withMaxNewTokens(Integer maxNewTokens) {
427+
return maxNewTokens(maxNewTokens);
428+
}
429+
430+
/**
431+
* @deprecated use {@link #minNewTokens(Integer)} instead.
432+
*/
433+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
434+
public Builder withMinNewTokens(Integer minNewTokens) {
435+
return minNewTokens(minNewTokens);
436+
}
437+
438+
/**
439+
* @deprecated use {@link #stopSequences(List)} instead.
440+
*/
441+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
442+
public Builder withStopSequences(List<String> stopSequences) {
443+
return stopSequences(stopSequences);
444+
}
445+
446+
/**
447+
* @deprecated use {@link #repetitionPenalty(Double)} instead.
448+
*/
449+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
450+
public Builder withRepetitionPenalty(Double repetitionPenalty) {
451+
return repetitionPenalty(repetitionPenalty);
452+
}
453+
454+
/**
455+
* @deprecated use {@link #randomSeed(Integer)} instead.
456+
*/
457+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
458+
public Builder withRandomSeed(Integer randomSeed) {
459+
return randomSeed(randomSeed);
460+
}
461+
462+
/**
463+
* @deprecated use {@link #model(String)} instead.
464+
*/
465+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
466+
public Builder withModel(String model) {
467+
return model(model);
468+
}
469+
470+
/**
471+
* @deprecated use {@link #additionalProperty(String, Object)} instead.
472+
*/
473+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
474+
public Builder withAdditionalProperty(String key, Object value) {
475+
return additionalProperty(key, value);
476+
}
477+
478+
/**
479+
* @deprecated use {@link #additionalProperties(Map)} instead.
480+
*/
481+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
482+
public Builder withAdditionalProperties(Map<String, Object> properties) {
483+
return additionalProperties(properties);
484+
}
485+
389486
public WatsonxAiChatOptions build() {
390487
return this.options;
391488
}

models/spring-ai-watsonx-ai/src/test/java/org/springframework/ai/watsonx/WatsonxAiChatModelTest.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
/**
4848
* @author Pablo Sanchidrian Herrera
4949
* @author John Jairo Moreno Rojas
50+
* @author Alexandros Pappas
5051
*/
5152
public class WatsonxAiChatModelTest {
5253

@@ -66,9 +67,7 @@ public void testCreateRequestSuccessfullyWithDefaultParams() {
6667

6768
String msg = "Test message";
6869

69-
WatsonxAiChatOptions modelOptions = WatsonxAiChatOptions.builder()
70-
.withModel("meta-llama/llama-2-70b-chat")
71-
.build();
70+
WatsonxAiChatOptions modelOptions = WatsonxAiChatOptions.builder().model("meta-llama/llama-2-70b-chat").build();
7271
Prompt prompt = new Prompt(msg, modelOptions);
7372

7473
WatsonxAiChatRequest request = this.chatModel.request(prompt);
@@ -91,16 +90,16 @@ public void testCreateRequestSuccessfullyWithNonDefaultParams() {
9190
String msg = "Test message";
9291

9392
WatsonxAiChatOptions modelOptions = WatsonxAiChatOptions.builder()
94-
.withModel("meta-llama/llama-2-70b-chat")
95-
.withDecodingMethod("sample")
96-
.withTemperature(0.1)
97-
.withTopP(0.2)
98-
.withTopK(10)
99-
.withMaxNewTokens(30)
100-
.withMinNewTokens(10)
101-
.withRepetitionPenalty(1.4)
102-
.withStopSequences(List.of("\n\n\n"))
103-
.withRandomSeed(4)
93+
.model("meta-llama/llama-2-70b-chat")
94+
.decodingMethod("sample")
95+
.temperature(0.1)
96+
.topP(0.2)
97+
.topK(10)
98+
.maxNewTokens(30)
99+
.minNewTokens(10)
100+
.repetitionPenalty(1.4)
101+
.stopSequences(List.of("\n\n\n"))
102+
.randomSeed(4)
104103
.build();
105104

106105
Prompt prompt = new Prompt(msg, modelOptions);
@@ -125,16 +124,16 @@ public void testCreateRequestSuccessfullyWithChatDisabled() {
125124
String msg = "Test message";
126125

127126
WatsonxAiChatOptions modelOptions = WatsonxAiChatOptions.builder()
128-
.withModel("meta-llama/llama-2-70b-chat")
129-
.withDecodingMethod("sample")
130-
.withTemperature(0.1)
131-
.withTopP(0.2)
132-
.withTopK(10)
133-
.withMaxNewTokens(30)
134-
.withMinNewTokens(10)
135-
.withRepetitionPenalty(1.4)
136-
.withStopSequences(List.of("\n\n\n"))
137-
.withRandomSeed(4)
127+
.model("meta-llama/llama-2-70b-chat")
128+
.decodingMethod("sample")
129+
.temperature(0.1)
130+
.topP(0.2)
131+
.topK(10)
132+
.maxNewTokens(30)
133+
.minNewTokens(10)
134+
.repetitionPenalty(1.4)
135+
.stopSequences(List.of("\n\n\n"))
136+
.randomSeed(4)
138137
.build();
139138

140139
Prompt prompt = new Prompt(msg, modelOptions);
@@ -160,9 +159,9 @@ public void testCallMethod() {
160159
WatsonxAiChatModel chatModel = new WatsonxAiChatModel(mockChatApi);
161160

162161
Prompt prompt = new Prompt(List.of(new SystemMessage("Your prompt here")),
163-
WatsonxAiChatOptions.builder().withModel("google/flan-ul2").build());
162+
WatsonxAiChatOptions.builder().model("google/flan-ul2").build());
164163

165-
WatsonxAiChatOptions parameters = WatsonxAiChatOptions.builder().withModel("google/flan-ul2").build();
164+
WatsonxAiChatOptions parameters = WatsonxAiChatOptions.builder().model("google/flan-ul2").build();
166165

167166
WatsonxAiChatResults fakeResults = new WatsonxAiChatResults("LLM response", 4, 3, "max_tokens");
168167

@@ -193,9 +192,9 @@ public void testStreamMethod() {
193192
WatsonxAiChatModel chatModel = new WatsonxAiChatModel(mockChatApi);
194193

195194
Prompt prompt = new Prompt(List.of(new SystemMessage("Your prompt here")),
196-
WatsonxAiChatOptions.builder().withModel("google/flan-ul2").build());
195+
WatsonxAiChatOptions.builder().model("google/flan-ul2").build());
197196

198-
WatsonxAiChatOptions parameters = WatsonxAiChatOptions.builder().withModel("google/flan-ul2").build();
197+
WatsonxAiChatOptions parameters = WatsonxAiChatOptions.builder().model("google/flan-ul2").build();
199198

200199
WatsonxAiChatResults fakeResultsFirst = new WatsonxAiChatResults("LLM resp", 0, 0, "max_tokens");
201200
WatsonxAiChatResults fakeResultsSecond = new WatsonxAiChatResults("onse", 4, 3, "not_finished");

0 commit comments

Comments
 (0)