Skip to content

Commit 92d4ec3

Browse files
committed
Merge PR TheoKanning#433 ✨feature(base_url): user can define custom openai base url by @vacuityv
1 parent b86aecd commit 92d4ec3

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

service/src/main/java/com/theokanning/openai/service/OpenAiService.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,37 @@ public OpenAiService(final String token) {
8484
this(token, DEFAULT_TIMEOUT);
8585
}
8686

87+
/**
88+
* Creates a new OpenAiService that wraps OpenAiApi
89+
*
90+
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
91+
* @param baseUrl OpenAi baseUrl, default is "https://api.openai.com/"
92+
*/
93+
public OpenAiService(final String token, String baseUrl) {
94+
this(token, DEFAULT_TIMEOUT, baseUrl);
95+
}
96+
8797
/**
8898
* Creates a new OpenAiService that wraps OpenAiApi
8999
*
90100
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
91101
* @param timeout http read timeout, Duration.ZERO means no timeout
92102
*/
93103
public OpenAiService(final String token, final Duration timeout) {
104+
this(token, timeout, null);
105+
}
106+
107+
/**
108+
* Creates a new OpenAiService that wraps OpenAiApi
109+
*
110+
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
111+
* @param timeout http read timeout, Duration.ZERO means no timeout
112+
* @param baseUrl OpenAi baseUrl, default is "https://api.openai.com/"
113+
*/
114+
public OpenAiService(final String token, final Duration timeout, String baseUrl) {
94115
ObjectMapper mapper = defaultObjectMapper();
95116
OkHttpClient client = defaultClient(token, timeout);
96-
Retrofit retrofit = defaultRetrofit(client, mapper);
117+
Retrofit retrofit = defaultRetrofit(client, mapper, baseUrl);
97118

98119
this.api = retrofit.create(OpenAiApi.class);
99120
this.executorService = client.dispatcher().executorService();
@@ -572,7 +593,7 @@ public void shutdownExecutor() {
572593
public static OpenAiApi buildApi(String token, Duration timeout) {
573594
ObjectMapper mapper = defaultObjectMapper();
574595
OkHttpClient client = defaultClient(token, timeout);
575-
Retrofit retrofit = defaultRetrofit(client, mapper);
596+
Retrofit retrofit = defaultRetrofit(client, mapper, null);
576597

577598
return retrofit.create(OpenAiApi.class);
578599
}
@@ -596,9 +617,9 @@ public static OkHttpClient defaultClient(String token, Duration timeout) {
596617
.build();
597618
}
598619

599-
public static Retrofit defaultRetrofit(OkHttpClient client, ObjectMapper mapper) {
620+
public static Retrofit defaultRetrofit(OkHttpClient client, ObjectMapper mapper, String baseUrl) {
600621
return new Retrofit.Builder()
601-
.baseUrl(BASE_URL)
622+
.baseUrl(baseUrl == null || baseUrl.isEmpty() ? BASE_URL : baseUrl)
602623
.client(client)
603624
.addConverterFactory(JacksonConverterFactory.create(mapper))
604625
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())

0 commit comments

Comments
 (0)