Skip to content
This repository was archived by the owner on Jun 6, 2024. It is now read-only.

Commit 4d5878d

Browse files
authored
Add OpenAiService constructor that takes timeout parameter (#17)
If certain engines are timing out regularly, then OpenAiService needs a timeout parameter. I also added a constructor that takes an OpenAiApi, and this will allow users to customize their api settings much more easily. If we need more parameters later, I might add a builder for OpenAiService. Based on feedback in #5
1 parent 9e0a26b commit 4d5878d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,20 @@ public class OpenAiService {
3333

3434
OpenAiApi api;
3535

36+
/**
37+
* Creates a new OpenAiService that wraps OpenAiApi
38+
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
39+
*/
3640
public OpenAiService(String token) {
41+
this(token, 10);
42+
}
43+
44+
/**
45+
* Creates a new OpenAiService that wraps OpenAiApi
46+
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
47+
* @param timeout http read timeout in seconds, 0 means no timeout
48+
*/
49+
public OpenAiService(String token, int timeout) {
3750
ObjectMapper mapper = new ObjectMapper();
3851
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
3952
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
@@ -42,6 +55,7 @@ public OpenAiService(String token) {
4255
OkHttpClient client = new OkHttpClient.Builder()
4356
.addInterceptor(new AuthenticationInterceptor(token))
4457
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
58+
.readTimeout(timeout, TimeUnit.SECONDS)
4559
.build();
4660

4761
Retrofit retrofit = new Retrofit.Builder()
@@ -51,7 +65,15 @@ public OpenAiService(String token) {
5165
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
5266
.build();
5367

54-
api = retrofit.create(OpenAiApi.class);
68+
this.api = retrofit.create(OpenAiApi.class);
69+
}
70+
71+
/**
72+
* Creates a new OpenAiService that wraps OpenAiApi
73+
* @param api OpenAiApi instance to use for all methods
74+
*/
75+
public OpenAiService(OpenAiApi api) {
76+
this.api = api;
5577
}
5678

5779
public List<Engine> getEngines() {

0 commit comments

Comments
 (0)