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

io.quarkiverse.langchain4j.watsonx.prompt.PromptFormatterUtil Maven / Gradle / Ivy

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

import java.util.Collection;
import java.util.Map;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;

/**
 * Utility class for handling various prompt-formatter related tasks.
 */
public class PromptFormatterUtil {

    /**
     * Converts a Map into a JSON string representation.
     *
     * @param map the map to convert
     * @return a JSON string representing the map
     */
    @SuppressWarnings("unchecked")
    public static JsonObject convert(Map map) {

        var json = Json.createObjectBuilder();

        for (Map.Entry entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            if (value instanceof Map cValue) {
                json.add(key, convert(cValue));
            } else if (value instanceof Collection cValue) {
                json.add(key, convert(cValue));
            } else if (value instanceof String cValue) {
                json.add(key, cValue);
            } else if (value instanceof Integer cValue) {
                json.add(key, cValue);
            } else if (value instanceof Boolean cValue) {
                json.add(key, cValue);
            } else {
                json.add(key, value.toString());
            }
        }
        return json.build();
    }

    //
    // Converts a Collection of objects into a JsonArray.
    //
    @SuppressWarnings("unchecked")
    private static JsonArray convert(Collection list) {

        JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();

        for (Object value : list) {
            if (value instanceof Map cValue) {
                jsonArrayBuilder.add(convert(cValue));
            } else if (value instanceof String cValue) {
                jsonArrayBuilder.add(cValue);
            } else if (value instanceof Integer cValue) {
                jsonArrayBuilder.add(cValue);
            } else if (value instanceof Boolean cValue) {
                jsonArrayBuilder.add(cValue);
            } else {
                jsonArrayBuilder.add(value.toString());
            }
        }
        return jsonArrayBuilder.build();
    }
}