Skip to content

Commit

Permalink
Add example test
Browse files Browse the repository at this point in the history
  • Loading branch information
Konboi committed Mar 7, 2024
1 parent e2e7d3b commit 2076e07
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.launchableinc.openai.completion.chat.*;
import com.launchableinc.openai.completion.chat.ChatResponseFormat.ResponseFormat;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -77,6 +80,37 @@ void createChatCompletion() {
assertEquals(5, choices.size());
}

@Test
void createChatCompletion_with_json_mode() {
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(),
"Generate a random name and age json object. name field is a object that has first and last fields. age is a number.");
messages.add(systemMessage);

ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
.builder()
.model("gpt-3.5-turbo-1106")
.messages(messages)
.maxTokens(50)
.logitBias(new HashMap<>())
.responseFormat(ChatResponseFormat.builder().type(ResponseFormat.JSON).build())
.build();

ChatCompletionChoice choices = service.createChatCompletion(chatCompletionRequest)
.getChoices().get(0);
assertTrue(isValidJson(choices.getMessage().getContent()));
}

private boolean isValidJson(String jsonString) {
ObjectMapper objectMapper = new ObjectMapper();
try {
objectMapper.readTree(jsonString);
return true;
} catch (JsonProcessingException e) {
return false;
}
}

@Test
void streamChatCompletion() {
final List<ChatMessage> messages = new ArrayList<>();
Expand Down

0 comments on commit 2076e07

Please sign in to comment.