OpenAiImageModel support configure imagesPath

Signed-off-by: lambochen <lambochen@yeah.net>
This commit is contained in:
lambochen
2025-05-12 17:12:05 +08:00
parent 30eb3ce3f2
commit 7739e1e54c
3 changed files with 32 additions and 3 deletions

View File

@@ -49,6 +49,7 @@ import static org.springframework.ai.model.openai.autoconfigure.OpenAIAutoConfig
* @author Stefan Vassilev
* @author Thomas Vitale
* @author Ilayaperumal Gopinathan
* @author lambochen
*/
@AutoConfiguration(after = { RestClientAutoConfiguration.class, WebClientAutoConfiguration.class,
SpringAiRetryAutoConfiguration.class })
@@ -75,6 +76,7 @@ public class OpenAiImageAutoConfiguration {
.baseUrl(resolved.baseUrl())
.apiKey(new SimpleApiKey(resolved.apiKey()))
.headers(resolved.headers())
.imagesPath(imageProperties.getImagesPath())
.restClientBuilder(restClientBuilderProvider.getIfAvailable(RestClient::builder))
.responseErrorHandler(responseErrorHandler)
.build();

View File

@@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.NestedConfigurationProperty;
* OpenAI Image autoconfiguration properties.
*
* @author Thomas Vitale
* @author lambochen
* @since 0.8.0
*/
@ConfigurationProperties(OpenAiImageProperties.CONFIG_PREFIX)
@@ -32,6 +33,10 @@ public class OpenAiImageProperties extends OpenAiParentProperties {
public static final String CONFIG_PREFIX = "spring.ai.openai.image";
public static final String DEFAULT_IMAGES_PATH = "v1/images/generations";
private String imagesPath = DEFAULT_IMAGES_PATH;
public static final String DEFAULT_IMAGE_MODEL = OpenAiImageApi.ImageModel.DALL_E_3.getValue();
/**
@@ -48,4 +53,12 @@ public class OpenAiImageProperties extends OpenAiParentProperties {
this.options = options;
}
public String getImagesPath() {
return imagesPath;
}
public void setImagesPath(String imagesPath) {
this.imagesPath = imagesPath;
}
}

View File

@@ -39,6 +39,7 @@ import org.springframework.web.client.RestClient;
* OpenAI Image API.
*
* @see <a href= "https://platform.openai.com/docs/api-reference/images">Images</a>
* @author lambochen
*/
public class OpenAiImageApi {
@@ -46,15 +47,18 @@ public class OpenAiImageApi {
private final RestClient restClient;
private final String imagesPath;
/**
* Create a new OpenAI Image API with the provided base URL.
* @param baseUrl the base URL for the OpenAI API.
* @param apiKey OpenAI apiKey.
* @param headers the http headers to use.
* @param imagesPath the images path to use.
* @param restClientBuilder the rest client builder to use.
* @param responseErrorHandler the response error handler to use.
*/
public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers,
public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers, String imagesPath,
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
// @formatter:off
@@ -69,6 +73,8 @@ public class OpenAiImageApi {
.defaultStatusHandler(responseErrorHandler)
.build();
// @formatter:on
this.imagesPath = imagesPath;
}
public ResponseEntity<OpenAiImageResponse> createImage(OpenAiImageRequest openAiImageRequest) {
@@ -76,7 +82,7 @@ public class OpenAiImageApi {
Assert.hasLength(openAiImageRequest.prompt(), "Prompt cannot be empty.");
return this.restClient.post()
.uri("v1/images/generations")
.uri(this.imagesPath)
.body(openAiImageRequest)
.retrieve()
.toEntity(OpenAiImageResponse.class);
@@ -163,12 +169,20 @@ public class OpenAiImageApi {
private ResponseErrorHandler responseErrorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER;
private String imagesPath = "v1/images/generations";
public Builder baseUrl(String baseUrl) {
Assert.hasText(baseUrl, "baseUrl cannot be null or empty");
this.baseUrl = baseUrl;
return this;
}
public Builder imagesPath(String imagesPath) {
Assert.hasText(imagesPath, "imagesPath cannot be null or empty");
this.imagesPath = imagesPath;
return this;
}
public Builder apiKey(ApiKey apiKey) {
Assert.notNull(apiKey, "apiKey cannot be null");
this.apiKey = apiKey;
@@ -201,7 +215,7 @@ public class OpenAiImageApi {
public OpenAiImageApi build() {
Assert.notNull(this.apiKey, "apiKey must be set");
return new OpenAiImageApi(this.baseUrl, this.apiKey, this.headers, this.restClientBuilder,
return new OpenAiImageApi(this.baseUrl, this.apiKey, this.headers, this.imagesPath, this.restClientBuilder,
this.responseErrorHandler);
}