24
24
import retrofit2 .adapter .rxjava2 .RxJava2CallAdapterFactory ;
25
25
import retrofit2 .converter .jackson .JacksonConverterFactory ;
26
26
27
+ import java .time .Duration ;
27
28
import java .util .List ;
28
29
import java .util .concurrent .TimeUnit ;
29
30
31
+ import static java .time .Duration .ofSeconds ;
32
+
30
33
public class OpenAiService {
31
34
32
- OpenAiApi api ;
35
+ private static final String BASE_URL = "https://api.openai.com/" ;
36
+
37
+ final OpenAiApi api ;
33
38
34
39
/**
35
40
* Creates a new OpenAiService that wraps OpenAiApi
36
41
*
37
42
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
38
43
*/
39
- public OpenAiService (String token ) {
40
- this (token , 10 );
44
+ public OpenAiService (final String token ) {
45
+ this (token , BASE_URL , ofSeconds ( 10 ) );
41
46
}
42
47
43
48
/**
44
49
* Creates a new OpenAiService that wraps OpenAiApi
45
50
*
46
51
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
47
52
* @param timeout http read timeout in seconds, 0 means no timeout
53
+ * @deprecated use {@link OpenAiService(String, Duration)}
54
+ */
55
+ @ Deprecated
56
+ public OpenAiService (final String token , final int timeout ) {
57
+ this (token , BASE_URL , ofSeconds (timeout ));
58
+ }
59
+
60
+ /**
61
+ * Creates a new OpenAiService that wraps OpenAiApi
62
+ *
63
+ * @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
64
+ * @param timeout http read timeout, Duration.ZERO means no timeout
65
+ */
66
+ public OpenAiService (final String token , final Duration timeout ) {
67
+ this (token , BASE_URL , timeout );
68
+ }
69
+
70
+ /**
71
+ * Creates a new OpenAiService that wraps OpenAiApi
72
+ *
73
+ * @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
74
+ * @param timeout http read timeout, Duration.ZERO means no timeout
48
75
*/
49
- public OpenAiService (String token , int timeout ) {
76
+ public OpenAiService (final String token , final String baseUrl , final Duration timeout ) {
50
77
ObjectMapper mapper = new ObjectMapper ();
51
78
mapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
52
79
mapper .setSerializationInclusion (JsonInclude .Include .NON_NULL );
@@ -55,11 +82,11 @@ public OpenAiService(String token, int timeout) {
55
82
OkHttpClient client = new OkHttpClient .Builder ()
56
83
.addInterceptor (new AuthenticationInterceptor (token ))
57
84
.connectionPool (new ConnectionPool (5 , 1 , TimeUnit .SECONDS ))
58
- .readTimeout (timeout , TimeUnit .SECONDS )
85
+ .readTimeout (timeout . toMillis () , TimeUnit .MILLISECONDS )
59
86
.build ();
60
87
61
88
Retrofit retrofit = new Retrofit .Builder ()
62
- .baseUrl ("https://api.openai.com/" )
89
+ .baseUrl (baseUrl )
63
90
.client (client )
64
91
.addConverterFactory (JacksonConverterFactory .create (mapper ))
65
92
.addCallAdapterFactory (RxJava2CallAdapterFactory .create ())
@@ -73,7 +100,7 @@ public OpenAiService(String token, int timeout) {
73
100
*
74
101
* @param api OpenAiApi instance to use for all methods
75
102
*/
76
- public OpenAiService (OpenAiApi api ) {
103
+ public OpenAiService (final OpenAiApi api ) {
77
104
this .api = api ;
78
105
}
79
106
0 commit comments