22
33import com .fasterxml .jackson .annotation .JsonInclude ;
44import com .fasterxml .jackson .databind .ObjectMapper ;
5- import com .google .common .base .Preconditions ;
65import lombok .Builder ;
76import lombok .extern .slf4j .Slf4j ;
87import okhttp3 .OkHttpClient ;
98import org .apache .commons .lang3 .ObjectUtils ;
109import org .apache .commons .lang3 .StringUtils ;
10+ import org .devlive .sdk .openai .exception .ParamException ;
11+ import org .devlive .sdk .openai .interceptor .AzureInterceptor ;
1112import org .devlive .sdk .openai .interceptor .DefaultInterceptor ;
13+ import org .devlive .sdk .openai .interceptor .OpenAiInterceptor ;
14+ import org .devlive .sdk .openai .model .ProviderModel ;
1215import retrofit2 .Retrofit ;
1316import retrofit2 .adapter .rxjava2 .RxJava2CallAdapterFactory ;
1417import retrofit2 .converter .jackson .JacksonConverterFactory ;
@@ -27,14 +30,31 @@ public class OpenAiClient
2730 private Integer timeout ;
2831 private TimeUnit unit ;
2932 private OkHttpClient client ;
33+ private ProviderModel provider ;
34+ // Azure provider requires
35+ private String model ; // The model name deployed in azure
36+ private String version ;
3037
3138 private OpenAiClient (OpenAiClientBuilder builder )
3239 {
3340 boolean hasApiKey = StringUtils .isNotEmpty (builder .apiKey );
3441 if (!hasApiKey ) {
3542 log .error ("Invalid OpenAi token" );
43+ throw new ParamException ("Invalid OpenAi token" );
44+ }
45+
46+ if (ObjectUtils .isEmpty (builder .provider )) {
47+ builder .provider (ProviderModel .openai );
48+ }
49+
50+ if (builder .provider .equals (ProviderModel .azure )) {
51+ if (ObjectUtils .isEmpty (builder .model )) {
52+ throw new ParamException ("Azure provider model not specified" );
53+ }
54+ if (ObjectUtils .isEmpty (builder .version )) {
55+ throw new ParamException ("Azure provider version not specified" );
56+ }
3657 }
37- Preconditions .checkState (hasApiKey , "Invalid OpenAi token" );
3858
3959 if (ObjectUtils .isEmpty (builder .apiHost )) {
4060 builder .apiHost (null );
@@ -49,6 +69,7 @@ private OpenAiClient(OpenAiClientBuilder builder)
4969 builder .client (null );
5070 }
5171
72+ super .provider = builder .provider ;
5273 // Build a remote API client
5374 objectMapper .setSerializationInclusion (JsonInclude .Include .NON_NULL );
5475 this .api = new Retrofit .Builder ()
@@ -74,8 +95,10 @@ public OpenAiClientBuilder apiHost(String apiHost)
7495 apiHost = "https://api.openai.com" ;
7596 }
7697 else {
77- Preconditions .checkState (apiHost .startsWith ("http" ) || apiHost .startsWith ("https" ),
78- "Api host must start with http or https" );
98+ boolean flag = apiHost .startsWith ("http" ) || apiHost .startsWith ("https" );
99+ if (!flag ) {
100+ throw new ParamException (String .format ("Invalid apiHost <%s> must start with http or https" , apiHost ));
101+ }
79102 }
80103 this .apiHost = apiHost ;
81104 return this ;
@@ -101,8 +124,12 @@ public OpenAiClientBuilder unit(TimeUnit unit)
101124
102125 public OpenAiClientBuilder client (OkHttpClient client )
103126 {
127+ if (ObjectUtils .isEmpty (this .provider )) {
128+ this .provider = ProviderModel .openai ;
129+ }
130+
104131 if (ObjectUtils .isEmpty (client )) {
105- log .warn ("No client, creating default client" );
132+ log .debug ("No client specified , creating default client" );
106133 client = new OkHttpClient .Builder ()
107134 .connectTimeout (this .timeout , this .unit )
108135 .writeTimeout (this .timeout , this .unit )
@@ -111,8 +138,13 @@ public OpenAiClientBuilder client(OkHttpClient client)
111138 .build ();
112139 }
113140 // Add default interceptor
114- DefaultInterceptor interceptor = new DefaultInterceptor ();
115- interceptor .setApiKey (this .apiKey );
141+ DefaultInterceptor interceptor = new OpenAiInterceptor ();
142+ if (this .provider .equals (ProviderModel .azure )) {
143+ interceptor = new AzureInterceptor ();
144+ interceptor .setVersion (this .version );
145+ interceptor .setModel (this .model );
146+ }
147+ interceptor .setApiKey (apiKey );
116148 client = client .newBuilder ()
117149 .addInterceptor (interceptor )
118150 .build ();
0 commit comments