26
26
import reactor .core .publisher .Flux ;
27
27
import reactor .core .publisher .Mono ;
28
28
29
+ import org .springframework .ai .model .ApiKey ;
30
+ import org .springframework .ai .model .NoopApiKey ;
31
+ import org .springframework .ai .model .SimpleApiKey ;
29
32
import org .springframework .ai .openai .api .common .OpenAiApiConstants ;
30
33
import org .springframework .ai .retry .RetryUtils ;
31
34
import org .springframework .core .io .ByteArrayResource ;
45
48
* <a href="https://platform.openai.com/docs/api-reference/audio">OpenAI Audio</a>
46
49
*
47
50
* @author Christian Tzolov
51
+ * @author Ilayaperumal Gopinathan
48
52
* @since 0.8.1
49
53
*/
50
54
public class OpenAiAudioApi {
@@ -56,7 +60,9 @@ public class OpenAiAudioApi {
56
60
/**
57
61
* Create a new audio api.
58
62
* @param openAiToken OpenAI apiKey.
63
+ * @deprecated use {@link Builder} instead.
59
64
*/
65
+ @ Deprecated (forRemoval = true , since = "1.0.0-M6" )
60
66
public OpenAiAudioApi (String openAiToken ) {
61
67
this (OpenAiApiConstants .DEFAULT_BASE_URL , openAiToken , RestClient .builder (), WebClient .builder (),
62
68
RetryUtils .DEFAULT_RESPONSE_ERROR_HANDLER );
@@ -68,10 +74,11 @@ public OpenAiAudioApi(String openAiToken) {
68
74
* @param openAiToken OpenAI apiKey.
69
75
* @param restClientBuilder RestClient builder.
70
76
* @param responseErrorHandler Response error handler.
77
+ * @deprecated use {@link Builder} instead.
71
78
*/
79
+ @ Deprecated (forRemoval = true , since = "1.0.0-M6" )
72
80
public OpenAiAudioApi (String baseUrl , String openAiToken , RestClient .Builder restClientBuilder ,
73
81
ResponseErrorHandler responseErrorHandler ) {
74
-
75
82
Consumer <HttpHeaders > authHeaders ;
76
83
if (openAiToken != null && !openAiToken .isEmpty ()) {
77
84
authHeaders = h -> h .setBearerAuth (openAiToken );
@@ -96,7 +103,9 @@ public OpenAiAudioApi(String baseUrl, String openAiToken, RestClient.Builder res
96
103
* @param restClientBuilder RestClient builder.
97
104
* @param webClientBuilder WebClient builder.
98
105
* @param responseErrorHandler Response error handler.
106
+ * @deprecated use {@link Builder} instead.
99
107
*/
108
+ @ Deprecated (forRemoval = true , since = "1.0.0-M6" )
100
109
public OpenAiAudioApi (String baseUrl , String apiKey , RestClient .Builder restClientBuilder ,
101
110
WebClient .Builder webClientBuilder , ResponseErrorHandler responseErrorHandler ) {
102
111
@@ -112,14 +121,31 @@ public OpenAiAudioApi(String baseUrl, String apiKey, RestClient.Builder restClie
112
121
* @param restClientBuilder RestClient builder.
113
122
* @param webClientBuilder WebClient builder.
114
123
* @param responseErrorHandler Response error handler.
124
+ * @deprecated use {@link Builder} instead.
115
125
*/
126
+ @ Deprecated (forRemoval = true , since = "1.0.0-M6" )
116
127
public OpenAiAudioApi (String baseUrl , String apiKey , MultiValueMap <String , String > headers ,
117
128
RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
118
129
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 ) {
119
145
120
146
Consumer <HttpHeaders > authHeaders = h -> {
121
- if (apiKey != null && ! apiKey . isEmpty ( )) {
122
- h .setBearerAuth (apiKey );
147
+ if (!( apiKey instanceof NoopApiKey )) {
148
+ h .setBearerAuth (apiKey . getValue () );
123
149
}
124
150
h .addAll (headers );
125
151
// h.setContentType(MediaType.APPLICATION_JSON);
@@ -133,6 +159,10 @@ public OpenAiAudioApi(String baseUrl, String apiKey, MultiValueMap<String, Strin
133
159
this .webClient = webClientBuilder .baseUrl (baseUrl ).defaultHeaders (authHeaders ).build ();
134
160
}
135
161
162
+ public static Builder builder () {
163
+ return new Builder ();
164
+ }
165
+
136
166
/**
137
167
* Request to generates audio from the input text.
138
168
* @param requestBody The request body.
@@ -932,4 +962,71 @@ public record Segment(
932
962
933
963
}
934
964
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
+
935
1032
}
0 commit comments