Skip to content

Commit

Permalink
Fix serializing type twice
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed May 15, 2024
1 parent fc39bf2 commit 1208a1b
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import java.util.Map;

/** Details of the tool call the {@link ThreadRunStepDelta} was involved in. */
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = DeltaToolCall.CodeInterpreterToolCall.class,
Expand Down Expand Up @@ -53,7 +56,10 @@ public String type() {
*/
public record CodeInterpreter(String input, List<Output> outputs) {

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = Output.LogOutput.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public record ThreadMessage(
/** On an incomplete message, details about why the message is incomplete. */
public record IncompleteDetails(String reason) {}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = Content.ImageFileContent.class,
Expand Down Expand Up @@ -56,7 +59,10 @@ public String type() {

public record Text(String value, List<Annotation> annotations) {

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = Annotation.FileCitationAnnotation.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ public record ThreadMessageDelta(String id, Delta delta) implements AssistantStr
/** The delta containing the fields that have changed on the Message. */
public record Delta(String role, List<Content> content) {

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = Content.ImageFileContent.class,
Expand Down Expand Up @@ -44,7 +47,10 @@ public String type() {

public record Text(String value, List<Annotation> annotations) {

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = Annotation.FileCitationAnnotation.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public record ThreadRunStep(
implements AssistantStreamEvent.Data {

/** The details of the run step. */
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = StepDetails.MessageCreationStepDetails.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public record ThreadRunStepDelta(String id, Delta delta) implements AssistantStr
public record Delta(StepDetails stepDetails) {}

/** The details of the run step. */
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = StepDetails.MessageCreationStepDetails.class,
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/github/stefanbratanov/jvm/openai/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = Tool.CodeInterpreterTool.class,
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/github/stefanbratanov/jvm/openai/ToolCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import java.util.List;
import java.util.Map;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = ToolCall.CodeInterpreterToolCall.class,
Expand Down Expand Up @@ -48,7 +51,10 @@ public String type() {
*/
public record CodeInterpreter(String input, List<Output> outputs) {

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(
value = Output.LogOutput.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.stefanbratanov.jvm.openai.DeltaToolCall.CodeInterpreterToolCall.CodeInterpreter;
import io.github.stefanbratanov.jvm.openai.ThreadMessage.Content.TextContent;
import io.github.stefanbratanov.jvm.openai.ThreadMessage.Content.TextContent.Text.Annotation;
import io.github.stefanbratanov.jvm.openai.ThreadMessage.Content.TextContent.Text.Annotation.FileCitationAnnotation.FileCitation;
import io.github.stefanbratanov.jvm.openai.ThreadMessageDelta.Delta;
import io.github.stefanbratanov.jvm.openai.ThreadMessageDelta.Delta.Content.TextContent.Text;
import io.github.stefanbratanov.jvm.openai.ThreadMessageDelta.Delta.Content.TextContent.Text.Annotation.FilePathAnnotation.FilePath;
import io.github.stefanbratanov.jvm.openai.ThreadRunStep.StepDetails;
import io.github.stefanbratanov.jvm.openai.ThreadRunStepDelta.StepDetails.MessageCreationStepDetails;
import io.github.stefanbratanov.jvm.openai.ThreadRunStepDelta.StepDetails.MessageCreationStepDetails.MessageCreation;
import io.github.stefanbratanov.jvm.openai.ToolCall.CodeInterpreterToolCall.CodeInterpreter.Output;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
Expand Down Expand Up @@ -162,4 +173,75 @@ void serializesFunction() throws JsonProcessingException, JSONException {
objectMapper.writeValueAsString(function),
JSONCompareMode.STRICT);
}

@Test
void doesNotSerializeTypeTwiceForJsonSubTypesAnnotatedClasses() throws JsonProcessingException {
Tool.FileSearchTool fileSearchTool = Tool.fileSearchTool();

assertThat(objectMapper.writeValueAsString(fileSearchTool))
.isEqualTo("{\"type\":\"file_search\"}");

ToolCall.FileSearchToolCall fileSearchToolCall = ToolCall.fileSearchToolCall("foobar");

assertThat(objectMapper.writeValueAsString(fileSearchToolCall))
.isEqualTo("{\"id\":\"foobar\",\"file_search\":{},\"type\":\"file_search\"}");

DeltaToolCall.FileSearchToolCall deltaFileSearchToolCall =
DeltaToolCall.fileSearchToolCall(0, "foobar");

assertThat(objectMapper.writeValueAsString(deltaFileSearchToolCall))
.isEqualTo("{\"index\":0,\"id\":\"foobar\",\"file_search\":{},\"type\":\"file_search\"}");

TextContent textContent = new TextContent(new TextContent.Text("foobar", List.of()));

assertThat(objectMapper.writeValueAsString(textContent))
.isEqualTo("{\"text\":{\"value\":\"foobar\",\"annotations\":[]},\"type\":\"text\"}");

MessageCreationStepDetails messageCreationStepDetails =
new MessageCreationStepDetails(new MessageCreation("foobar"));

assertThat(objectMapper.writeValueAsString(messageCreationStepDetails))
.isEqualTo(
"{\"message_creation\":{\"message_id\":\"foobar\"},\"type\":\"message_creation\"}");

Delta.Content.TextContent deltaTextContent =
new Delta.Content.TextContent(0, new Text("foobar", List.of()));

assertThat(objectMapper.writeValueAsString(deltaTextContent))
.isEqualTo(
"{\"index\":0,\"text\":{\"value\":\"foobar\",\"annotations\":[]},\"type\":\"text\"}");

StepDetails.MessageCreationStepDetails runMessageCreationStepDetails =
new StepDetails.MessageCreationStepDetails(
new StepDetails.MessageCreationStepDetails.MessageCreation("foobar"));

assertThat(objectMapper.writeValueAsString(runMessageCreationStepDetails))
.isEqualTo(
"{\"message_creation\":{\"message_id\":\"foobar\"},\"type\":\"message_creation\"}");

Output.LogOutput logOutput = Output.logOutput("foobar");

assertThat(objectMapper.writeValueAsString(logOutput))
.isEqualTo("{\"logs\":\"foobar\",\"type\":\"logs\"}");

DeltaToolCall.CodeInterpreterToolCall.CodeInterpreter.Output.LogOutput deltaLogOutput =
CodeInterpreter.Output.logOutput(0, "foobar");

assertThat(objectMapper.writeValueAsString(deltaLogOutput))
.isEqualTo("{\"index\":0,\"logs\":\"foobar\",\"type\":\"logs\"}");

Annotation annotation =
new Annotation.FileCitationAnnotation("foobar", new FileCitation("foobar", "foobar"), 0, 0);

assertThat(objectMapper.writeValueAsString(annotation))
.isEqualTo(
"{\"text\":\"foobar\",\"file_citation\":{\"file_id\":\"foobar\",\"quote\":\"foobar\"},\"start_index\":0,\"end_index\":0,\"type\":\"file_citation\"}");

Delta.Content.TextContent.Text.Annotation.FilePathAnnotation deltaAnnotation =
new Text.Annotation.FilePathAnnotation(0, "foobar", new FilePath("foobar"), 0, 0);

assertThat(objectMapper.writeValueAsString(deltaAnnotation))
.isEqualTo(
"{\"index\":0,\"text\":\"foobar\",\"file_path\":{\"file_id\":\"foobar\"},\"start_index\":0,\"end_index\":0,\"type\":\"file_path\"}");
}
}

0 comments on commit 1208a1b

Please sign in to comment.