From 507498f707388c8b961bff6d9d07ec11a2be8ce5 Mon Sep 17 00:00:00 2001 From: ZYinNJU <1754350460@qq.com> Date: Wed, 4 Sep 2024 20:01:53 +0800 Subject: [PATCH] Migrate Redis from gson to jackson (#1694) ## Issue Closes #1685 ## Change Migrate `langchain4j-redis` from gson to jackson ## General checklist - [x] There are no breaking changes - [ ] I have added unit and integration tests for my change - [x] I have manually run all the unit and integration tests in the module I have added/changed, and they are all green - [x] I have manually run all the unit and integration tests in the [core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core) and [main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j) modules, and they are all green - [ ] I have added/updated the [documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs) - [ ] I have added an example in the [examples repo](https://github.com/langchain4j/langchain4j-examples) (only for "big" features) - [ ] I have added/updated [Spring Boot starter(s)](https://github.com/langchain4j/langchain4j-spring) (if applicable) --- langchain4j-redis/pom.xml | 5 +++++ .../store/embedding/redis/RedisEmbeddingStore.java | 14 +++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/langchain4j-redis/pom.xml b/langchain4j-redis/pom.xml index 06e3a5f84..eb9b6d314 100644 --- a/langchain4j-redis/pom.xml +++ b/langchain4j-redis/pom.xml @@ -26,6 +26,11 @@ jedis + + com.fasterxml.jackson.core + jackson-databind + + org.slf4j slf4j-api diff --git a/langchain4j-redis/src/main/java/dev/langchain4j/store/embedding/redis/RedisEmbeddingStore.java b/langchain4j-redis/src/main/java/dev/langchain4j/store/embedding/redis/RedisEmbeddingStore.java index f3180dd75..64dd90a45 100644 --- a/langchain4j-redis/src/main/java/dev/langchain4j/store/embedding/redis/RedisEmbeddingStore.java +++ b/langchain4j-redis/src/main/java/dev/langchain4j/store/embedding/redis/RedisEmbeddingStore.java @@ -1,6 +1,7 @@ package dev.langchain4j.store.embedding.redis; -import com.google.gson.Gson; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import dev.langchain4j.data.document.Metadata; import dev.langchain4j.data.embedding.Embedding; import dev.langchain4j.data.segment.TextSegment; @@ -13,6 +14,7 @@ import redis.clients.jedis.Pipeline; import redis.clients.jedis.json.Path2; import redis.clients.jedis.search.*; +import java.io.IOException; import java.util.*; import static dev.langchain4j.internal.Utils.*; @@ -33,7 +35,7 @@ import static redis.clients.jedis.search.RediSearchUtil.ToByteArray; public class RedisEmbeddingStore implements EmbeddingStore { private static final Logger log = LoggerFactory.getLogger(RedisEmbeddingStore.class); - private static final Gson GSON = new Gson(); + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private final JedisPooled client; private final RedisSchema schema; @@ -206,7 +208,13 @@ public class RedisEmbeddingStore implements EmbeddingStore { .collect(toMap(metadataKey -> metadataKey, document::getString)); embedded = new TextSegment(text, new Metadata(metadata)); } - Embedding embedding = new Embedding(GSON.fromJson(document.getString(schema.vectorFieldName()), float[].class)); + Embedding embedding; + try { + float[] vectors = OBJECT_MAPPER.readValue(document.getString(schema.vectorFieldName()), float[].class); + embedding = new Embedding(vectors); + } catch (JsonProcessingException e) { + throw new RedisRequestFailedException("failed to parse embedding", e); + } return new EmbeddingMatch<>(score, id, embedding, embedded); }) .filter(embeddingMatch -> embeddingMatch.score() >= minScore)