Skip to content

Commit

Permalink
Merge pull request #31 from Lambdua/mapper_optimization
Browse files Browse the repository at this point in the history
objectMapper optimization
  • Loading branch information
Lambdua authored May 24, 2024
2 parents 860a580 + 4017236 commit 8ba13ac
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.theokanning.openai.assistants.run.MessageCreation;
import com.theokanning.openai.assistants.run.ToolCall;
import lombok.AllArgsConstructor;
Expand All @@ -18,8 +16,6 @@
@NoArgsConstructor
@AllArgsConstructor
public class StepDetails {


/**
* message_creation/tool_calls
*/
Expand All @@ -33,11 +29,4 @@ public class StepDetails {
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<ToolCall> toolCalls;

public String toPrettyString() {
try {
return new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.TextNode;
import com.theokanning.openai.utils.JsonUtil;

import java.io.IOException;

public class ChatFunctionCallArgumentsSerializerAndDeserializer {

private final static ObjectMapper MAPPER = new ObjectMapper();
private final static ObjectMapper MAPPER = JsonUtil.getInstance();

private ChatFunctionCallArgumentsSerializerAndDeserializer() {
}
Expand All @@ -30,7 +31,6 @@ public void serialize(JsonNode value, JsonGenerator gen, SerializerProvider seri
}

public static class Deserializer extends JsonDeserializer<JsonNode> {

private Deserializer() {
}

Expand All @@ -46,26 +46,20 @@ public JsonNode deserialize(JsonParser p, DeserializationContext ctxt) throws IO
json = MAPPER.writeValueAsString(json);
}

JsonNode node = null;
try {
JsonNode node = null;
try {
node = MAPPER.readTree(json);
} catch (JsonParseException ignored) {
}
if (node == null || node.getNodeType() == JsonNodeType.MISSING) {
node = MAPPER.readTree(p);
}
return node;
} catch (Exception ex) {
ex.printStackTrace();
return null;
node = MAPPER.readTree(json);
} catch (JsonParseException ignored) {
}
if (node == null || node.getNodeType() == JsonNodeType.MISSING) {
node = MAPPER.readTree(p);
}
return node;
}

private boolean isValidJson(String jsonString) {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode tree = objectMapper.readTree(jsonString);
JsonNode tree = MAPPER.readTree(jsonString);
return tree != null && (tree.isObject() || tree.isArray());
} catch (JsonProcessingException e) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.theokanning.openai.assistants.run.SubmitToolOutputRequestItem;
import com.theokanning.openai.completion.chat.FunctionMessage;
import com.theokanning.openai.completion.chat.ToolMessage;
import com.theokanning.openai.utils.JsonUtil;
import lombok.Getter;

import java.util.*;
Expand All @@ -24,11 +25,11 @@ public class FunctionExecutorManager {
private final ExecutorService executorService;

public FunctionExecutorManager() {
this(new ObjectMapper(), Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()), Collections.emptyList());
this(JsonUtil.getInstance(), Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()), Collections.emptyList());
}

public FunctionExecutorManager(List<FunctionDefinition> functionDefinitionList) {
this(new ObjectMapper(), Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()), functionDefinitionList);
this(JsonUtil.getInstance(), Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()), functionDefinitionList);
}

public FunctionExecutorManager(ObjectMapper mapper, ExecutorService executorService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.kjetland.jackson.jsonSchema.JsonSchemaConfig;
import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;
import com.theokanning.openai.utils.JsonUtil;

import java.io.IOException;

public class FunctionParametersSerializer extends JsonSerializer<FunctionDefinition> {

private final ObjectMapper mapper = new ObjectMapper();
private final JsonSchemaConfig config = JsonSchemaConfig.vanillaJsonSchemaDraft4();
private final JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper, config);

private final JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(JsonUtil.getInstance(), config);

@Override
public void serialize(FunctionDefinition value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
Expand All @@ -27,10 +26,10 @@ public void serialize(FunctionDefinition value, JsonGenerator gen, SerializerPro
parameterSchema.remove("$schema");
parameterSchema.remove("title");
parameterSchema.remove("additionalProperties");
gen.writeRawValue(mapper.writeValueAsString(parameterSchema));
gen.writeRawValue(JsonUtil.writeValueAsString(parameterSchema));
} else {
gen.writeFieldName("parameters");
gen.writeRawValue(mapper.writeValueAsString(value.getParametersDefinition()));
gen.writeRawValue(JsonUtil.writeValueAsString(value.getParametersDefinition()));
}
gen.writeEndObject();
}
Expand Down
38 changes: 38 additions & 0 deletions api/src/main/java/com/theokanning/openai/utils/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.theokanning.openai.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* @author LiangTao
* @date 2024年05月24 15:02
**/
public class JsonUtil {
private static final ObjectMapper mapper = ObjectMapperHolder.mapper;

public static ObjectMapper getInstance() {
return mapper;
}

public static String writeValueAsString(Object value) {
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

public static <T> T readValue(String content, Class<T> valueType) {
try {
return mapper.readValue(content, valueType);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private static class ObjectMapperHolder {
private static final ObjectMapper mapper = new ObjectMapper();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.kjetland.jackson.jsonSchema.JsonSchemaConfig;
import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;
import com.theokanning.openai.utils.JsonUtil;

import java.io.IOException;

@Deprecated
public class ChatFunctionParametersSerializer extends JsonSerializer<Class<?>> {

private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = JsonUtil.getInstance();
private final JsonSchemaConfig config = JsonSchemaConfig.vanillaJsonSchemaDraft4();
private final JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper, config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.theokanning.openai.completion.chat.ChatFunctionCall;
import com.theokanning.openai.completion.chat.FunctionMessage;
import com.theokanning.openai.completion.chat.ToolMessage;
import com.theokanning.openai.utils.JsonUtil;

import java.util.*;

Expand All @@ -20,7 +21,7 @@
@Deprecated
public class FunctionExecutor {

private ObjectMapper MAPPER = new ObjectMapper();
private ObjectMapper MAPPER = JsonUtil.getInstance();
private final Map<String, ChatFunction> FUNCTIONS = new HashMap<>();

public FunctionExecutor(List<ChatFunction> functions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.theokanning.openai.service.assistant_stream;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.theokanning.openai.assistants.StreamEvent;
import com.theokanning.openai.utils.JsonUtil;
import lombok.Getter;

/**
Expand All @@ -14,9 +13,6 @@ public class AssistantSSE {
private StreamEvent event;
private String data;

private static final ObjectMapper mapper = new ObjectMapper();


public AssistantSSE(StreamEvent event, String data) {
this.event = event;
this.data = data;
Expand All @@ -28,11 +24,7 @@ public boolean isDone() {
}

public <T> T getPojo() {
try {
return (T) mapper.readValue(data, event.dataClass);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return (T) JsonUtil.readValue(data, event.dataClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.theokanning.openai.assistants.run.ToolCallFunction;
import com.theokanning.openai.assistants.run_step.RunStep;
import com.theokanning.openai.assistants.run_step.RunStepDelta;
import com.theokanning.openai.utils.JsonUtil;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import lombok.Getter;
Expand All @@ -35,7 +36,7 @@ public class AssistantStreamManager {
private final List<MessageDelta> msgDeltas;
private final List<RunStepDelta> runStepDeltas;
private final List<AssistantSSE> eventMsgsHolder;
private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = JsonUtil.getInstance();
private MessageDelta accumulatedMessageDelta;

private RunStepDelta accumulatedRsd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.theokanning.openai.assistants.run.ToolCallFunction;
import com.theokanning.openai.assistants.run_step.RunStepDelta;
import com.theokanning.openai.assistants.run_step.StepDetails;
import com.theokanning.openai.utils.JsonUtil;
import lombok.SneakyThrows;

import java.util.List;
Expand All @@ -21,7 +22,7 @@
* @date 2024年05月02 15:56
**/
public class DeltaUtil {
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper mapper = JsonUtil.getInstance();

/**
* merge delta msg to accumulated delta msg
Expand Down

0 comments on commit 8ba13ac

Please sign in to comment.