31
31
import reactor .core .publisher .Flux ;
32
32
import reactor .core .publisher .Mono ;
33
33
34
+ import org .springframework .ai .model .ApiKey ;
34
35
import org .springframework .ai .model .ChatModelDescription ;
35
36
import org .springframework .ai .model .ModelOptionsUtils ;
37
+ import org .springframework .ai .model .SimpleApiKey ;
36
38
import org .springframework .ai .openai .api .common .OpenAiApiConstants ;
37
39
import org .springframework .ai .retry .RetryUtils ;
38
40
import org .springframework .core .ParameterizedTypeReference ;
62
64
*/
63
65
public class OpenAiApi {
64
66
67
+ public static Builder builder () {
68
+ return new Builder ();
69
+ }
70
+
65
71
public static final OpenAiApi .ChatModel DEFAULT_CHAT_MODEL = ChatModel .GPT_4_O ;
66
72
67
73
public static final String DEFAULT_EMBEDDING_MODEL = EmbeddingModel .TEXT_EMBEDDING_ADA_002 .getValue ();
@@ -81,7 +87,9 @@ public class OpenAiApi {
81
87
/**
82
88
* Create a new chat completion api with base URL set to https://api.openai.com
83
89
* @param apiKey OpenAI apiKey.
90
+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
84
91
*/
92
+ @ Deprecated (since = "1.0.0.M6" )
85
93
public OpenAiApi (String apiKey ) {
86
94
this (OpenAiApiConstants .DEFAULT_BASE_URL , apiKey );
87
95
}
@@ -90,7 +98,9 @@ public OpenAiApi(String apiKey) {
90
98
* Create a new chat completion api.
91
99
* @param baseUrl api base URL.
92
100
* @param apiKey OpenAI apiKey.
101
+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
93
102
*/
103
+ @ Deprecated (since = "1.0.0.M6" )
94
104
public OpenAiApi (String baseUrl , String apiKey ) {
95
105
this (baseUrl , apiKey , RestClient .builder (), WebClient .builder ());
96
106
}
@@ -101,7 +111,9 @@ public OpenAiApi(String baseUrl, String apiKey) {
101
111
* @param apiKey OpenAI apiKey.
102
112
* @param restClientBuilder RestClient builder.
103
113
* @param webClientBuilder WebClient builder.
114
+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
104
115
*/
116
+ @ Deprecated (since = "1.0.0.M6" )
105
117
public OpenAiApi (String baseUrl , String apiKey , RestClient .Builder restClientBuilder ,
106
118
WebClient .Builder webClientBuilder ) {
107
119
this (baseUrl , apiKey , restClientBuilder , webClientBuilder , RetryUtils .DEFAULT_RESPONSE_ERROR_HANDLER );
@@ -114,7 +126,9 @@ public OpenAiApi(String baseUrl, String apiKey, RestClient.Builder restClientBui
114
126
* @param restClientBuilder RestClient builder.
115
127
* @param webClientBuilder WebClient builder.
116
128
* @param responseErrorHandler Response error handler.
129
+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
117
130
*/
131
+ @ Deprecated (since = "1.0.0.M6" )
118
132
public OpenAiApi (String baseUrl , String apiKey , RestClient .Builder restClientBuilder ,
119
133
WebClient .Builder webClientBuilder , ResponseErrorHandler responseErrorHandler ) {
120
134
this (baseUrl , apiKey , "/v1/chat/completions" , "/v1/embeddings" , restClientBuilder , webClientBuilder ,
@@ -130,7 +144,9 @@ public OpenAiApi(String baseUrl, String apiKey, RestClient.Builder restClientBui
130
144
* @param restClientBuilder RestClient builder.
131
145
* @param webClientBuilder WebClient builder.
132
146
* @param responseErrorHandler Response error handler.
147
+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
133
148
*/
149
+ @ Deprecated (since = "1.0.0.M6" )
134
150
public OpenAiApi (String baseUrl , String apiKey , String completionsPath , String embeddingsPath ,
135
151
RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
136
152
ResponseErrorHandler responseErrorHandler ) {
@@ -149,10 +165,32 @@ public OpenAiApi(String baseUrl, String apiKey, String completionsPath, String e
149
165
* @param restClientBuilder RestClient builder.
150
166
* @param webClientBuilder WebClient builder.
151
167
* @param responseErrorHandler Response error handler.
168
+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
152
169
*/
170
+ @ Deprecated (since = "1.0.0.M6" )
153
171
public OpenAiApi (String baseUrl , String apiKey , MultiValueMap <String , String > headers , String completionsPath ,
154
172
String embeddingsPath , RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
155
173
ResponseErrorHandler responseErrorHandler ) {
174
+ this (baseUrl , new SimpleApiKey (apiKey ), headers , completionsPath , embeddingsPath , restClientBuilder ,
175
+ webClientBuilder , responseErrorHandler );
176
+ }
177
+
178
+ /**
179
+ * Create a new chat completion api.
180
+ * @param baseUrl api base URL.
181
+ * @param apiKey OpenAI apiKey.
182
+ * @param headers the http headers to use.
183
+ * @param completionsPath the path to the chat completions endpoint.
184
+ * @param embeddingsPath the path to the embeddings endpoint.
185
+ * @param restClientBuilder RestClient builder.
186
+ * @param webClientBuilder WebClient builder.
187
+ * @param responseErrorHandler Response error handler.
188
+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
189
+ */
190
+ @ Deprecated (since = "1.0.0.M6" )
191
+ public OpenAiApi (String baseUrl , ApiKey apiKey , MultiValueMap <String , String > headers , String completionsPath ,
192
+ String embeddingsPath , RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
193
+ ResponseErrorHandler responseErrorHandler ) {
156
194
157
195
Assert .hasText (completionsPath , "Completions Path must not be null" );
158
196
Assert .hasText (embeddingsPath , "Embeddings Path must not be null" );
@@ -162,7 +200,7 @@ public OpenAiApi(String baseUrl, String apiKey, MultiValueMap<String, String> he
162
200
this .embeddingsPath = embeddingsPath ;
163
201
// @formatter:off
164
202
Consumer <HttpHeaders > finalHeaders = h -> {
165
- h .setBearerAuth (apiKey );
203
+ h .setBearerAuth (apiKey . getValue () );
166
204
h .setContentType (MediaType .APPLICATION_JSON );
167
205
h .addAll (headers );
168
206
};
@@ -1607,4 +1645,78 @@ public record EmbeddingList<T>(// @formatter:off
1607
1645
@ JsonProperty ("usage" ) Usage usage ) { // @formatter:on
1608
1646
}
1609
1647
1648
+ public static class Builder {
1649
+
1650
+ private String baseUrl = OpenAiApiConstants .DEFAULT_BASE_URL ;
1651
+
1652
+ private ApiKey apiKey ;
1653
+
1654
+ private MultiValueMap <String , String > headers = new LinkedMultiValueMap <>();
1655
+
1656
+ private String completionsPath = "/v1/chat/completions" ;
1657
+
1658
+ private String embeddingsPath = "/v1/embeddings" ;
1659
+
1660
+ private RestClient .Builder restClientBuilder = RestClient .builder ();
1661
+
1662
+ private WebClient .Builder webClientBuilder = WebClient .builder ();
1663
+
1664
+ private ResponseErrorHandler responseErrorHandler = RetryUtils .DEFAULT_RESPONSE_ERROR_HANDLER ;
1665
+
1666
+ public Builder baseUrl (String baseUrl ) {
1667
+ Assert .hasText (baseUrl , "baseUrl cannot be null or empty" );
1668
+ this .baseUrl = baseUrl ;
1669
+ return this ;
1670
+ }
1671
+
1672
+ public Builder apiKey (ApiKey apiKey ) {
1673
+ Assert .notNull (apiKey , "apiKey cannot be null" );
1674
+ this .apiKey = apiKey ;
1675
+ return this ;
1676
+ }
1677
+
1678
+ public Builder headers (MultiValueMap <String , String > headers ) {
1679
+ Assert .notNull (headers , "headers cannot be null" );
1680
+ this .headers = headers ;
1681
+ return this ;
1682
+ }
1683
+
1684
+ public Builder completionsPath (String completionsPath ) {
1685
+ Assert .hasText (completionsPath , "completionsPath cannot be null or empty" );
1686
+ this .completionsPath = completionsPath ;
1687
+ return this ;
1688
+ }
1689
+
1690
+ public Builder embeddingsPath (String embeddingsPath ) {
1691
+ Assert .hasText (embeddingsPath , "embeddingsPath cannot be null or empty" );
1692
+ this .embeddingsPath = embeddingsPath ;
1693
+ return this ;
1694
+ }
1695
+
1696
+ public Builder restClientBuilder (RestClient .Builder restClientBuilder ) {
1697
+ Assert .notNull (restClientBuilder , "restClientBuilder cannot be null" );
1698
+ this .restClientBuilder = restClientBuilder ;
1699
+ return this ;
1700
+ }
1701
+
1702
+ public Builder webClientBuilder (WebClient .Builder webClientBuilder ) {
1703
+ Assert .notNull (webClientBuilder , "webClientBuilder cannot be null" );
1704
+ this .webClientBuilder = webClientBuilder ;
1705
+ return this ;
1706
+ }
1707
+
1708
+ public Builder responseErrorHandler (ResponseErrorHandler responseErrorHandler ) {
1709
+ Assert .notNull (responseErrorHandler , "responseErrorHandler cannot be null" );
1710
+ this .responseErrorHandler = responseErrorHandler ;
1711
+ return this ;
1712
+ }
1713
+
1714
+ public OpenAiApi build () {
1715
+ Assert .notNull (this .apiKey , "apiKey must be set" );
1716
+ return new OpenAiApi (this .baseUrl , this .apiKey , this .headers , this .completionsPath , this .embeddingsPath ,
1717
+ this .restClientBuilder , this .webClientBuilder , this .responseErrorHandler );
1718
+ }
1719
+
1720
+ }
1721
+
1610
1722
}
0 commit comments