Skip to content

Commit dcf815d

Browse files
committed
Refactoring OpenAI Providers
1 parent b075471 commit dcf815d

File tree

11 files changed

+348
-276
lines changed

11 files changed

+348
-276
lines changed

src/main/java/io/github/sashirestela/openai/SimpleOpenAI.java

Lines changed: 89 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
package io.github.sashirestela.openai;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import io.github.sashirestela.openai.base.AbstractOpenAIProvider;
54
import io.github.sashirestela.openai.base.ClientConfig;
5+
import io.github.sashirestela.openai.base.OpenAIConfigurator;
6+
import io.github.sashirestela.openai.base.OpenAIProvider;
67
import io.github.sashirestela.openai.base.RealtimeConfig;
7-
import io.github.sashirestela.openai.service.provider.StandardOpenAIServices;
8+
import io.github.sashirestela.openai.service.AssistantServices;
9+
import io.github.sashirestela.openai.service.AudioServices;
10+
import io.github.sashirestela.openai.service.BatchServices;
11+
import io.github.sashirestela.openai.service.ChatCompletionServices;
12+
import io.github.sashirestela.openai.service.CompletionServices;
13+
import io.github.sashirestela.openai.service.EmbeddingServices;
14+
import io.github.sashirestela.openai.service.FileServices;
15+
import io.github.sashirestela.openai.service.FineTunningServices;
16+
import io.github.sashirestela.openai.service.ImageServices;
17+
import io.github.sashirestela.openai.service.ModelServices;
18+
import io.github.sashirestela.openai.service.ModerationServices;
19+
import io.github.sashirestela.openai.service.RealtimeServices;
20+
import io.github.sashirestela.openai.service.SessionServices;
21+
import io.github.sashirestela.openai.service.UploadServices;
822
import io.github.sashirestela.openai.support.Constant;
923
import lombok.Builder;
1024
import lombok.NonNull;
25+
import lombok.experimental.SuperBuilder;
1126

1227
import java.net.http.HttpClient;
1328
import java.util.HashMap;
@@ -17,7 +32,21 @@
1732
/**
1833
* The standard OpenAI implementation which implements the full services.
1934
*/
20-
public class SimpleOpenAI extends AbstractOpenAIProvider implements StandardOpenAIServices {
35+
public class SimpleOpenAI extends OpenAIProvider implements
36+
AssistantServices,
37+
AudioServices,
38+
BatchServices,
39+
ChatCompletionServices,
40+
CompletionServices,
41+
EmbeddingServices,
42+
FileServices,
43+
FineTunningServices,
44+
ImageServices,
45+
ModelServices,
46+
ModerationServices,
47+
RealtimeServices,
48+
SessionServices,
49+
UploadServices {
2150

2251
/**
2352
* Constructor used to generate a builder.
@@ -34,47 +63,15 @@ public class SimpleOpenAI extends AbstractOpenAIProvider implements StandardOpen
3463
@Builder
3564
public SimpleOpenAI(@NonNull String apiKey, String organizationId, String projectId, String baseUrl,
3665
HttpClient httpClient, ObjectMapper objectMapper, RealtimeConfig realtimeConfig) {
37-
super(buildConfig(apiKey, organizationId, projectId, baseUrl, httpClient, objectMapper, realtimeConfig));
38-
}
39-
40-
public static ClientConfig buildConfig(String apiKey, String organizationId, String projectId, String baseUrl,
41-
HttpClient httpClient, ObjectMapper objectMapper, RealtimeConfig realtimeConfig) {
42-
return ClientConfig.builder()
43-
.baseUrl(Optional.ofNullable(baseUrl).orElse(Constant.OPENAI_BASE_URL))
44-
.headers(headers(apiKey, organizationId, projectId))
66+
super(StandardConfigurator.builder()
67+
.apiKey(apiKey)
68+
.organizationId(organizationId)
69+
.projectId(projectId)
70+
.baseUrl(baseUrl)
4571
.httpClient(httpClient)
4672
.objectMapper(objectMapper)
47-
.realtimeConfig(realtimeConfig(apiKey, realtimeConfig))
48-
.build();
49-
}
50-
51-
private static Map<String, String> headers(String apiKey, String organizationId, String projectId) {
52-
var headers = new HashMap<String, String>();
53-
headers.put(Constant.AUTHORIZATION_HEADER, Constant.BEARER_AUTHORIZATION + apiKey);
54-
if (organizationId != null) {
55-
headers.put(Constant.OPENAI_ORG_HEADER, organizationId);
56-
}
57-
if (projectId != null) {
58-
headers.put(Constant.OPENAI_PRJ_HEADER, projectId);
59-
}
60-
return headers;
61-
}
62-
63-
private static RealtimeConfig realtimeConfig(String apiKey, RealtimeConfig realtimeConfig) {
64-
if (realtimeConfig == null) {
65-
return null;
66-
}
67-
var headers = new HashMap<String, String>();
68-
headers.put(Constant.AUTHORIZATION_HEADER, Constant.BEARER_AUTHORIZATION + apiKey);
69-
headers.put(Constant.OPENAI_BETA_HEADER, Constant.OPENAI_REALTIME_VERSION);
70-
var queryParams = new HashMap<String, String>();
71-
queryParams.put(Constant.OPENAI_REALTIME_MODEL_NAME, realtimeConfig.getModel());
72-
return RealtimeConfig.builder()
73-
.endpointUrl(
74-
Optional.ofNullable(realtimeConfig.getEndpointUrl()).orElse(Constant.OPENAI_WS_ENDPOINT_URL))
75-
.headers(headers)
76-
.queryParams(queryParams)
77-
.build();
73+
.realtimeConfig(realtimeConfig)
74+
.build());
7875
}
7976

8077
@Override
@@ -182,4 +179,54 @@ public OpenAIRealtime realtime() {
182179
return this.realtime;
183180
}
184181

182+
@SuperBuilder
183+
static class StandardConfigurator extends OpenAIConfigurator {
184+
185+
private String organizationId;
186+
private String projectId;
187+
private RealtimeConfig realtimeConfig;
188+
189+
@Override
190+
public ClientConfig buildConfig() {
191+
return ClientConfig.builder()
192+
.baseUrl(Optional.ofNullable(baseUrl).orElse(Constant.OPENAI_BASE_URL))
193+
.headers(makeHeaders())
194+
.httpClient(httpClient)
195+
.objectMapper(objectMapper)
196+
.realtimeConfig(makeRealtimeConfig())
197+
.build();
198+
}
199+
200+
private Map<String, String> makeHeaders() {
201+
var headers = new HashMap<String, String>();
202+
headers.put(Constant.AUTHORIZATION_HEADER, Constant.BEARER_AUTHORIZATION + apiKey);
203+
if (organizationId != null) {
204+
headers.put(Constant.OPENAI_ORG_HEADER, organizationId);
205+
}
206+
if (projectId != null) {
207+
headers.put(Constant.OPENAI_PRJ_HEADER, projectId);
208+
}
209+
return headers;
210+
}
211+
212+
private RealtimeConfig makeRealtimeConfig() {
213+
if (realtimeConfig == null) {
214+
return null;
215+
}
216+
var headers = new HashMap<String, String>();
217+
headers.put(Constant.AUTHORIZATION_HEADER, Constant.BEARER_AUTHORIZATION + apiKey);
218+
headers.put(Constant.OPENAI_BETA_HEADER, Constant.OPENAI_REALTIME_VERSION);
219+
var queryParams = new HashMap<String, String>();
220+
queryParams.put(Constant.OPENAI_REALTIME_MODEL_NAME, realtimeConfig.getModel());
221+
return RealtimeConfig.builder()
222+
.endpointUrl(
223+
Optional.ofNullable(realtimeConfig.getEndpointUrl())
224+
.orElse(Constant.OPENAI_WS_ENDPOINT_URL))
225+
.headers(headers)
226+
.queryParams(queryParams)
227+
.build();
228+
}
229+
230+
}
231+
185232
}
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.github.sashirestela.openai;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import io.github.sashirestela.openai.base.AbstractOpenAIProvider;
54
import io.github.sashirestela.openai.base.ClientConfig;
6-
import io.github.sashirestela.openai.service.provider.AnyscaleOpenAIServices;
5+
import io.github.sashirestela.openai.base.OpenAIConfigurator;
6+
import io.github.sashirestela.openai.base.OpenAIProvider;
7+
import io.github.sashirestela.openai.service.ChatCompletionServices;
78
import io.github.sashirestela.openai.support.Constant;
89
import lombok.Builder;
910
import lombok.NonNull;
11+
import lombok.experimental.SuperBuilder;
1012

1113
import java.net.http.HttpClient;
1214
import java.util.Map;
@@ -15,7 +17,8 @@
1517
/**
1618
* The Anyscale OpenAI implementation which implements a subset of the standard services.
1719
*/
18-
public class SimpleOpenAIAnyscale extends AbstractOpenAIProvider implements AnyscaleOpenAIServices {
20+
public class SimpleOpenAIAnyscale extends OpenAIProvider implements
21+
ChatCompletionServices {
1922

2023
/**
2124
* Constructor used to generate a builder.
@@ -29,23 +32,32 @@ public class SimpleOpenAIAnyscale extends AbstractOpenAIProvider implements Anys
2932
@Builder
3033
public SimpleOpenAIAnyscale(@NonNull String apiKey, String baseUrl, HttpClient httpClient,
3134
ObjectMapper objectMapper) {
32-
super(buildConfig(apiKey, baseUrl, httpClient, objectMapper));
33-
}
34-
35-
public static ClientConfig buildConfig(String apiKey, String baseUrl, HttpClient httpClient,
36-
ObjectMapper objectMapper) {
37-
return ClientConfig.builder()
38-
.baseUrl(Optional.ofNullable(baseUrl).orElse(Constant.ANYSCALE_BASE_URL))
39-
.headers(Map.of(Constant.AUTHORIZATION_HEADER, Constant.BEARER_AUTHORIZATION + apiKey))
35+
super(AnyscaleConfigurator.builder()
36+
.apiKey(apiKey)
37+
.baseUrl(baseUrl)
4038
.httpClient(httpClient)
4139
.objectMapper(objectMapper)
42-
.build();
43-
40+
.build());
4441
}
4542

4643
@Override
4744
public OpenAI.ChatCompletions chatCompletions() {
4845
return getOrCreateService(OpenAI.ChatCompletions.class);
4946
}
5047

48+
@SuperBuilder
49+
static class AnyscaleConfigurator extends OpenAIConfigurator {
50+
51+
@Override
52+
public ClientConfig buildConfig() {
53+
return ClientConfig.builder()
54+
.baseUrl(Optional.ofNullable(baseUrl).orElse(Constant.ANYSCALE_BASE_URL))
55+
.headers(Map.of(Constant.AUTHORIZATION_HEADER, Constant.BEARER_AUTHORIZATION + apiKey))
56+
.httpClient(httpClient)
57+
.objectMapper(objectMapper)
58+
.build();
59+
}
60+
61+
}
62+
5163
}

0 commit comments

Comments
 (0)