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

io.quarkiverse.langchain4j.guardrails.JsonGuardrailsUtils Maven / Gradle / Ivy

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

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

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

@ApplicationScoped
public class JsonGuardrailsUtils {

    @Inject
    ObjectMapper objectMapper;

    private JsonGuardrailsUtils() {
    }

    public String trimNonJson(String llmResponse) {
        int jsonMapStart = llmResponse.indexOf('{');
        int jsonListStart = llmResponse.indexOf('[');
        if (jsonMapStart < 0 && jsonListStart < 0) {
            return null;
        }
        boolean isJsonMap = jsonMapStart >= 0 && (jsonMapStart < jsonListStart || jsonListStart < 0);

        int jsonStart = isJsonMap ? jsonMapStart : jsonListStart;
        int jsonEnd = isJsonMap ? llmResponse.lastIndexOf('}') : llmResponse.lastIndexOf(']');
        return jsonEnd >= 0 && jsonStart < jsonEnd ? llmResponse.substring(jsonStart, jsonEnd + 1) : null;
    }

    public  T deserialize(String json, Class expectedOutputClass) {
        try {
            return objectMapper.readValue(json, expectedOutputClass);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    public  T deserialize(String json, TypeReference expectedOutputType) {
        try {
            return objectMapper.readValue(json, expectedOutputType);
        } catch (JsonProcessingException e) {
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy