Skip to content

Commit c0d7633

Browse files
lambochennamsoo2
authored andcommitted
OpenAiImageModel support configure imagesPath
Signed-off-by: lambochen <[email protected]> Signed-off-by: minsoo.nam <[email protected]>
1 parent 3e92689 commit c0d7633

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,26 @@
4141
* OpenAI Image API.
4242
*
4343
* @see <a href= "https://platform.openai.com/docs/api-reference/images">Images</a>
44+
* @author lambochen
4445
*/
4546
public class OpenAiImageApi {
4647

4748
public static final String DEFAULT_IMAGE_MODEL = ImageModel.DALL_E_3.getValue();
4849

4950
private final RestClient restClient;
5051

52+
private final String imagesPath;
53+
5154
/**
5255
* Create a new OpenAI Image API with the provided base URL.
5356
* @param baseUrl the base URL for the OpenAI API.
5457
* @param apiKey OpenAI apiKey.
5558
* @param headers the http headers to use.
59+
* @param imagesPath the images path to use.
5660
* @param restClientBuilder the rest client builder to use.
5761
* @param responseErrorHandler the response error handler to use.
5862
*/
59-
public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers,
63+
public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers, String imagesPath,
6064
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
6165

6266
// @formatter:off
@@ -70,14 +74,16 @@ public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, Strin
7074
.defaultStatusHandler(responseErrorHandler)
7175
.build();
7276
// @formatter:on
77+
78+
this.imagesPath = imagesPath;
7379
}
7480

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

7985
return this.restClient.post()
80-
.uri("v1/images/generations")
86+
.uri(this.imagesPath)
8187
.body(openAiImageRequest)
8288
.contentType(MediaType.APPLICATION_JSON)
8389
.retrieve()
@@ -233,12 +239,20 @@ public static class Builder {
233239

234240
private ResponseErrorHandler responseErrorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER;
235241

242+
private String imagesPath = "v1/images/generations";
243+
236244
public Builder baseUrl(String baseUrl) {
237245
Assert.hasText(baseUrl, "baseUrl cannot be null or empty");
238246
this.baseUrl = baseUrl;
239247
return this;
240248
}
241249

250+
public Builder imagesPath(String imagesPath) {
251+
Assert.hasText(imagesPath, "imagesPath cannot be null or empty");
252+
this.imagesPath = imagesPath;
253+
return this;
254+
}
255+
242256
public Builder apiKey(ApiKey apiKey) {
243257
Assert.notNull(apiKey, "apiKey cannot be null");
244258
this.apiKey = apiKey;
@@ -271,7 +285,7 @@ public Builder responseErrorHandler(ResponseErrorHandler responseErrorHandler) {
271285

272286
public OpenAiImageApi build() {
273287
Assert.notNull(this.apiKey, "apiKey must be set");
274-
return new OpenAiImageApi(this.baseUrl, this.apiKey, this.headers, this.restClientBuilder,
288+
return new OpenAiImageApi(this.baseUrl, this.apiKey, this.headers, this.imagesPath, this.restClientBuilder,
275289
this.responseErrorHandler);
276290
}
277291

0 commit comments

Comments
 (0)