Skip to content

Commit cb3e02b

Browse files
committed
Add outputFormat and outputCompression to OpenAiSdkImageOptions
Add support for outputFormat and outputCompression parameters in image generation using the OpenAI SDK. These options allow users to specify the output format (png, jpeg, webp) and compression level for generated images. Changes: * Add outputFormat and outputCompression fields to OpenAiSdkImageOptions * Add getters, setters, and builder methods for the new fields * Update equals, hashCode, and toString methods * Update toOpenAiImageGenerateParams to include the new parameters * Update Builder.from() and Builder.merge() to handle new fields * Add test coverage for the new properties Signed-off-by: Igor Bari <mucsi96@gmail.com>
1 parent bf5ebce commit cb3e02b

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImagePropertiesTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ public void imageOptionsTest() {
102102
"spring.ai.openai-sdk.image.options.responseFormat=url",
103103
"spring.ai.openai-sdk.image.options.size=1024x1792",
104104
"spring.ai.openai-sdk.image.options.style=vivid",
105-
"spring.ai.openai-sdk.image.options.user=userXYZ"
105+
"spring.ai.openai-sdk.image.options.user=userXYZ",
106+
"spring.ai.openai-sdk.image.options.outputFormat=jpeg",
107+
"spring.ai.openai-sdk.image.options.outputCompression=75"
106108
)
107109
// @formatter:on
108110
.withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class))
@@ -122,6 +124,8 @@ public void imageOptionsTest() {
122124
assertThat(imageProperties.getOptions().getSize()).isEqualTo("1024x1792");
123125
assertThat(imageProperties.getOptions().getStyle()).isEqualTo("vivid");
124126
assertThat(imageProperties.getOptions().getUser()).isEqualTo("userXYZ");
127+
assertThat(imageProperties.getOptions().getOutputFormat()).isEqualTo("jpeg");
128+
assertThat(imageProperties.getOptions().getOutputCompression()).isEqualTo(75);
125129
});
126130
}
127131

models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ public class OpenAiSdkImageOptions extends AbstractOpenAiSdkOptions implements I
8282
*/
8383
private String user;
8484

85+
/**
86+
* The output format of the generated images. Must be one of png, jpeg, or webp.
87+
*/
88+
private String outputFormat;
89+
90+
/**
91+
* The compression level (0-100) for lossy formats like jpeg and webp. Only applies
92+
* when outputFormat is jpeg or webp.
93+
*/
94+
private Integer outputCompression;
95+
8596
public static Builder builder() {
8697
return new Builder();
8798
}
@@ -160,6 +171,22 @@ public void setStyle(String style) {
160171
this.style = style;
161172
}
162173

174+
public String getOutputFormat() {
175+
return this.outputFormat;
176+
}
177+
178+
public void setOutputFormat(String outputFormat) {
179+
this.outputFormat = outputFormat;
180+
}
181+
182+
public Integer getOutputCompression() {
183+
return this.outputCompression;
184+
}
185+
186+
public void setOutputCompression(Integer outputCompression) {
187+
this.outputCompression = outputCompression;
188+
}
189+
163190
@Override
164191
public boolean equals(Object o) {
165192
if (o == null || getClass() != o.getClass()) {
@@ -169,20 +196,23 @@ public boolean equals(Object o) {
169196
return Objects.equals(this.n, that.n) && Objects.equals(this.width, that.width)
170197
&& Objects.equals(this.height, that.height) && Objects.equals(this.quality, that.quality)
171198
&& Objects.equals(this.responseFormat, that.responseFormat) && Objects.equals(this.size, that.size)
172-
&& Objects.equals(this.style, that.style) && Objects.equals(this.user, that.user);
199+
&& Objects.equals(this.style, that.style) && Objects.equals(this.user, that.user)
200+
&& Objects.equals(this.outputFormat, that.outputFormat)
201+
&& Objects.equals(this.outputCompression, that.outputCompression);
173202
}
174203

175204
@Override
176205
public int hashCode() {
177206
return Objects.hash(this.n, this.width, this.height, this.quality, this.responseFormat, this.size, this.style,
178-
this.user);
207+
this.user, this.outputFormat, this.outputCompression);
179208
}
180209

181210
@Override
182211
public String toString() {
183212
return "OpenAiSdkImageOptions{" + "n=" + this.n + ", width=" + this.width + ", height=" + this.height
184213
+ ", quality='" + this.quality + '\'' + ", responseFormat='" + this.responseFormat + '\'' + ", size='"
185-
+ this.size + '\'' + ", style='" + this.style + '\'' + ", user='" + this.user + '\'' + '}';
214+
+ this.size + '\'' + ", style='" + this.style + '\'' + ", user='" + this.user + '\''
215+
+ ", outputFormat='" + this.outputFormat + '\'' + ", outputCompression=" + this.outputCompression + '}';
186216
}
187217

188218
public ImageGenerateParams toOpenAiImageGenerateParams(ImagePrompt imagePrompt) {
@@ -220,6 +250,12 @@ else if (this.getModel() != null) {
220250
if (this.getUser() != null) {
221251
builder.user(this.getUser());
222252
}
253+
if (this.getOutputFormat() != null) {
254+
builder.outputFormat(ImageGenerateParams.OutputFormat.of(this.getOutputFormat().toLowerCase()));
255+
}
256+
if (this.getOutputCompression() != null) {
257+
builder.outputCompression(this.getOutputCompression().longValue());
258+
}
223259

224260
return builder.build();
225261
}
@@ -256,6 +292,8 @@ public Builder from(OpenAiSdkImageOptions fromOptions) {
256292
this.options.setSize(fromOptions.getSize());
257293
this.options.setStyle(fromOptions.getStyle());
258294
this.options.setUser(fromOptions.getUser());
295+
this.options.setOutputFormat(fromOptions.getOutputFormat());
296+
this.options.setOutputCompression(fromOptions.getOutputCompression());
259297
return this;
260298
}
261299

@@ -322,6 +360,12 @@ public Builder merge(ImageOptions from) {
322360
if (castFrom.getUser() != null) {
323361
this.options.setUser(castFrom.getUser());
324362
}
363+
if (castFrom.getOutputFormat() != null) {
364+
this.options.setOutputFormat(castFrom.getOutputFormat());
365+
}
366+
if (castFrom.getOutputCompression() != null) {
367+
this.options.setOutputCompression(castFrom.getOutputCompression());
368+
}
325369
}
326370
return this;
327371
}
@@ -421,6 +465,16 @@ public Builder style(String style) {
421465
return this;
422466
}
423467

468+
public Builder outputFormat(String outputFormat) {
469+
this.options.setOutputFormat(outputFormat);
470+
return this;
471+
}
472+
473+
public Builder outputCompression(Integer outputCompression) {
474+
this.options.setOutputCompression(outputCompression);
475+
return this;
476+
}
477+
424478
public OpenAiSdkImageOptions build() {
425479
return this.options;
426480
}

0 commit comments

Comments
 (0)