39
39
* OpenAI Image API.
40
40
*
41
41
* @see <a href= "https://platform.openai.com/docs/api-reference/images">Images</a>
42
+ * @author lambochen
42
43
*/
43
44
public class OpenAiImageApi {
44
45
45
46
public static final String DEFAULT_IMAGE_MODEL = ImageModel .DALL_E_3 .getValue ();
46
47
47
48
private final RestClient restClient ;
48
49
50
+ private final String imagesPath ;
51
+
49
52
/**
50
53
* Create a new OpenAI Image API with the provided base URL.
51
54
* @param baseUrl the base URL for the OpenAI API.
52
55
* @param apiKey OpenAI apiKey.
53
56
* @param headers the http headers to use.
57
+ * @param imagesPath the images path to use.
54
58
* @param restClientBuilder the rest client builder to use.
55
59
* @param responseErrorHandler the response error handler to use.
56
60
*/
57
- public OpenAiImageApi (String baseUrl , ApiKey apiKey , MultiValueMap <String , String > headers ,
61
+ public OpenAiImageApi (String baseUrl , ApiKey apiKey , MultiValueMap <String , String > headers , String imagesPath ,
58
62
RestClient .Builder restClientBuilder , ResponseErrorHandler responseErrorHandler ) {
59
63
60
64
// @formatter:off
@@ -69,14 +73,16 @@ public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, Strin
69
73
.defaultStatusHandler (responseErrorHandler )
70
74
.build ();
71
75
// @formatter:on
76
+
77
+ this .imagesPath = imagesPath ;
72
78
}
73
79
74
80
public ResponseEntity <OpenAiImageResponse > createImage (OpenAiImageRequest openAiImageRequest ) {
75
81
Assert .notNull (openAiImageRequest , "Image request cannot be null." );
76
82
Assert .hasLength (openAiImageRequest .prompt (), "Prompt cannot be empty." );
77
83
78
84
return this .restClient .post ()
79
- .uri ("v1/images/generations" )
85
+ .uri (this . imagesPath )
80
86
.body (openAiImageRequest )
81
87
.retrieve ()
82
88
.toEntity (OpenAiImageResponse .class );
@@ -163,12 +169,20 @@ public static class Builder {
163
169
164
170
private ResponseErrorHandler responseErrorHandler = RetryUtils .DEFAULT_RESPONSE_ERROR_HANDLER ;
165
171
172
+ private String imagesPath = "v1/images/generations" ;
173
+
166
174
public Builder baseUrl (String baseUrl ) {
167
175
Assert .hasText (baseUrl , "baseUrl cannot be null or empty" );
168
176
this .baseUrl = baseUrl ;
169
177
return this ;
170
178
}
171
179
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
+
172
186
public Builder apiKey (ApiKey apiKey ) {
173
187
Assert .notNull (apiKey , "apiKey cannot be null" );
174
188
this .apiKey = apiKey ;
@@ -201,7 +215,7 @@ public Builder responseErrorHandler(ResponseErrorHandler responseErrorHandler) {
201
215
202
216
public OpenAiImageApi build () {
203
217
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 ,
205
219
this .responseErrorHandler );
206
220
}
207
221
0 commit comments