All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkiverse.langchain4j.runtime.jackson.AiMessageDeserializer Maven / Gradle / Ivy

There is a newer version: 0.21.0
Show newest version
package io.quarkiverse.langchain4j.runtime.jackson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.data.message.AiMessage;

public class AiMessageDeserializer extends StdDeserializer {
    public AiMessageDeserializer() {
        super(AiMessage.class);
    }

    @Override
    public AiMessage deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = p.getCodec().readTree(p);
        if (node.has("toolExecutionRequests")) {
            JsonNode toolExecutionRequestsNode = node.get("toolExecutionRequests");
            JsonParser toolExecutionsRequestsParser = toolExecutionRequestsNode.traverse();
            toolExecutionsRequestsParser.nextToken();
            List toolExecutionRequests = ctxt.readValue(toolExecutionsRequestsParser,
                    ctxt.getTypeFactory().constructCollectionType(ArrayList.class,
                            ToolExecutionRequest.class));
            return new AiMessage(toolExecutionRequests);
        } else {
            return new AiMessage(node.get("text").asText());
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy