Enable base URL to be configurable for OpenAI using SPRING_AI_OPENAI_BASE_URL
This commit is contained in:
committed by
Mark Pollack
parent
09816b43df
commit
0218069613
@@ -47,6 +47,12 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>converter-jackson</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -16,8 +16,15 @@
|
||||
|
||||
package org.springframework.ai.autoconfigure.openai;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.theokanning.openai.OpenAiApi;
|
||||
import com.theokanning.openai.service.OpenAiService;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
import retrofit2.converter.jackson.JacksonConverterFactory;
|
||||
|
||||
import org.springframework.ai.autoconfigure.NativeHints;
|
||||
import org.springframework.ai.embedding.EmbeddingClient;
|
||||
import org.springframework.ai.openai.embedding.OpenAiEmbeddingClient;
|
||||
@@ -45,11 +52,27 @@ public class OpenAiAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public OpenAiService theoOpenAiService(OpenAiProperties openAiProperties) {
|
||||
if (!StringUtils.hasText(openAiProperties.getApiKey())) {
|
||||
throw new IllegalArgumentException(
|
||||
"You must provide an API key with the property name " + CONFIG_PREFIX + ".api-key");
|
||||
if (openAiProperties.getBaseUrl().equals("https://api.openai.com")) {
|
||||
if (!StringUtils.hasText(openAiProperties.getApiKey())) {
|
||||
throw new IllegalArgumentException(
|
||||
"You must provide an API key with the property name " + CONFIG_PREFIX + ".api-key");
|
||||
}
|
||||
}
|
||||
return new OpenAiService(openAiProperties.getApiKey(), openAiProperties.getDuration());
|
||||
|
||||
ObjectMapper mapper = OpenAiService.defaultObjectMapper();
|
||||
OkHttpClient client = OpenAiService.defaultClient(openAiProperties.getApiKey(), openAiProperties.getDuration());
|
||||
|
||||
// Waiting for https://github.com/TheoKanning/openai-java/issues/249 to be
|
||||
// resolved.
|
||||
Retrofit retrofit = new Retrofit.Builder().baseUrl(openAiProperties.getBaseUrl())
|
||||
.client(client)
|
||||
.addConverterFactory(JacksonConverterFactory.create(mapper))
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.build();
|
||||
|
||||
OpenAiApi api = retrofit.create(OpenAiApi.class);
|
||||
|
||||
return new OpenAiService(api);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -35,6 +35,8 @@ public class OpenAiProperties {
|
||||
|
||||
private String model = "gpt-3.5-turbo";
|
||||
|
||||
private String baseUrl = "https://api.openai.com";
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
@@ -67,4 +69,12 @@ public class OpenAiProperties {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
public void setBaseUrl(String baseUrl) {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user