2626import reactor .core .publisher .Flux ;
2727import reactor .core .publisher .Mono ;
2828
29+ import org .springframework .ai .model .ApiKey ;
30+ import org .springframework .ai .model .NoopApiKey ;
31+ import org .springframework .ai .model .SimpleApiKey ;
2932import org .springframework .ai .openai .api .common .OpenAiApiConstants ;
3033import org .springframework .ai .retry .RetryUtils ;
3134import org .springframework .core .io .ByteArrayResource ;
4548 * <a href="https://platform.openai.com/docs/api-reference/audio">OpenAI Audio</a>
4649 *
4750 * @author Christian Tzolov
51+ * @author Ilayaperumal Gopinathan
4852 * @since 0.8.1
4953 */
5054public class OpenAiAudioApi {
@@ -56,7 +60,9 @@ public class OpenAiAudioApi {
5660 /**
5761 * Create a new audio api.
5862 * @param openAiToken OpenAI apiKey.
63+ * @deprecated use {@link Builder} instead.
5964 */
65+ @ Deprecated (forRemoval = true , since = "1.0.0-M6" )
6066 public OpenAiAudioApi (String openAiToken ) {
6167 this (OpenAiApiConstants .DEFAULT_BASE_URL , openAiToken , RestClient .builder (), WebClient .builder (),
6268 RetryUtils .DEFAULT_RESPONSE_ERROR_HANDLER );
@@ -68,10 +74,11 @@ public OpenAiAudioApi(String openAiToken) {
6874 * @param openAiToken OpenAI apiKey.
6975 * @param restClientBuilder RestClient builder.
7076 * @param responseErrorHandler Response error handler.
77+ * @deprecated use {@link Builder} instead.
7178 */
79+ @ Deprecated (forRemoval = true , since = "1.0.0-M6" )
7280 public OpenAiAudioApi (String baseUrl , String openAiToken , RestClient .Builder restClientBuilder ,
7381 ResponseErrorHandler responseErrorHandler ) {
74-
7582 Consumer <HttpHeaders > authHeaders ;
7683 if (openAiToken != null && !openAiToken .isEmpty ()) {
7784 authHeaders = h -> h .setBearerAuth (openAiToken );
@@ -96,7 +103,9 @@ public OpenAiAudioApi(String baseUrl, String openAiToken, RestClient.Builder res
96103 * @param restClientBuilder RestClient builder.
97104 * @param webClientBuilder WebClient builder.
98105 * @param responseErrorHandler Response error handler.
106+ * @deprecated use {@link Builder} instead.
99107 */
108+ @ Deprecated (forRemoval = true , since = "1.0.0-M6" )
100109 public OpenAiAudioApi (String baseUrl , String apiKey , RestClient .Builder restClientBuilder ,
101110 WebClient .Builder webClientBuilder , ResponseErrorHandler responseErrorHandler ) {
102111
@@ -112,14 +121,31 @@ public OpenAiAudioApi(String baseUrl, String apiKey, RestClient.Builder restClie
112121 * @param restClientBuilder RestClient builder.
113122 * @param webClientBuilder WebClient builder.
114123 * @param responseErrorHandler Response error handler.
124+ * @deprecated use {@link Builder} instead.
115125 */
126+ @ Deprecated (forRemoval = true , since = "1.0.0-M6" )
116127 public OpenAiAudioApi (String baseUrl , String apiKey , MultiValueMap <String , String > headers ,
117128 RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
118129 ResponseErrorHandler responseErrorHandler ) {
130+ this (baseUrl , new SimpleApiKey (apiKey ), headers , restClientBuilder , webClientBuilder , responseErrorHandler );
131+ }
132+
133+ /**
134+ * Create a new audio api.
135+ * @param baseUrl api base URL.
136+ * @param apiKey OpenAI apiKey.
137+ * @param headers the http headers to use.
138+ * @param restClientBuilder RestClient builder.
139+ * @param webClientBuilder WebClient builder.
140+ * @param responseErrorHandler Response error handler.
141+ */
142+ public OpenAiAudioApi (String baseUrl , ApiKey apiKey , MultiValueMap <String , String > headers ,
143+ RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
144+ ResponseErrorHandler responseErrorHandler ) {
119145
120146 Consumer <HttpHeaders > authHeaders = h -> {
121- if (apiKey != null && ! apiKey . isEmpty ( )) {
122- h .setBearerAuth (apiKey );
147+ if (!( apiKey instanceof NoopApiKey )) {
148+ h .setBearerAuth (apiKey . getValue () );
123149 }
124150 h .addAll (headers );
125151 // h.setContentType(MediaType.APPLICATION_JSON);
@@ -133,6 +159,10 @@ public OpenAiAudioApi(String baseUrl, String apiKey, MultiValueMap<String, Strin
133159 this .webClient = webClientBuilder .baseUrl (baseUrl ).defaultHeaders (authHeaders ).build ();
134160 }
135161
162+ public static Builder builder () {
163+ return new Builder ();
164+ }
165+
136166 /**
137167 * Request to generates audio from the input text.
138168 * @param requestBody The request body.
@@ -932,4 +962,71 @@ public record Segment(
932962
933963 }
934964
965+ /**
966+ * Builder to construct {@link OpenAiAudioApi} instance.
967+ */
968+ public static class Builder {
969+
970+ private String baseUrl = OpenAiApiConstants .DEFAULT_BASE_URL ;
971+
972+ private ApiKey apiKey ;
973+
974+ private MultiValueMap <String , String > headers = new LinkedMultiValueMap <>();
975+
976+ private RestClient .Builder restClientBuilder = RestClient .builder ();
977+
978+ private WebClient .Builder webClientBuilder = WebClient .builder ();
979+
980+ private ResponseErrorHandler responseErrorHandler = RetryUtils .DEFAULT_RESPONSE_ERROR_HANDLER ;
981+
982+ public Builder baseUrl (String baseUrl ) {
983+ Assert .hasText (baseUrl , "baseUrl cannot be null or empty" );
984+ this .baseUrl = baseUrl ;
985+ return this ;
986+ }
987+
988+ public Builder apiKey (ApiKey apiKey ) {
989+ Assert .notNull (apiKey , "apiKey cannot be null" );
990+ this .apiKey = apiKey ;
991+ return this ;
992+ }
993+
994+ public Builder apiKey (String simpleApiKey ) {
995+ Assert .notNull (simpleApiKey , "simpleApiKey cannot be null" );
996+ this .apiKey = new SimpleApiKey (simpleApiKey );
997+ return this ;
998+ }
999+
1000+ public Builder headers (MultiValueMap <String , String > headers ) {
1001+ Assert .notNull (headers , "headers cannot be null" );
1002+ this .headers = headers ;
1003+ return this ;
1004+ }
1005+
1006+ public Builder restClientBuilder (RestClient .Builder restClientBuilder ) {
1007+ Assert .notNull (restClientBuilder , "restClientBuilder cannot be null" );
1008+ this .restClientBuilder = restClientBuilder ;
1009+ return this ;
1010+ }
1011+
1012+ public Builder webClientBuilder (WebClient .Builder webClientBuilder ) {
1013+ Assert .notNull (webClientBuilder , "webClientBuilder cannot be null" );
1014+ this .webClientBuilder = webClientBuilder ;
1015+ return this ;
1016+ }
1017+
1018+ public Builder responseErrorHandler (ResponseErrorHandler responseErrorHandler ) {
1019+ Assert .notNull (responseErrorHandler , "responseErrorHandler cannot be null" );
1020+ this .responseErrorHandler = responseErrorHandler ;
1021+ return this ;
1022+ }
1023+
1024+ public OpenAiAudioApi build () {
1025+ Assert .notNull (this .apiKey , "apiKey must be set" );
1026+ return new OpenAiAudioApi (this .baseUrl , this .apiKey , this .headers , this .restClientBuilder ,
1027+ this .webClientBuilder , this .responseErrorHandler );
1028+ }
1029+
1030+ }
1031+
9351032}
0 commit comments