Skip to content

Commit 1c93ae5

Browse files
ThomasVitaletzolov
authored andcommitted
OpenAI: Fix model property being overwritten by default value
Fixes gh-671 Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
1 parent f91ccf0 commit 1c93ae5

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiImageOptions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.fasterxml.jackson.annotation.JsonProperty;
2020

2121
import org.springframework.ai.image.ImageOptions;
22-
import org.springframework.ai.openai.api.OpenAiImageApi;
2322

2423
import java.util.Objects;
2524

@@ -44,7 +43,7 @@ public class OpenAiImageOptions implements ImageOptions {
4443
* The model to use for image generation.
4544
*/
4645
@JsonProperty("model")
47-
private String model = OpenAiImageApi.DEFAULT_IMAGE_MODEL;
46+
private String model;
4847

4948
/**
5049
* The width of the generated images. Must be one of 256, 512, or 1024 for dall-e-2.

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/openai/OpenAiImageProperties.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.ai.autoconfigure.openai;
1717

1818
import org.springframework.ai.openai.OpenAiImageOptions;
19+
import org.springframework.ai.openai.api.OpenAiImageApi;
1920
import org.springframework.boot.context.properties.ConfigurationProperties;
2021
import org.springframework.boot.context.properties.NestedConfigurationProperty;
2122

@@ -30,6 +31,8 @@ public class OpenAiImageProperties extends OpenAiParentProperties {
3031

3132
public static final String CONFIG_PREFIX = "spring.ai.openai.image";
3233

34+
public static final String DEFAULT_IMAGE_MODEL = OpenAiImageApi.ImageModel.DALL_E_3.getValue();
35+
3336
/**
3437
* Enable OpenAI Image client.
3538
*/
@@ -39,7 +42,7 @@ public class OpenAiImageProperties extends OpenAiParentProperties {
3942
* Options for OpenAI Image API.
4043
*/
4144
@NestedConfigurationProperty
42-
private OpenAiImageOptions options = OpenAiImageOptions.builder().build();
45+
private OpenAiImageOptions options = OpenAiImageOptions.builder().withModel(DEFAULT_IMAGE_MODEL).build();
4346

4447
public OpenAiImageOptions getOptions() {
4548
return options;

spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/OpenAiAutoConfigurationIT.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,19 @@ void generateImage() {
144144
});
145145
}
146146

147+
@Test
148+
void generateImageWithModel() {
149+
// The 256x256 size is supported by dall-e-2, but not by dall-e-3.
150+
contextRunner
151+
.withPropertyValues("spring.ai.openai.image.options.model=dall-e-2",
152+
"spring.ai.openai.image.options.size=256x256")
153+
.run(context -> {
154+
OpenAiImageClient client = context.getBean(OpenAiImageClient.class);
155+
ImageResponse imageResponse = client.call(new ImagePrompt("forest"));
156+
assertThat(imageResponse.getResults()).hasSize(1);
157+
assertThat(imageResponse.getResult().getOutput().getUrl()).isNotEmpty();
158+
logger.info("Generated image: " + imageResponse.getResult().getOutput().getUrl());
159+
});
160+
}
161+
147162
}

0 commit comments

Comments
 (0)