Skip to content

Commit 0218069

Browse files
habumamarkpollack
authored andcommitted
Enable base URL to be configurable for OpenAI using SPRING_AI_OPENAI_BASE_URL
1 parent 09816b4 commit 0218069

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

spring-ai-spring-boot-autoconfigure/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
<optional>true</optional>
4848
</dependency>
4949

50+
<dependency>
51+
<groupId>com.squareup.retrofit2</groupId>
52+
<artifactId>converter-jackson</artifactId>
53+
<version>2.9.0</version>
54+
</dependency>
55+
5056
<!-- test dependencies -->
5157
<dependency>
5258
<groupId>org.springframework.boot</groupId>

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/openai/OpenAiAutoConfiguration.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616

1717
package org.springframework.ai.autoconfigure.openai;
1818

19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.theokanning.openai.OpenAiApi;
1921
import com.theokanning.openai.service.OpenAiService;
2022

23+
import okhttp3.OkHttpClient;
24+
import retrofit2.Retrofit;
25+
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
26+
import retrofit2.converter.jackson.JacksonConverterFactory;
27+
2128
import org.springframework.ai.autoconfigure.NativeHints;
2229
import org.springframework.ai.embedding.EmbeddingClient;
2330
import org.springframework.ai.openai.embedding.OpenAiEmbeddingClient;
@@ -45,11 +52,27 @@ public OpenAiAutoConfiguration(OpenAiProperties openAiProperties) {
4552

4653
@Bean
4754
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+
}
5160
}
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);
5376
}
5477

5578
@Bean

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/openai/OpenAiProperties.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class OpenAiProperties {
3535

3636
private String model = "gpt-3.5-turbo";
3737

38+
private String baseUrl = "https://api.openai.com";
39+
3840
public String getApiKey() {
3941
return apiKey;
4042
}
@@ -67,4 +69,12 @@ public void setDuration(Duration duration) {
6769
this.duration = duration;
6870
}
6971

72+
public String getBaseUrl() {
73+
return baseUrl;
74+
}
75+
76+
public void setBaseUrl(String baseUrl) {
77+
this.baseUrl = baseUrl;
78+
}
79+
7080
}

0 commit comments

Comments
 (0)