Skip to content

Commit

Permalink
1.1.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
jetkai committed Mar 8, 2023
1 parent 304f126 commit 8a0247c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
18 changes: 12 additions & 6 deletions src/main/java/io/github/jetkai/openai/openai/OpenAIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
final class OpenAIImpl extends OpenAI {

private final boolean isTesting = Objects.equals(System.getProperty("is.testing"), "true");
private final HttpClient httpClient;
private final HttpClientInstance httpClientInstance;
private final String apiKey;
Expand Down Expand Up @@ -105,15 +106,15 @@ public <T> T createInstance(Class<T> clazz, Object data) {
return (T) this;
}
try {
Class<?> superClazz = data.getClass().getSuperclass();
Class<?> dataClazz = superClazz == Object.class ? data.getClass() : superClazz;
T instance = clazz.getConstructor(dataClazz).newInstance(data);
T instance = data != null
? clazz.getConstructor(data.getClass().getSuperclass() == Object.class
? data.getClass() : data.getClass().getSuperclass()).newInstance(data)
: clazz.getConstructor().newInstance();
if (instance instanceof OAPI) {
((OAPI) instance).setOpenAI(this).initialize();
}
return instance;
} catch (InstantiationException | IllegalAccessException
| InvocationTargetException | NoSuchMethodException e) {
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -129,6 +130,9 @@ public <T> T createInstance(OpenAIEndpoints endpoint, Object data) {
instance = new ListModel(String.valueOf(data));
break;
case LIST_MODELS:
if(isTesting && data == null) {
throw new IllegalArgumentException("Data can not be null (when testing)");
}
instance = new ListModels();
break;
case CREATE_CHAT_COMPLETION:
Expand Down Expand Up @@ -194,7 +198,9 @@ public <T> T createInstance(OpenAIEndpoints endpoint, Object data) {
default:
throw new IllegalArgumentException("Endpoint not handled: " + endpoint);
}
instance.setOpenAI(this).initialize();
if(!isTesting) {
instance.setOpenAI(this).initialize();
}
return (T) instance;
}

Expand Down
35 changes: 15 additions & 20 deletions src/test/java/CreateCompletionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import io.github.jetkai.openai.openai.OpenAI;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

/**
Expand Down Expand Up @@ -90,31 +88,28 @@ void createCompletionTest() {
.build()
.sendRequest();

assertNotNull(openAI);
assertNotNull(openAI, "OpenAI not found");

//Call the CreateCompletion API from OpenAI & create instance
Optional<CreateCompletion> optionalCreateCompletion = openAI.completion();
assertFalse(optionalCreateCompletion.isEmpty());

//Additionally check the getter method is not null
assertNotNull(openAI.getCompletion());

CreateCompletion createCompletion = optionalCreateCompletion.get();

assertNotNull(createCompletion.asStringArray());
assertNotNull(createCompletion.asSentences());
assertNotNull(createCompletion.asNormalizedSentences(2048));
assertNotNull(createCompletion.asNormalizedSentences(1));
assertNotNull(createCompletion.asText());
CreateCompletion createCompletion = openAI.completion().orElseThrow(() ->
new AssertionError("CreateCompletion object not found"));

//Data structure example
//Additional checks
assertNotNull(openAI.getCompletion(), "CreateCompletion object not found");
CompletionResponseData responseData = createCompletion.asData();
assertNotNull(responseData);
assertNotNull(responseData, "CompletionResponseData object not found");
assertNotNull(createCompletion.asStringArray(), "String Array not found");
assertNotNull(createCompletion.asSentences(), "Sentence List not found");
assertNotNull(createCompletion.asNormalizedSentences(2048),
"Normalized Sentence List (2048) not found");
assertNotNull(createCompletion.asNormalizedSentences(1),
"Normalized Sentence List (1) not found");
assertNotNull(createCompletion.asText(), "Text not found");

//Json example
String json = createCompletion.asJson();
assertNotNull(json);
assertFalse(json.isEmpty());
assertNotNull(json, "Json not found");
assertFalse(json.isEmpty(), "Json is empty");
}

}
9 changes: 9 additions & 0 deletions src/test/java/CreateInstanceTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import io.github.jetkai.openai.api.ListModel;
import io.github.jetkai.openai.api.ListModels;
import io.github.jetkai.openai.net.OpenAIEndpoints;
import io.github.jetkai.openai.openai.OpenAI;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* CreateInstanceTest
Expand All @@ -17,6 +19,7 @@ public class CreateInstanceTest {

@Test
void createInstanceTest() {
System.setProperty("is.testing", "true");
String apiKey = System.getenv("OPEN_AI_API_KEY");
String organization = System.getenv("OPEN_AI_ORGANIZATION");
assertNotNull(apiKey);
Expand All @@ -32,6 +35,12 @@ void createInstanceTest() {

ListModel instance2 = openAI.createInstance(OpenAIEndpoints.LIST_MODEL, "davinci");
assertNotNull(instance2);

//This test should throw
for(OpenAIEndpoints openAIEndpoints : OpenAIEndpoints.values()) {
assertThrows(IllegalArgumentException.class, ()-> openAI.createInstance(openAIEndpoints, null));
}

}

}

0 comments on commit 8a0247c

Please sign in to comment.