Skip to content

Commit 9e56642

Browse files
authored
Merge pull request #6 from launchableinc/support-json-mode
Support json mode
2 parents 39febda + 2076e07 commit 9e56642

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

api/src/main/java/com/launchableinc/openai/completion/chat/ChatCompletionRequest.java

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public class ChatCompletionRequest {
2727
*/
2828
List<ChatMessage> messages;
2929

30+
/**
31+
* An object specifying the format that the model must output. Compatible with GPT-4 Turbo and all
32+
* GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106.
33+
* https://platform.openai.com/docs/api-reference/chat/create#chat-create-response_format
34+
*/
35+
@JsonProperty("response_format")
36+
ChatResponseFormat responseFormat;
37+
3038
/**
3139
* What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output
3240
* more random, while lower values like 0.2 will make it more focused and deterministic.<br> We
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.launchableinc.openai.completion.chat;
2+
3+
import com.fasterxml.jackson.annotation.JsonValue;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
7+
/*
8+
* OpenAI API Document:https://platform.openai.com/docs/api-reference/chat/create#chat-create-response_format
9+
*/
10+
@Data
11+
@Builder
12+
public class ChatResponseFormat {
13+
14+
private ResponseFormat type;
15+
16+
public enum ResponseFormat {
17+
TEXT("text"), JSON("json_object");
18+
19+
private final String value;
20+
21+
ResponseFormat(String value) {
22+
this.value = value;
23+
}
24+
25+
@JsonValue
26+
public String getValue() {
27+
return value;
28+
}
29+
}
30+
31+
}

service/src/test/java/com/launchableinc/openai/service/ChatCompletionTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
5+
import com.fasterxml.jackson.core.JsonProcessingException;
56
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
68
import com.fasterxml.jackson.databind.node.ObjectNode;
79
import com.launchableinc.openai.completion.chat.*;
10+
import com.launchableinc.openai.completion.chat.ChatResponseFormat.ResponseFormat;
811
import org.junit.jupiter.api.Assumptions;
912
import org.junit.jupiter.api.BeforeAll;
1013
import org.junit.jupiter.api.Test;
@@ -77,6 +80,37 @@ void createChatCompletion() {
7780
assertEquals(5, choices.size());
7881
}
7982

83+
@Test
84+
void createChatCompletion_with_json_mode() {
85+
final List<ChatMessage> messages = new ArrayList<>();
86+
final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(),
87+
"Generate a random name and age json object. name field is a object that has first and last fields. age is a number.");
88+
messages.add(systemMessage);
89+
90+
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
91+
.builder()
92+
.model("gpt-3.5-turbo-1106")
93+
.messages(messages)
94+
.maxTokens(50)
95+
.logitBias(new HashMap<>())
96+
.responseFormat(ChatResponseFormat.builder().type(ResponseFormat.JSON).build())
97+
.build();
98+
99+
ChatCompletionChoice choices = service.createChatCompletion(chatCompletionRequest)
100+
.getChoices().get(0);
101+
assertTrue(isValidJson(choices.getMessage().getContent()));
102+
}
103+
104+
private boolean isValidJson(String jsonString) {
105+
ObjectMapper objectMapper = new ObjectMapper();
106+
try {
107+
objectMapper.readTree(jsonString);
108+
return true;
109+
} catch (JsonProcessingException e) {
110+
return false;
111+
}
112+
}
113+
80114
@Test
81115
void streamChatCompletion() {
82116
final List<ChatMessage> messages = new ArrayList<>();

0 commit comments

Comments
 (0)