Skip to content

Commit 8a0247c

Browse files
committed
1.1.1 release
1 parent 304f126 commit 8a0247c

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

src/main/java/io/github/jetkai/openai/openai/OpenAIImpl.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131
final class OpenAIImpl extends OpenAI {
3232

33+
private final boolean isTesting = Objects.equals(System.getProperty("is.testing"), "true");
3334
private final HttpClient httpClient;
3435
private final HttpClientInstance httpClientInstance;
3536
private final String apiKey;
@@ -105,15 +106,15 @@ public <T> T createInstance(Class<T> clazz, Object data) {
105106
return (T) this;
106107
}
107108
try {
108-
Class<?> superClazz = data.getClass().getSuperclass();
109-
Class<?> dataClazz = superClazz == Object.class ? data.getClass() : superClazz;
110-
T instance = clazz.getConstructor(dataClazz).newInstance(data);
109+
T instance = data != null
110+
? clazz.getConstructor(data.getClass().getSuperclass() == Object.class
111+
? data.getClass() : data.getClass().getSuperclass()).newInstance(data)
112+
: clazz.getConstructor().newInstance();
111113
if (instance instanceof OAPI) {
112114
((OAPI) instance).setOpenAI(this).initialize();
113115
}
114116
return instance;
115-
} catch (InstantiationException | IllegalAccessException
116-
| InvocationTargetException | NoSuchMethodException e) {
117+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
117118
throw new RuntimeException(e);
118119
}
119120
}
@@ -129,6 +130,9 @@ public <T> T createInstance(OpenAIEndpoints endpoint, Object data) {
129130
instance = new ListModel(String.valueOf(data));
130131
break;
131132
case LIST_MODELS:
133+
if(isTesting && data == null) {
134+
throw new IllegalArgumentException("Data can not be null (when testing)");
135+
}
132136
instance = new ListModels();
133137
break;
134138
case CREATE_CHAT_COMPLETION:
@@ -194,7 +198,9 @@ public <T> T createInstance(OpenAIEndpoints endpoint, Object data) {
194198
default:
195199
throw new IllegalArgumentException("Endpoint not handled: " + endpoint);
196200
}
197-
instance.setOpenAI(this).initialize();
201+
if(!isTesting) {
202+
instance.setOpenAI(this).initialize();
203+
}
198204
return (T) instance;
199205
}
200206

src/test/java/CreateCompletionTest.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import io.github.jetkai.openai.openai.OpenAI;
55
import org.junit.jupiter.api.Test;
66

7-
import java.util.Optional;
8-
97
import static org.junit.jupiter.api.Assertions.*;
108

119
/**
@@ -90,31 +88,28 @@ void createCompletionTest() {
9088
.build()
9189
.sendRequest();
9290

93-
assertNotNull(openAI);
91+
assertNotNull(openAI, "OpenAI not found");
9492

9593
//Call the CreateCompletion API from OpenAI & create instance
96-
Optional<CreateCompletion> optionalCreateCompletion = openAI.completion();
97-
assertFalse(optionalCreateCompletion.isEmpty());
98-
99-
//Additionally check the getter method is not null
100-
assertNotNull(openAI.getCompletion());
101-
102-
CreateCompletion createCompletion = optionalCreateCompletion.get();
103-
104-
assertNotNull(createCompletion.asStringArray());
105-
assertNotNull(createCompletion.asSentences());
106-
assertNotNull(createCompletion.asNormalizedSentences(2048));
107-
assertNotNull(createCompletion.asNormalizedSentences(1));
108-
assertNotNull(createCompletion.asText());
94+
CreateCompletion createCompletion = openAI.completion().orElseThrow(() ->
95+
new AssertionError("CreateCompletion object not found"));
10996

110-
//Data structure example
97+
//Additional checks
98+
assertNotNull(openAI.getCompletion(), "CreateCompletion object not found");
11199
CompletionResponseData responseData = createCompletion.asData();
112-
assertNotNull(responseData);
100+
assertNotNull(responseData, "CompletionResponseData object not found");
101+
assertNotNull(createCompletion.asStringArray(), "String Array not found");
102+
assertNotNull(createCompletion.asSentences(), "Sentence List not found");
103+
assertNotNull(createCompletion.asNormalizedSentences(2048),
104+
"Normalized Sentence List (2048) not found");
105+
assertNotNull(createCompletion.asNormalizedSentences(1),
106+
"Normalized Sentence List (1) not found");
107+
assertNotNull(createCompletion.asText(), "Text not found");
113108

114109
//Json example
115110
String json = createCompletion.asJson();
116-
assertNotNull(json);
117-
assertFalse(json.isEmpty());
111+
assertNotNull(json, "Json not found");
112+
assertFalse(json.isEmpty(), "Json is empty");
118113
}
119114

120115
}

src/test/java/CreateInstanceTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import io.github.jetkai.openai.api.ListModel;
2+
import io.github.jetkai.openai.api.ListModels;
23
import io.github.jetkai.openai.net.OpenAIEndpoints;
34
import io.github.jetkai.openai.openai.OpenAI;
45
import org.junit.jupiter.api.Test;
56

67
import static org.junit.jupiter.api.Assertions.assertNotNull;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
79

810
/**
911
* CreateInstanceTest
@@ -17,6 +19,7 @@ public class CreateInstanceTest {
1719

1820
@Test
1921
void createInstanceTest() {
22+
System.setProperty("is.testing", "true");
2023
String apiKey = System.getenv("OPEN_AI_API_KEY");
2124
String organization = System.getenv("OPEN_AI_ORGANIZATION");
2225
assertNotNull(apiKey);
@@ -32,6 +35,12 @@ void createInstanceTest() {
3235

3336
ListModel instance2 = openAI.createInstance(OpenAIEndpoints.LIST_MODEL, "davinci");
3437
assertNotNull(instance2);
38+
39+
//This test should throw
40+
for(OpenAIEndpoints openAIEndpoints : OpenAIEndpoints.values()) {
41+
assertThrows(IllegalArgumentException.class, ()-> openAI.createInstance(openAIEndpoints, null));
42+
}
43+
3544
}
3645

3746
}

0 commit comments

Comments
 (0)