From fc328cccc8ea3a7faeb8036a45ab0c852ed5a7d5 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Wed, 28 Feb 2024 11:56:14 +0200 Subject: [PATCH] Add Content serialization support Fixes: #333 --- .../openai/runtime/jackson/ContentMixin.java | 11 ++++++++++ .../runtime/jackson/ContentTypeMixin.java | 11 ++++++++++ .../jackson/ContentTypeSerializer.java | 21 +++++++++++++++++++ .../openai/runtime/jackson/ImageUrlMixin.java | 11 ++++++++++ 4 files changed, 54 insertions(+) create mode 100644 openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentMixin.java create mode 100644 openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentTypeMixin.java create mode 100644 openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentTypeSerializer.java create mode 100644 openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ImageUrlMixin.java diff --git a/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentMixin.java b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentMixin.java new file mode 100644 index 000000000..96685117a --- /dev/null +++ b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentMixin.java @@ -0,0 +1,11 @@ +package io.quarkiverse.langchain4j.openai.runtime.jackson; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; + +import dev.ai4j.openai4j.chat.Content; +import io.quarkus.jackson.JacksonMixin; + +@JacksonMixin(Content.class) +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE) +public abstract class ContentMixin { +} diff --git a/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentTypeMixin.java b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentTypeMixin.java new file mode 100644 index 000000000..3df3edf05 --- /dev/null +++ b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentTypeMixin.java @@ -0,0 +1,11 @@ +package io.quarkiverse.langchain4j.openai.runtime.jackson; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import dev.ai4j.openai4j.chat.ContentType; +import io.quarkus.jackson.JacksonMixin; + +@JacksonMixin(ContentType.class) +@JsonSerialize(using = ContentTypeSerializer.class) +public abstract class ContentTypeMixin { +} diff --git a/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentTypeSerializer.java b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentTypeSerializer.java new file mode 100644 index 000000000..906f3c80b --- /dev/null +++ b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ContentTypeSerializer.java @@ -0,0 +1,21 @@ +package io.quarkiverse.langchain4j.openai.runtime.jackson; + +import java.io.IOException; +import java.util.Locale; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +import dev.ai4j.openai4j.chat.ContentType; + +public class ContentTypeSerializer extends StdSerializer { + public ContentTypeSerializer() { + super(ContentType.class); + } + + @Override + public void serialize(ContentType value, JsonGenerator gen, SerializerProvider provider) throws IOException { + gen.writeString(value.toString().toLowerCase(Locale.ROOT)); + } +} diff --git a/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ImageUrlMixin.java b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ImageUrlMixin.java new file mode 100644 index 000000000..c938ed793 --- /dev/null +++ b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/jackson/ImageUrlMixin.java @@ -0,0 +1,11 @@ +package io.quarkiverse.langchain4j.openai.runtime.jackson; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; + +import dev.ai4j.openai4j.chat.ImageUrl; +import io.quarkus.jackson.JacksonMixin; + +@JacksonMixin(ImageUrl.class) +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE) +public abstract class ImageUrlMixin { +}