Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support json mode #6

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed locally then, OpenAI API returned

{
  "name": {
    "first": "Samantha",
    "last": "Jones"
  },
  "age": 27
}

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
Loading