From 4f8d05dff690451ff17dd27c61087bd6fd4f8b7f Mon Sep 17 00:00:00 2001 From: humberto Date: Mon, 15 Jul 2024 16:52:36 +0200 Subject: [PATCH] Fiw role serialization, Ollama 2.5 + does not accept anymore in upper case. --- .../quarkiverse/langchain4j/ollama/Role.java | 3 +++ .../runtime/jackson/RoleSerializer.java | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 model-providers/ollama/runtime/src/main/java/io/quarkiverse/langchain4j/ollama/runtime/jackson/RoleSerializer.java diff --git a/model-providers/ollama/runtime/src/main/java/io/quarkiverse/langchain4j/ollama/Role.java b/model-providers/ollama/runtime/src/main/java/io/quarkiverse/langchain4j/ollama/Role.java index 2878c8ac9..7715af519 100644 --- a/model-providers/ollama/runtime/src/main/java/io/quarkiverse/langchain4j/ollama/Role.java +++ b/model-providers/ollama/runtime/src/main/java/io/quarkiverse/langchain4j/ollama/Role.java @@ -1,10 +1,13 @@ package io.quarkiverse.langchain4j.ollama; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import io.quarkiverse.langchain4j.ollama.runtime.jackson.RoleDeserializer; +import io.quarkiverse.langchain4j.ollama.runtime.jackson.RoleSerializer; @JsonDeserialize(using = RoleDeserializer.class) +@JsonSerialize(using = RoleSerializer.class) public enum Role { SYSTEM, diff --git a/model-providers/ollama/runtime/src/main/java/io/quarkiverse/langchain4j/ollama/runtime/jackson/RoleSerializer.java b/model-providers/ollama/runtime/src/main/java/io/quarkiverse/langchain4j/ollama/runtime/jackson/RoleSerializer.java new file mode 100644 index 000000000..461649403 --- /dev/null +++ b/model-providers/ollama/runtime/src/main/java/io/quarkiverse/langchain4j/ollama/runtime/jackson/RoleSerializer.java @@ -0,0 +1,21 @@ +package io.quarkiverse.langchain4j.ollama.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 io.quarkiverse.langchain4j.ollama.Role; + +public class RoleSerializer extends StdSerializer { + public RoleSerializer() { + super(Role.class); + } + + @Override + public void serialize(Role role, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + jsonGenerator.writeString(role.toString().toLowerCase(Locale.ROOT)); + } +}