3131import reactor .core .publisher .Flux ;
3232import reactor .core .publisher .Mono ;
3333
34+ import org .springframework .ai .model .ApiKey ;
3435import org .springframework .ai .model .ChatModelDescription ;
3536import org .springframework .ai .model .ModelOptionsUtils ;
37+ import org .springframework .ai .model .SimpleApiKey ;
3638import org .springframework .ai .openai .api .common .OpenAiApiConstants ;
3739import org .springframework .ai .retry .RetryUtils ;
3840import org .springframework .core .ParameterizedTypeReference ;
6264 */
6365public class OpenAiApi {
6466
67+ public static Builder builder () {
68+ return new Builder ();
69+ }
70+
6571 public static final OpenAiApi .ChatModel DEFAULT_CHAT_MODEL = ChatModel .GPT_4_O ;
6672
6773 public static final String DEFAULT_EMBEDDING_MODEL = EmbeddingModel .TEXT_EMBEDDING_ADA_002 .getValue ();
@@ -81,7 +87,9 @@ public class OpenAiApi {
8187 /**
8288 * Create a new chat completion api with base URL set to https://api.openai.com
8389 * @param apiKey OpenAI apiKey.
90+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
8491 */
92+ @ Deprecated (since = "1.0.0.M6" )
8593 public OpenAiApi (String apiKey ) {
8694 this (OpenAiApiConstants .DEFAULT_BASE_URL , apiKey );
8795 }
@@ -90,7 +98,9 @@ public OpenAiApi(String apiKey) {
9098 * Create a new chat completion api.
9199 * @param baseUrl api base URL.
92100 * @param apiKey OpenAI apiKey.
101+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
93102 */
103+ @ Deprecated (since = "1.0.0.M6" )
94104 public OpenAiApi (String baseUrl , String apiKey ) {
95105 this (baseUrl , apiKey , RestClient .builder (), WebClient .builder ());
96106 }
@@ -101,7 +111,9 @@ public OpenAiApi(String baseUrl, String apiKey) {
101111 * @param apiKey OpenAI apiKey.
102112 * @param restClientBuilder RestClient builder.
103113 * @param webClientBuilder WebClient builder.
114+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
104115 */
116+ @ Deprecated (since = "1.0.0.M6" )
105117 public OpenAiApi (String baseUrl , String apiKey , RestClient .Builder restClientBuilder ,
106118 WebClient .Builder webClientBuilder ) {
107119 this (baseUrl , apiKey , restClientBuilder , webClientBuilder , RetryUtils .DEFAULT_RESPONSE_ERROR_HANDLER );
@@ -114,7 +126,9 @@ public OpenAiApi(String baseUrl, String apiKey, RestClient.Builder restClientBui
114126 * @param restClientBuilder RestClient builder.
115127 * @param webClientBuilder WebClient builder.
116128 * @param responseErrorHandler Response error handler.
129+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
117130 */
131+ @ Deprecated (since = "1.0.0.M6" )
118132 public OpenAiApi (String baseUrl , String apiKey , RestClient .Builder restClientBuilder ,
119133 WebClient .Builder webClientBuilder , ResponseErrorHandler responseErrorHandler ) {
120134 this (baseUrl , apiKey , "/v1/chat/completions" , "/v1/embeddings" , restClientBuilder , webClientBuilder ,
@@ -130,7 +144,9 @@ public OpenAiApi(String baseUrl, String apiKey, RestClient.Builder restClientBui
130144 * @param restClientBuilder RestClient builder.
131145 * @param webClientBuilder WebClient builder.
132146 * @param responseErrorHandler Response error handler.
147+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
133148 */
149+ @ Deprecated (since = "1.0.0.M6" )
134150 public OpenAiApi (String baseUrl , String apiKey , String completionsPath , String embeddingsPath ,
135151 RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
136152 ResponseErrorHandler responseErrorHandler ) {
@@ -149,10 +165,32 @@ public OpenAiApi(String baseUrl, String apiKey, String completionsPath, String e
149165 * @param restClientBuilder RestClient builder.
150166 * @param webClientBuilder WebClient builder.
151167 * @param responseErrorHandler Response error handler.
168+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
152169 */
170+ @ Deprecated (since = "1.0.0.M6" )
153171 public OpenAiApi (String baseUrl , String apiKey , MultiValueMap <String , String > headers , String completionsPath ,
154172 String embeddingsPath , RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
155173 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 ) {
156194
157195 Assert .hasText (completionsPath , "Completions Path must not be null" );
158196 Assert .hasText (embeddingsPath , "Embeddings Path must not be null" );
@@ -162,7 +200,7 @@ public OpenAiApi(String baseUrl, String apiKey, MultiValueMap<String, String> he
162200 this .embeddingsPath = embeddingsPath ;
163201 // @formatter:off
164202 Consumer <HttpHeaders > finalHeaders = h -> {
165- h .setBearerAuth (apiKey );
203+ h .setBearerAuth (apiKey . getValue () );
166204 h .setContentType (MediaType .APPLICATION_JSON );
167205 h .addAll (headers );
168206 };
@@ -1607,4 +1645,78 @@ public record EmbeddingList<T>(// @formatter:off
16071645 @ JsonProperty ("usage" ) Usage usage ) { // @formatter:on
16081646 }
16091647
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+
16101722}
0 commit comments