Skip to content

Commit 7739e1e

Browse files
committed
OpenAiImageModel support configure imagesPath
Signed-off-by: lambochen <[email protected]>
1 parent 30eb3ce commit 7739e1e

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

auto-configurations/models/spring-ai-autoconfigure-model-openai/src/main/java/org/springframework/ai/model/openai/autoconfigure/OpenAiImageAutoConfiguration.java

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Stefan Vassilev
5050
* @author Thomas Vitale
5151
* @author Ilayaperumal Gopinathan
52+
* @author lambochen
5253
*/
5354
@AutoConfiguration(after = { RestClientAutoConfiguration.class, WebClientAutoConfiguration.class,
5455
SpringAiRetryAutoConfiguration.class })
@@ -75,6 +76,7 @@ public OpenAiImageModel openAiImageModel(OpenAiConnectionProperties commonProper
7576
.baseUrl(resolved.baseUrl())
7677
.apiKey(new SimpleApiKey(resolved.apiKey()))
7778
.headers(resolved.headers())
79+
.imagesPath(imageProperties.getImagesPath())
7880
.restClientBuilder(restClientBuilderProvider.getIfAvailable(RestClient::builder))
7981
.responseErrorHandler(responseErrorHandler)
8082
.build();

auto-configurations/models/spring-ai-autoconfigure-model-openai/src/main/java/org/springframework/ai/model/openai/autoconfigure/OpenAiImageProperties.java

+13
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@
2525
* OpenAI Image autoconfiguration properties.
2626
*
2727
* @author Thomas Vitale
28+
* @author lambochen
2829
* @since 0.8.0
2930
*/
3031
@ConfigurationProperties(OpenAiImageProperties.CONFIG_PREFIX)
3132
public class OpenAiImageProperties extends OpenAiParentProperties {
3233

3334
public static final String CONFIG_PREFIX = "spring.ai.openai.image";
3435

36+
public static final String DEFAULT_IMAGES_PATH = "v1/images/generations";
37+
38+
private String imagesPath = DEFAULT_IMAGES_PATH;
39+
3540
public static final String DEFAULT_IMAGE_MODEL = OpenAiImageApi.ImageModel.DALL_E_3.getValue();
3641

3742
/**
@@ -48,4 +53,12 @@ public void setOptions(OpenAiImageOptions options) {
4853
this.options = options;
4954
}
5055

56+
public String getImagesPath() {
57+
return imagesPath;
58+
}
59+
60+
public void setImagesPath(String imagesPath) {
61+
this.imagesPath = imagesPath;
62+
}
63+
5164
}

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,26 @@
3939
* OpenAI Image API.
4040
*
4141
* @see <a href= "https://platform.openai.com/docs/api-reference/images">Images</a>
42+
* @author lambochen
4243
*/
4344
public class OpenAiImageApi {
4445

4546
public static final String DEFAULT_IMAGE_MODEL = ImageModel.DALL_E_3.getValue();
4647

4748
private final RestClient restClient;
4849

50+
private final String imagesPath;
51+
4952
/**
5053
* Create a new OpenAI Image API with the provided base URL.
5154
* @param baseUrl the base URL for the OpenAI API.
5255
* @param apiKey OpenAI apiKey.
5356
* @param headers the http headers to use.
57+
* @param imagesPath the images path to use.
5458
* @param restClientBuilder the rest client builder to use.
5559
* @param responseErrorHandler the response error handler to use.
5660
*/
57-
public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers,
61+
public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers, String imagesPath,
5862
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
5963

6064
// @formatter:off
@@ -69,14 +73,16 @@ public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, Strin
6973
.defaultStatusHandler(responseErrorHandler)
7074
.build();
7175
// @formatter:on
76+
77+
this.imagesPath = imagesPath;
7278
}
7379

7480
public ResponseEntity<OpenAiImageResponse> createImage(OpenAiImageRequest openAiImageRequest) {
7581
Assert.notNull(openAiImageRequest, "Image request cannot be null.");
7682
Assert.hasLength(openAiImageRequest.prompt(), "Prompt cannot be empty.");
7783

7884
return this.restClient.post()
79-
.uri("v1/images/generations")
85+
.uri(this.imagesPath)
8086
.body(openAiImageRequest)
8187
.retrieve()
8288
.toEntity(OpenAiImageResponse.class);
@@ -163,12 +169,20 @@ public static class Builder {
163169

164170
private ResponseErrorHandler responseErrorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER;
165171

172+
private String imagesPath = "v1/images/generations";
173+
166174
public Builder baseUrl(String baseUrl) {
167175
Assert.hasText(baseUrl, "baseUrl cannot be null or empty");
168176
this.baseUrl = baseUrl;
169177
return this;
170178
}
171179

180+
public Builder imagesPath(String imagesPath) {
181+
Assert.hasText(imagesPath, "imagesPath cannot be null or empty");
182+
this.imagesPath = imagesPath;
183+
return this;
184+
}
185+
172186
public Builder apiKey(ApiKey apiKey) {
173187
Assert.notNull(apiKey, "apiKey cannot be null");
174188
this.apiKey = apiKey;
@@ -201,7 +215,7 @@ public Builder responseErrorHandler(ResponseErrorHandler responseErrorHandler) {
201215

202216
public OpenAiImageApi build() {
203217
Assert.notNull(this.apiKey, "apiKey must be set");
204-
return new OpenAiImageApi(this.baseUrl, this.apiKey, this.headers, this.restClientBuilder,
218+
return new OpenAiImageApi(this.baseUrl, this.apiKey, this.headers, this.imagesPath, this.restClientBuilder,
205219
this.responseErrorHandler);
206220
}
207221

0 commit comments

Comments
 (0)