Skip to content

Commit 6f36337

Browse files
committed
Refactor ImageOptions builder method
- Deprecate the builder method with the prefix 'with' and add new methods
1 parent d77c950 commit 6f36337

File tree

8 files changed

+71
-17
lines changed

8 files changed

+71
-17
lines changed

models/spring-ai-azure-openai/src/test/java/org/springframework/ai/azure/openai/image/AzureOpenAiImageModelIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class AzureOpenAiImageModelIT {
5353

5454
@Test
5555
void imageAsUrlTest() {
56-
var options = ImageOptionsBuilder.builder().withHeight(1024).withWidth(1024).build();
56+
var options = ImageOptionsBuilder.builder().height(1024).width(1024).build();
5757

5858
var instructions = """
5959
A light cream colored mini golden doodle with a sign that contains the message "I'm on my way to BARCADE!".""";

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/image/OpenAiImageModelIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class OpenAiImageModelIT extends AbstractIT {
3838

3939
@Test
4040
void imageAsUrlTest() {
41-
var options = ImageOptionsBuilder.builder().withHeight(1024).withWidth(1024).build();
41+
var options = ImageOptionsBuilder.builder().height(1024).width(1024).build();
4242

4343
var instructions = """
4444
A light cream colored mini golden doodle with a sign that contains the message "I'm on my way to BARCADE!".""";

models/spring-ai-qianfan/src/test/java/org/springframework/ai/qianfan/image/QianFanImageModelIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class QianFanImageModelIT {
4545

4646
@Test
4747
void imageTest() {
48-
var options = ImageOptionsBuilder.builder().withHeight(1024).withWidth(1024).build();
48+
var options = ImageOptionsBuilder.builder().height(1024).width(1024).build();
4949

5050
var instructions = """
5151
A light cream colored mini golden doodle with a sign that contains the message "I'm on my way to BARCADE!".""";

models/spring-ai-zhipuai/src/test/java/org/springframework/ai/zhipuai/image/ZhiPuAiImageModelIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ZhiPuAiImageModelIT {
4040

4141
@Test
4242
void imageAsUrlTest() {
43-
var options = ImageOptionsBuilder.builder().withHeight(1024).withWidth(1024).build();
43+
var options = ImageOptionsBuilder.builder().height(1024).width(1024).build();
4444

4545
var instructions = """
4646
A light cream colored mini golden doodle with a sign that contains the message "I'm on my way to BARCADE!".""";

spring-ai-core/src/main/java/org/springframework/ai/image/ImageOptionsBuilder.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,85 @@ public static ImageOptionsBuilder builder() {
2828
return new ImageOptionsBuilder();
2929
}
3030

31+
public ImageOptionsBuilder N(Integer n) {
32+
this.options.setN(n);
33+
return this;
34+
}
35+
36+
public ImageOptionsBuilder model(String model) {
37+
this.options.setModel(model);
38+
return this;
39+
}
40+
41+
public ImageOptionsBuilder responseFormat(String responseFormat) {
42+
this.options.setResponseFormat(responseFormat);
43+
return this;
44+
}
45+
46+
public ImageOptionsBuilder width(Integer width) {
47+
this.options.setWidth(width);
48+
return this;
49+
}
50+
51+
public ImageOptionsBuilder height(Integer height) {
52+
this.options.setHeight(height);
53+
return this;
54+
}
55+
56+
public ImageOptionsBuilder style(String style) {
57+
this.options.setStyle(style);
58+
return this;
59+
}
60+
61+
/**
62+
* @deprecated use {@link #N(Integer)} instead.
63+
*/
64+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
3165
public ImageOptionsBuilder withN(Integer n) {
3266
this.options.setN(n);
3367
return this;
3468
}
3569

70+
/**
71+
* @deprecated use {@link #model(String)} instead.
72+
*/
73+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
3674
public ImageOptionsBuilder withModel(String model) {
3775
this.options.setModel(model);
3876
return this;
3977
}
4078

79+
/**
80+
* @deprecated use {@link #responseFormat(String)} instead.
81+
*/
82+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
4183
public ImageOptionsBuilder withResponseFormat(String responseFormat) {
4284
this.options.setResponseFormat(responseFormat);
4385
return this;
4486
}
4587

88+
/**
89+
* @deprecated use {@link #width(Integer)} instead.
90+
*/
91+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
4692
public ImageOptionsBuilder withWidth(Integer width) {
4793
this.options.setWidth(width);
4894
return this;
4995
}
5096

97+
/**
98+
* @deprecated use {@link #height(Integer)} instead.
99+
*/
100+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
51101
public ImageOptionsBuilder withHeight(Integer height) {
52102
this.options.setHeight(height);
53103
return this;
54104
}
55105

106+
/**
107+
* @deprecated use {@link #style(String)} instead.
108+
*/
109+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
56110
public ImageOptionsBuilder withStyle(String style) {
57111
this.options.setStyle(style);
58112
return this;

spring-ai-core/src/test/java/org/springframework/ai/image/observation/DefaultImageModelObservationConventionTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void contextualNameWhenModelIsDefined() {
4646
ImageModelObservationContext observationContext = ImageModelObservationContext.builder()
4747
.imagePrompt(generateImagePrompt())
4848
.provider("superprovider")
49-
.requestOptions(ImageOptionsBuilder.builder().withModel("mistral").build())
49+
.requestOptions(ImageOptionsBuilder.builder().model("mistral").build())
5050
.build();
5151
assertThat(this.observationConvention.getContextualName(observationContext)).isEqualTo("image mistral");
5252
}
@@ -66,7 +66,7 @@ void supportsOnlyImageModelObservationContext() {
6666
ImageModelObservationContext observationContext = ImageModelObservationContext.builder()
6767
.imagePrompt(generateImagePrompt())
6868
.provider("superprovider")
69-
.requestOptions(ImageOptionsBuilder.builder().withModel("mistral").build())
69+
.requestOptions(ImageOptionsBuilder.builder().model("mistral").build())
7070
.build();
7171
assertThat(this.observationConvention.supportsContext(observationContext)).isTrue();
7272
assertThat(this.observationConvention.supportsContext(new Observation.Context())).isFalse();
@@ -77,7 +77,7 @@ void shouldHaveLowCardinalityKeyValuesWhenDefined() {
7777
ImageModelObservationContext observationContext = ImageModelObservationContext.builder()
7878
.imagePrompt(generateImagePrompt())
7979
.provider("superprovider")
80-
.requestOptions(ImageOptionsBuilder.builder().withModel("mistral").build())
80+
.requestOptions(ImageOptionsBuilder.builder().model("mistral").build())
8181
.build();
8282
assertThat(this.observationConvention.getLowCardinalityKeyValues(observationContext)).contains(
8383
KeyValue.of(AiObservationAttributes.AI_OPERATION_TYPE.value(), "image"),
@@ -91,12 +91,12 @@ void shouldHaveHighCardinalityKeyValuesWhenDefined() {
9191
.imagePrompt(generateImagePrompt())
9292
.provider("superprovider")
9393
.requestOptions(ImageOptionsBuilder.builder()
94-
.withModel("mistral")
95-
.withN(1)
96-
.withHeight(1080)
97-
.withWidth(1920)
98-
.withStyle("sketch")
99-
.withResponseFormat("base64")
94+
.model("mistral")
95+
.N(1)
96+
.height(1080)
97+
.width(1920)
98+
.style("sketch")
99+
.responseFormat("base64")
100100
.build())
101101
.build();
102102

spring-ai-core/src/test/java/org/springframework/ai/image/observation/ImageModelObservationContextTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void whenMandatoryRequestOptionsThenReturn() {
3636
var observationContext = ImageModelObservationContext.builder()
3737
.imagePrompt(generateImagePrompt())
3838
.provider("superprovider")
39-
.requestOptions(ImageOptionsBuilder.builder().withModel("supersun").build())
39+
.requestOptions(ImageOptionsBuilder.builder().model("supersun").build())
4040
.build();
4141

4242
assertThat(observationContext).isNotNull();

spring-ai-core/src/test/java/org/springframework/ai/image/observation/ImageModelPromptContentObservationFilterTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void whenEmptyPromptThenReturnOriginalContext() {
5151
var expectedContext = ImageModelObservationContext.builder()
5252
.imagePrompt(new ImagePrompt(""))
5353
.provider("superprovider")
54-
.requestOptions(ImageOptionsBuilder.builder().withModel("mistral").build())
54+
.requestOptions(ImageOptionsBuilder.builder().model("mistral").build())
5555
.build();
5656
var actualContext = this.observationFilter.map(expectedContext);
5757

@@ -63,7 +63,7 @@ void whenPromptWithTextThenAugmentContext() {
6363
var originalContext = ImageModelObservationContext.builder()
6464
.imagePrompt(new ImagePrompt("supercalifragilisticexpialidocious"))
6565
.provider("superprovider")
66-
.requestOptions(ImageOptionsBuilder.builder().withModel("mistral").build())
66+
.requestOptions(ImageOptionsBuilder.builder().model("mistral").build())
6767
.build();
6868
var augmentedContext = this.observationFilter.map(originalContext);
6969

@@ -77,7 +77,7 @@ void whenPromptWithMessagesThenAugmentContext() {
7777
.imagePrompt(new ImagePrompt(List.of(new ImageMessage("you're a chimney sweep"),
7878
new ImageMessage("supercalifragilisticexpialidocious"))))
7979
.provider("superprovider")
80-
.requestOptions(ImageOptionsBuilder.builder().withModel("mistral").build())
80+
.requestOptions(ImageOptionsBuilder.builder().model("mistral").build())
8181
.build();
8282
var augmentedContext = this.observationFilter.map(originalContext);
8383

0 commit comments

Comments
 (0)