|
16 | 16 |
|
17 | 17 | package org.springframework.ai.autoconfigure.openai; |
18 | 18 |
|
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import com.theokanning.openai.OpenAiApi; |
19 | 21 | import com.theokanning.openai.service.OpenAiService; |
20 | 22 |
|
| 23 | +import okhttp3.OkHttpClient; |
| 24 | +import retrofit2.Retrofit; |
| 25 | +import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; |
| 26 | +import retrofit2.converter.jackson.JacksonConverterFactory; |
| 27 | + |
21 | 28 | import org.springframework.ai.autoconfigure.NativeHints; |
22 | 29 | import org.springframework.ai.embedding.EmbeddingClient; |
23 | 30 | import org.springframework.ai.openai.embedding.OpenAiEmbeddingClient; |
@@ -45,11 +52,27 @@ public OpenAiAutoConfiguration(OpenAiProperties openAiProperties) { |
45 | 52 |
|
46 | 53 | @Bean |
47 | 54 | public OpenAiService theoOpenAiService(OpenAiProperties openAiProperties) { |
48 | | -if (!StringUtils.hasText(openAiProperties.getApiKey())) { |
49 | | -throw new IllegalArgumentException( |
50 | | -"You must provide an API key with the property name " + CONFIG_PREFIX + ".api-key"); |
| 55 | +if (openAiProperties.getBaseUrl().equals("https://api.openai.com")) { |
| 56 | +if (!StringUtils.hasText(openAiProperties.getApiKey())) { |
| 57 | +throw new IllegalArgumentException( |
| 58 | +"You must provide an API key with the property name " + CONFIG_PREFIX + ".api-key"); |
| 59 | +} |
51 | 60 | } |
52 | | -return new OpenAiService(openAiProperties.getApiKey(), openAiProperties.getDuration()); |
| 61 | + |
| 62 | +ObjectMapper mapper = OpenAiService.defaultObjectMapper(); |
| 63 | +OkHttpClient client = OpenAiService.defaultClient(openAiProperties.getApiKey(), openAiProperties.getDuration()); |
| 64 | + |
| 65 | +// Waiting for https://github.com/TheoKanning/openai-java/issues/249 to be |
| 66 | +// resolved. |
| 67 | +Retrofit retrofit = new Retrofit.Builder().baseUrl(openAiProperties.getBaseUrl()) |
| 68 | +.client(client) |
| 69 | +.addConverterFactory(JacksonConverterFactory.create(mapper)) |
| 70 | +.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
| 71 | +.build(); |
| 72 | + |
| 73 | +OpenAiApi api = retrofit.create(OpenAiApi.class); |
| 74 | + |
| 75 | +return new OpenAiService(api); |
53 | 76 | } |
54 | 77 |
|
55 | 78 | @Bean |
|
0 commit comments