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

com.github.tjake.jlama.net.openai.model.ChatCompletionRequestMessage Maven / Gradle / Ivy

/*
 * OpenAI API
 * The OpenAI REST API. Please see https://platform.openai.com/docs/api-reference for more details.
 *
 * The version of the OpenAPI document: 2.1.0
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


package com.github.tjake.jlama.net.openai.model;

import java.util.Objects;
import java.util.Map;
import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonValue;
import com.github.tjake.jlama.net.openai.model.ChatCompletionMessageToolCall;
import com.github.tjake.jlama.net.openai.model.ChatCompletionRequestAssistantMessage;
import com.github.tjake.jlama.net.openai.model.ChatCompletionRequestAssistantMessageFunctionCall;
import com.github.tjake.jlama.net.openai.model.ChatCompletionRequestFunctionMessage;
import com.github.tjake.jlama.net.openai.model.ChatCompletionRequestSystemMessage;
import com.github.tjake.jlama.net.openai.model.ChatCompletionRequestToolMessage;
import com.github.tjake.jlama.net.openai.model.ChatCompletionRequestUserMessage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import jakarta.validation.constraints.*;
import jakarta.validation.Valid;
import org.hibernate.validator.constraints.*;
import com.github.tjake.jlama.net.openai.JSON;

import com.fasterxml.jackson.core.type.TypeReference;

import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.github.tjake.jlama.net.openai.JSON;

@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.7.0")
@JsonDeserialize(using = ChatCompletionRequestMessage.ChatCompletionRequestMessageDeserializer.class)
@JsonSerialize(using = ChatCompletionRequestMessage.ChatCompletionRequestMessageSerializer.class)
public class ChatCompletionRequestMessage extends AbstractOpenApiSchema {
    private static final Logger log = Logger.getLogger(ChatCompletionRequestMessage.class.getName());

    public static class ChatCompletionRequestMessageSerializer extends StdSerializer {
        public ChatCompletionRequestMessageSerializer(Class t) {
            super(t);
        }

        public ChatCompletionRequestMessageSerializer() {
            this(null);
        }

        @Override
        public void serialize(ChatCompletionRequestMessage value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
            jgen.writeObject(value.getActualInstance());
        }
    }

    public static class ChatCompletionRequestMessageDeserializer extends StdDeserializer {
        public ChatCompletionRequestMessageDeserializer() {
            this(ChatCompletionRequestMessage.class);
        }

        public ChatCompletionRequestMessageDeserializer(Class vc) {
            super(vc);
        }

        @Override
        public ChatCompletionRequestMessage deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            JsonNode tree = jp.readValueAsTree();
            Object deserialized = null;
            ChatCompletionRequestMessage newChatCompletionRequestMessage = new ChatCompletionRequestMessage();
            Map result2 = tree.traverse(jp.getCodec()).readValueAs(new TypeReference>() {});
            String discriminatorValue = (String)result2.get("role");
            switch (discriminatorValue) {
                case "assistant":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestAssistantMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                case "function":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestFunctionMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                case "system":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestSystemMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                case "tool":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestToolMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                case "user":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestUserMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                case "ChatCompletionRequestAssistantMessage":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestAssistantMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                case "ChatCompletionRequestFunctionMessage":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestFunctionMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                case "ChatCompletionRequestSystemMessage":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestSystemMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                case "ChatCompletionRequestToolMessage":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestToolMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                case "ChatCompletionRequestUserMessage":
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestUserMessage.class);
                    newChatCompletionRequestMessage.setActualInstance(deserialized);
                    return newChatCompletionRequestMessage;
                default:
                    log.log(Level.WARNING, String.format("Failed to lookup discriminator value `%s` for ChatCompletionRequestMessage. Possible values: assistant function system tool user ChatCompletionRequestAssistantMessage ChatCompletionRequestFunctionMessage ChatCompletionRequestSystemMessage ChatCompletionRequestToolMessage ChatCompletionRequestUserMessage", discriminatorValue));
            }

            boolean typeCoercion = ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS);
            int match = 0;
            JsonToken token = tree.traverse(jp.getCodec()).nextToken();
            // deserialize ChatCompletionRequestSystemMessage
            try {
                boolean attemptParsing = true;
                if (attemptParsing) {
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestSystemMessage.class);
                    // TODO: there is no validation against JSON schema constraints
                    // (min, max, enum, pattern...), this does not perform a strict JSON
                    // validation, which means the 'match' count may be higher than it should be.
                    match++;
                    log.log(Level.FINER, "Input data matches schema 'ChatCompletionRequestSystemMessage'");
                }
            } catch (Exception e) {
                // deserialization failed, continue
                log.log(Level.FINER, "Input data does not match schema 'ChatCompletionRequestSystemMessage'", e);
            }

            // deserialize ChatCompletionRequestUserMessage
            try {
                boolean attemptParsing = true;
                if (attemptParsing) {
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestUserMessage.class);
                    // TODO: there is no validation against JSON schema constraints
                    // (min, max, enum, pattern...), this does not perform a strict JSON
                    // validation, which means the 'match' count may be higher than it should be.
                    match++;
                    log.log(Level.FINER, "Input data matches schema 'ChatCompletionRequestUserMessage'");
                }
            } catch (Exception e) {
                // deserialization failed, continue
                log.log(Level.FINER, "Input data does not match schema 'ChatCompletionRequestUserMessage'", e);
            }

            // deserialize ChatCompletionRequestAssistantMessage
            try {
                boolean attemptParsing = true;
                if (attemptParsing) {
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestAssistantMessage.class);
                    // TODO: there is no validation against JSON schema constraints
                    // (min, max, enum, pattern...), this does not perform a strict JSON
                    // validation, which means the 'match' count may be higher than it should be.
                    match++;
                    log.log(Level.FINER, "Input data matches schema 'ChatCompletionRequestAssistantMessage'");
                }
            } catch (Exception e) {
                // deserialization failed, continue
                log.log(Level.FINER, "Input data does not match schema 'ChatCompletionRequestAssistantMessage'", e);
            }

            // deserialize ChatCompletionRequestToolMessage
            try {
                boolean attemptParsing = true;
                if (attemptParsing) {
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestToolMessage.class);
                    // TODO: there is no validation against JSON schema constraints
                    // (min, max, enum, pattern...), this does not perform a strict JSON
                    // validation, which means the 'match' count may be higher than it should be.
                    match++;
                    log.log(Level.FINER, "Input data matches schema 'ChatCompletionRequestToolMessage'");
                }
            } catch (Exception e) {
                // deserialization failed, continue
                log.log(Level.FINER, "Input data does not match schema 'ChatCompletionRequestToolMessage'", e);
            }

            // deserialize ChatCompletionRequestFunctionMessage
            try {
                boolean attemptParsing = true;
                if (attemptParsing) {
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(ChatCompletionRequestFunctionMessage.class);
                    // TODO: there is no validation against JSON schema constraints
                    // (min, max, enum, pattern...), this does not perform a strict JSON
                    // validation, which means the 'match' count may be higher than it should be.
                    match++;
                    log.log(Level.FINER, "Input data matches schema 'ChatCompletionRequestFunctionMessage'");
                }
            } catch (Exception e) {
                // deserialization failed, continue
                log.log(Level.FINER, "Input data does not match schema 'ChatCompletionRequestFunctionMessage'", e);
            }

            if (match == 1) {
                ChatCompletionRequestMessage ret = new ChatCompletionRequestMessage();
                ret.setActualInstance(deserialized);
                return ret;
            }
            throw new IOException(String.format("Failed deserialization for ChatCompletionRequestMessage: %d classes match result, expected 1", match));
        }

        /**
         * Handle deserialization of the 'null' value.
         */
        @Override
        public ChatCompletionRequestMessage getNullValue(DeserializationContext ctxt) throws JsonMappingException {
            throw new JsonMappingException(ctxt.getParser(), "ChatCompletionRequestMessage cannot be null");
        }
    }

    // store a list of schema names defined in oneOf
    public static final Map> schemas = new HashMap<>();

    public ChatCompletionRequestMessage() {
        super("oneOf", Boolean.FALSE);
    }

    public ChatCompletionRequestMessage(ChatCompletionRequestSystemMessage o) {
        super("oneOf", Boolean.FALSE);
        setActualInstance(o);
    }

    public ChatCompletionRequestMessage(ChatCompletionRequestUserMessage o) {
        super("oneOf", Boolean.FALSE);
        setActualInstance(o);
    }

    public ChatCompletionRequestMessage(ChatCompletionRequestAssistantMessage o) {
        super("oneOf", Boolean.FALSE);
        setActualInstance(o);
    }

    public ChatCompletionRequestMessage(ChatCompletionRequestToolMessage o) {
        super("oneOf", Boolean.FALSE);
        setActualInstance(o);
    }

    public ChatCompletionRequestMessage(ChatCompletionRequestFunctionMessage o) {
        super("oneOf", Boolean.FALSE);
        setActualInstance(o);
    }

    static {
        schemas.put("ChatCompletionRequestAssistantMessage", new GenericType() {
        });
        schemas.put("ChatCompletionRequestFunctionMessage", new GenericType() {
        });
        schemas.put("ChatCompletionRequestSystemMessage", new GenericType() {
        });
        schemas.put("ChatCompletionRequestToolMessage", new GenericType() {
        });
        schemas.put("ChatCompletionRequestUserMessage", new GenericType() {
        });
        JSON.registerDescendants(ChatCompletionRequestMessage.class, Collections.unmodifiableMap(schemas));
        // Initialize and register the discriminator mappings.
        Map> mappings = new HashMap<>();
        mappings.put("assistant", ChatCompletionRequestAssistantMessage.class);
        mappings.put("function", ChatCompletionRequestFunctionMessage.class);
        mappings.put("system", ChatCompletionRequestSystemMessage.class);
        mappings.put("tool", ChatCompletionRequestToolMessage.class);
        mappings.put("user", ChatCompletionRequestUserMessage.class);
        mappings.put("ChatCompletionRequestAssistantMessage", ChatCompletionRequestAssistantMessage.class);
        mappings.put("ChatCompletionRequestFunctionMessage", ChatCompletionRequestFunctionMessage.class);
        mappings.put("ChatCompletionRequestSystemMessage", ChatCompletionRequestSystemMessage.class);
        mappings.put("ChatCompletionRequestToolMessage", ChatCompletionRequestToolMessage.class);
        mappings.put("ChatCompletionRequestUserMessage", ChatCompletionRequestUserMessage.class);
        mappings.put("ChatCompletionRequestMessage", ChatCompletionRequestMessage.class);
        JSON.registerDiscriminator(ChatCompletionRequestMessage.class, "role", mappings);
    }

    @Override
    public Map> getSchemas() {
        return ChatCompletionRequestMessage.schemas;
    }

    /**
     * Set the instance that matches the oneOf child schema, check
     * the instance parameter is valid against the oneOf child schemas:
     * ChatCompletionRequestAssistantMessage, ChatCompletionRequestFunctionMessage, ChatCompletionRequestSystemMessage, ChatCompletionRequestToolMessage, ChatCompletionRequestUserMessage
     *
     * It could be an instance of the 'oneOf' schemas.
     * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
     */
    @Override
    public void setActualInstance(Object instance) {
        if (JSON.isInstanceOf(ChatCompletionRequestSystemMessage.class, instance, new HashSet<>())) {
            super.setActualInstance(instance);
            return;
        }

        if (JSON.isInstanceOf(ChatCompletionRequestUserMessage.class, instance, new HashSet<>())) {
            super.setActualInstance(instance);
            return;
        }

        if (JSON.isInstanceOf(ChatCompletionRequestAssistantMessage.class, instance, new HashSet<>())) {
            super.setActualInstance(instance);
            return;
        }

        if (JSON.isInstanceOf(ChatCompletionRequestToolMessage.class, instance, new HashSet<>())) {
            super.setActualInstance(instance);
            return;
        }

        if (JSON.isInstanceOf(ChatCompletionRequestFunctionMessage.class, instance, new HashSet<>())) {
            super.setActualInstance(instance);
            return;
        }

        throw new RuntimeException("Invalid instance type. Must be ChatCompletionRequestAssistantMessage, ChatCompletionRequestFunctionMessage, ChatCompletionRequestSystemMessage, ChatCompletionRequestToolMessage, ChatCompletionRequestUserMessage");
    }

    /**
     * Get the actual instance, which can be the following:
     * ChatCompletionRequestAssistantMessage, ChatCompletionRequestFunctionMessage, ChatCompletionRequestSystemMessage, ChatCompletionRequestToolMessage, ChatCompletionRequestUserMessage
     *
     * @return The actual instance (ChatCompletionRequestAssistantMessage, ChatCompletionRequestFunctionMessage, ChatCompletionRequestSystemMessage, ChatCompletionRequestToolMessage, ChatCompletionRequestUserMessage)
     */
    @Override
    public Object getActualInstance() {
        return super.getActualInstance();
    }

    /**
     * Get the actual instance of `ChatCompletionRequestSystemMessage`. If the actual instance is not `ChatCompletionRequestSystemMessage`,
     * the ClassCastException will be thrown.
     *
     * @return The actual instance of `ChatCompletionRequestSystemMessage`
     * @throws ClassCastException if the instance is not `ChatCompletionRequestSystemMessage`
     */
    public ChatCompletionRequestSystemMessage getChatCompletionRequestSystemMessage() throws ClassCastException {
        return (ChatCompletionRequestSystemMessage)super.getActualInstance();
    }

    /**
     * Get the actual instance of `ChatCompletionRequestUserMessage`. If the actual instance is not `ChatCompletionRequestUserMessage`,
     * the ClassCastException will be thrown.
     *
     * @return The actual instance of `ChatCompletionRequestUserMessage`
     * @throws ClassCastException if the instance is not `ChatCompletionRequestUserMessage`
     */
    public ChatCompletionRequestUserMessage getChatCompletionRequestUserMessage() throws ClassCastException {
        return (ChatCompletionRequestUserMessage)super.getActualInstance();
    }

    /**
     * Get the actual instance of `ChatCompletionRequestAssistantMessage`. If the actual instance is not `ChatCompletionRequestAssistantMessage`,
     * the ClassCastException will be thrown.
     *
     * @return The actual instance of `ChatCompletionRequestAssistantMessage`
     * @throws ClassCastException if the instance is not `ChatCompletionRequestAssistantMessage`
     */
    public ChatCompletionRequestAssistantMessage getChatCompletionRequestAssistantMessage() throws ClassCastException {
        return (ChatCompletionRequestAssistantMessage)super.getActualInstance();
    }

    /**
     * Get the actual instance of `ChatCompletionRequestToolMessage`. If the actual instance is not `ChatCompletionRequestToolMessage`,
     * the ClassCastException will be thrown.
     *
     * @return The actual instance of `ChatCompletionRequestToolMessage`
     * @throws ClassCastException if the instance is not `ChatCompletionRequestToolMessage`
     */
    public ChatCompletionRequestToolMessage getChatCompletionRequestToolMessage() throws ClassCastException {
        return (ChatCompletionRequestToolMessage)super.getActualInstance();
    }

    /**
     * Get the actual instance of `ChatCompletionRequestFunctionMessage`. If the actual instance is not `ChatCompletionRequestFunctionMessage`,
     * the ClassCastException will be thrown.
     *
     * @return The actual instance of `ChatCompletionRequestFunctionMessage`
     * @throws ClassCastException if the instance is not `ChatCompletionRequestFunctionMessage`
     */
    public ChatCompletionRequestFunctionMessage getChatCompletionRequestFunctionMessage() throws ClassCastException {
        return (ChatCompletionRequestFunctionMessage)super.getActualInstance();
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy