Skip to content

Commit

Permalink
Merge pull request #6 from launchableinc/support-json-mode
Browse files Browse the repository at this point in the history
Support json mode
  • Loading branch information
Konboi authored Mar 7, 2024
2 parents 39febda + 2076e07 commit 9e56642
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public class ChatCompletionRequest {
*/
List<ChatMessage> messages;

/**
* An object specifying the format that the model must output. Compatible with GPT-4 Turbo and all
* GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106.
* https://platform.openai.com/docs/api-reference/chat/create#chat-create-response_format
*/
@JsonProperty("response_format")
ChatResponseFormat responseFormat;

/**
* What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output
* more random, while lower values like 0.2 will make it more focused and deterministic.<br> We
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.launchableinc.openai.completion.chat;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Builder;
import lombok.Data;

/*
* OpenAI API Document:https://platform.openai.com/docs/api-reference/chat/create#chat-create-response_format
*/
@Data
@Builder
public class ChatResponseFormat {

private ResponseFormat type;

public enum ResponseFormat {
TEXT("text"), JSON("json_object");

private final String value;

ResponseFormat(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}
}

}
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 9e56642

Please sign in to comment.