Migrate Redis from gson to jackson (#1694)
## Issue Closes #1685 ## Change Migrate `langchain4j-redis` from gson to jackson ## General checklist <!-- Please double-check the following points and mark them like this: [X] --> - [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 <!-- Before adding documentation and example(s) (below), please wait until the PR is reviewed and approved. --> - [ ] 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)
This commit is contained in:
parent
29b7456167
commit
507498f707
|
@ -26,6 +26,11 @@
|
|||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
|
|
|
@ -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<TextSegment> {
|
||||
|
||||
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<TextSegment> {
|
|||
.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)
|
||||
|
|
Loading…
Reference in New Issue