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 ;
6163 */
6264public class OpenAiApi {
6365
66+ public static Builder builder () {
67+ return new Builder ();
68+ }
69+
6470 public static final OpenAiApi .ChatModel DEFAULT_CHAT_MODEL = ChatModel .GPT_4_O ;
6571
6672 public static final String DEFAULT_EMBEDDING_MODEL = EmbeddingModel .TEXT_EMBEDDING_ADA_002 .getValue ();
@@ -80,7 +86,9 @@ public class OpenAiApi {
8086 /**
8187 * Create a new chat completion api with base URL set to https://api.openai.com
8288 * @param apiKey OpenAI apiKey.
89+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
8390 */
91+ @ Deprecated (since = "1.0.0.M6" )
8492 public OpenAiApi (String apiKey ) {
8593 this (OpenAiApiConstants .DEFAULT_BASE_URL , apiKey );
8694 }
@@ -89,7 +97,9 @@ public OpenAiApi(String apiKey) {
8997 * Create a new chat completion api.
9098 * @param baseUrl api base URL.
9199 * @param apiKey OpenAI apiKey.
100+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
92101 */
102+ @ Deprecated (since = "1.0.0.M6" )
93103 public OpenAiApi (String baseUrl , String apiKey ) {
94104 this (baseUrl , apiKey , RestClient .builder (), WebClient .builder ());
95105 }
@@ -100,7 +110,9 @@ public OpenAiApi(String baseUrl, String apiKey) {
100110 * @param apiKey OpenAI apiKey.
101111 * @param restClientBuilder RestClient builder.
102112 * @param webClientBuilder WebClient builder.
113+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
103114 */
115+ @ Deprecated (since = "1.0.0.M6" )
104116 public OpenAiApi (String baseUrl , String apiKey , RestClient .Builder restClientBuilder ,
105117 WebClient .Builder webClientBuilder ) {
106118 this (baseUrl , apiKey , restClientBuilder , webClientBuilder , RetryUtils .DEFAULT_RESPONSE_ERROR_HANDLER );
@@ -113,7 +125,9 @@ public OpenAiApi(String baseUrl, String apiKey, RestClient.Builder restClientBui
113125 * @param restClientBuilder RestClient builder.
114126 * @param webClientBuilder WebClient builder.
115127 * @param responseErrorHandler Response error handler.
128+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
116129 */
130+ @ Deprecated (since = "1.0.0.M6" )
117131 public OpenAiApi (String baseUrl , String apiKey , RestClient .Builder restClientBuilder ,
118132 WebClient .Builder webClientBuilder , ResponseErrorHandler responseErrorHandler ) {
119133 this (baseUrl , apiKey , "/v1/chat/completions" , "/v1/embeddings" , restClientBuilder , webClientBuilder ,
@@ -129,7 +143,9 @@ public OpenAiApi(String baseUrl, String apiKey, RestClient.Builder restClientBui
129143 * @param restClientBuilder RestClient builder.
130144 * @param webClientBuilder WebClient builder.
131145 * @param responseErrorHandler Response error handler.
146+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
132147 */
148+ @ Deprecated (since = "1.0.0.M6" )
133149 public OpenAiApi (String baseUrl , String apiKey , String completionsPath , String embeddingsPath ,
134150 RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
135151 ResponseErrorHandler responseErrorHandler ) {
@@ -148,10 +164,32 @@ public OpenAiApi(String baseUrl, String apiKey, String completionsPath, String e
148164 * @param restClientBuilder RestClient builder.
149165 * @param webClientBuilder WebClient builder.
150166 * @param responseErrorHandler Response error handler.
167+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
151168 */
169+ @ Deprecated (since = "1.0.0.M6" )
152170 public OpenAiApi (String baseUrl , String apiKey , MultiValueMap <String , String > headers , String completionsPath ,
153171 String embeddingsPath , RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
154172 ResponseErrorHandler responseErrorHandler ) {
173+ this (baseUrl , new SimpleApiKey (apiKey ), headers , completionsPath , embeddingsPath , restClientBuilder ,
174+ webClientBuilder , responseErrorHandler );
175+ }
176+
177+ /**
178+ * Create a new chat completion api.
179+ * @param baseUrl api base URL.
180+ * @param apiKey OpenAI apiKey.
181+ * @param headers the http headers to use.
182+ * @param completionsPath the path to the chat completions endpoint.
183+ * @param embeddingsPath the path to the embeddings endpoint.
184+ * @param restClientBuilder RestClient builder.
185+ * @param webClientBuilder WebClient builder.
186+ * @param responseErrorHandler Response error handler.
187+ * @deprecated since 1.0.0.M6 - use {@link #builder()} instead
188+ */
189+ @ Deprecated (since = "1.0.0.M6" )
190+ public OpenAiApi (String baseUrl , ApiKey apiKey , MultiValueMap <String , String > headers , String completionsPath ,
191+ String embeddingsPath , RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
192+ ResponseErrorHandler responseErrorHandler ) {
155193
156194 Assert .hasText (completionsPath , "Completions Path must not be null" );
157195 Assert .hasText (embeddingsPath , "Embeddings Path must not be null" );
@@ -161,7 +199,7 @@ public OpenAiApi(String baseUrl, String apiKey, MultiValueMap<String, String> he
161199 this .embeddingsPath = embeddingsPath ;
162200 // @formatter:off
163201 Consumer <HttpHeaders > finalHeaders = h -> {
164- h .setBearerAuth (apiKey );
202+ h .setBearerAuth (apiKey . getValue () );
165203 h .setContentType (MediaType .APPLICATION_JSON );
166204 h .addAll (headers );
167205 };
@@ -1507,4 +1545,78 @@ public record EmbeddingList<T>(// @formatter:off
15071545 @ JsonProperty ("usage" ) Usage usage ) { // @formatter:on
15081546 }
15091547
1548+ public static class Builder {
1549+
1550+ private String baseUrl = OpenAiApiConstants .DEFAULT_BASE_URL ;
1551+
1552+ private ApiKey apiKey ;
1553+
1554+ private MultiValueMap <String , String > headers = new LinkedMultiValueMap <>();
1555+
1556+ private String completionsPath = "/v1/chat/completions" ;
1557+
1558+ private String embeddingsPath = "/v1/embeddings" ;
1559+
1560+ private RestClient .Builder restClientBuilder = RestClient .builder ();
1561+
1562+ private WebClient .Builder webClientBuilder = WebClient .builder ();
1563+
1564+ private ResponseErrorHandler responseErrorHandler = RetryUtils .DEFAULT_RESPONSE_ERROR_HANDLER ;
1565+
1566+ public Builder baseUrl (String baseUrl ) {
1567+ Assert .hasText (baseUrl , "baseUrl cannot be null or empty" );
1568+ this .baseUrl = baseUrl ;
1569+ return this ;
1570+ }
1571+
1572+ public Builder apiKey (ApiKey apiKey ) {
1573+ Assert .notNull (apiKey , "apiKey cannot be null" );
1574+ this .apiKey = apiKey ;
1575+ return this ;
1576+ }
1577+
1578+ public Builder headers (MultiValueMap <String , String > headers ) {
1579+ Assert .notNull (headers , "headers cannot be null" );
1580+ this .headers = headers ;
1581+ return this ;
1582+ }
1583+
1584+ public Builder completionsPath (String completionsPath ) {
1585+ Assert .hasText (completionsPath , "completionsPath cannot be null or empty" );
1586+ this .completionsPath = completionsPath ;
1587+ return this ;
1588+ }
1589+
1590+ public Builder embeddingsPath (String embeddingsPath ) {
1591+ Assert .hasText (embeddingsPath , "embeddingsPath cannot be null or empty" );
1592+ this .embeddingsPath = embeddingsPath ;
1593+ return this ;
1594+ }
1595+
1596+ public Builder restClientBuilder (RestClient .Builder restClientBuilder ) {
1597+ Assert .notNull (restClientBuilder , "restClientBuilder cannot be null" );
1598+ this .restClientBuilder = restClientBuilder ;
1599+ return this ;
1600+ }
1601+
1602+ public Builder webClientBuilder (WebClient .Builder webClientBuilder ) {
1603+ Assert .notNull (webClientBuilder , "webClientBuilder cannot be null" );
1604+ this .webClientBuilder = webClientBuilder ;
1605+ return this ;
1606+ }
1607+
1608+ public Builder responseErrorHandler (ResponseErrorHandler responseErrorHandler ) {
1609+ Assert .notNull (responseErrorHandler , "responseErrorHandler cannot be null" );
1610+ this .responseErrorHandler = responseErrorHandler ;
1611+ return this ;
1612+ }
1613+
1614+ public OpenAiApi build () {
1615+ Assert .notNull (this .apiKey , "apiKey must be set" );
1616+ return new OpenAiApi (this .baseUrl , this .apiKey , this .headers , this .completionsPath , this .embeddingsPath ,
1617+ this .restClientBuilder , this .webClientBuilder , this .responseErrorHandler );
1618+ }
1619+
1620+ }
1621+
15101622}
0 commit comments