Java: Update redis and spring-starter versions (#4998)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
This commit is contained in:
John Oliver
2024-02-13 18:44:04 +00:00
committed by GitHub
parent d35757fa09
commit 2110556d55
6 changed files with 65 additions and 60 deletions
@@ -4,7 +4,7 @@
<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-parent</artifactId>
<version>0.2.10-alpha-SNAPSHOT</version>
<version>0.2.14-alpha-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
@@ -13,15 +13,6 @@
<name>Semantic Kernel Redis Memory Connector</name>
<description>Defines connector for Redis to be used with the Semantic Kernel</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
@@ -40,15 +31,18 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
@@ -56,19 +50,9 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- <version>3.8.1</version> -->
<configuration>
<source>17</source>
<target>17</target>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -8,7 +8,6 @@ import com.microsoft.semantickernel.memory.MemoryRecord;
import com.microsoft.semantickernel.memory.MemoryRecordMetadata;
import java.nio.charset.StandardCharsets;
import java.util.*;
import javax.annotation.Nullable;
/** Represents an entry in the Semantic Kernel Memory Table. */
public class JsonMemoryRecord {
@@ -92,7 +91,7 @@ public class JsonMemoryRecord {
* @return Embedding vector.
*/
public List<Float> getEmbedding() {
return embedding;
return Collections.unmodifiableList(embedding);
}
/**
@@ -152,18 +151,12 @@ public class JsonMemoryRecord {
// to encode values with a URL-safe algorithm.
// <param name="realId">Original Id</param>
// <returns>Encoded id</returns>
static String encodeId(@Nullable String realId) {
if (realId == null) {
return "";
}
static String encodeId(String realId) {
byte[] bytes = Base64.getUrlEncoder().encode(realId.getBytes(StandardCharsets.UTF_8));
return new String(bytes, StandardCharsets.UTF_8);
}
static String decodeId(@Nullable String encodedId) {
if (encodedId == null) {
return "";
}
static String decodeId(String encodedId) {
byte[] bytes = Base64.getUrlDecoder().decode(encodedId.getBytes(StandardCharsets.UTF_8));
return new String(bytes, StandardCharsets.UTF_8);
}
@@ -1,27 +1,42 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.connectors.memory.redis;
import com.microsoft.semantickernel.SKException;
import com.microsoft.semantickernel.ai.embeddings.Embedding;
import com.microsoft.semantickernel.memory.MemoryException;
import com.microsoft.semantickernel.memory.MemoryException.ErrorCodes;
import com.microsoft.semantickernel.memory.MemoryRecord;
import com.microsoft.semantickernel.memory.MemoryStore;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.MessageFormat;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.resps.ScanResult;
import redis.clients.jedis.search.*;
import redis.clients.jedis.search.Document;
import redis.clients.jedis.search.IndexDefinition;
import redis.clients.jedis.search.IndexOptions;
import redis.clients.jedis.search.Query;
import redis.clients.jedis.search.Schema;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.search.schemafields.VectorField.VectorAlgorithm;
/**
@@ -43,11 +58,12 @@ public class RedisMemoryStore implements MemoryStore {
private static final String OK = "OK";
private final JedisPooled client;
private Integer vectorSize = DefaultVectorSize;
private VectorAlgorithm vectorIndexAlgorithm = DefaultIndexAlgorithm;
private String vectorDistanceMetric = DefaultDistanceMetric;
private String vectorType = DefaultVectorType;
private Integer queryDialect;
private final String vectorDistanceMetric;
@SuppressFBWarnings("SS_SHOULD_BE_STATIC")
private final String vectorType = DefaultVectorType;
private final Integer queryDialect;
public static void isXinRange(Integer x, Integer lower, Integer upper, String message) {
if (lower > x || x > upper) {
@@ -61,15 +77,14 @@ public class RedisMemoryStore implements MemoryStore {
* @param client A Redis Database client connection.
* @param vectorSize Embedding vector size, defaults to 1536 - framework model dependent -
* ada-002.
* @param vectorIndexAlgorithm Indexing algorithm for vectors, defaults to "HNSW"
* @param vectorDistanceMetric Metric for measuring vector distances, defaults to "COSINE"
* @param queryDialect Query dialect, must be 2 or greater for vector similarity searching,
* defaults to 2
*/
@SuppressFBWarnings("EI_EXPOSE_REP2")
public RedisMemoryStore(
JedisPooled client,
Integer vectorSize,
VectorAlgorithm vectorIndexAlgorithm,
String vectorDistanceMetric,
Integer queryDialect) {
@@ -85,9 +100,6 @@ public class RedisMemoryStore implements MemoryStore {
"Invalid query dialect: {x}. Query dialect must be in the range 2-4.");
this.client = client;
this.vectorSize = vectorSize;
this.vectorIndexAlgorithm = vectorIndexAlgorithm;
this.vectorDistanceMetric = vectorDistanceMetric;
this.queryDialect = queryDialect;
}
@@ -121,9 +133,6 @@ public class RedisMemoryStore implements MemoryStore {
"Invalid query dialect: {x}. Query dialect must be in the range 2-4.");
this.client = new JedisPooled(connectionString);
this.vectorSize = vectorSize;
this.vectorIndexAlgorithm = vectorIndexAlgorithm;
this.vectorDistanceMetric = vectorDistanceMetric;
this.queryDialect = queryDialect;
}
@@ -136,6 +145,13 @@ public class RedisMemoryStore implements MemoryStore {
* @return A Redis key that identifies a particular Hash of Json object.
*/
private static String getRedisKey(String collectionName, String key) {
if (key == null) {
throw new SKException("cannot create a key with a null value");
}
if (collectionName == null) {
throw new SKException("cannot form a redis key with a null collection name");
}
String name = collectionName.toLowerCase(Locale.ROOT);
String id = JsonMemoryRecord.encodeId(key);
@@ -171,10 +187,16 @@ public class RedisMemoryStore implements MemoryStore {
@Override
public Mono<List<String>> getCollectionsAsync() {
return Mono.just(
getIndexesAsync().block().stream()
.map(name -> name.substring(0, name.length() - "-sk-idx".length()))
.collect(Collectors.toList()));
return getIndexesAsync()
.map(
names ->
names.stream()
.map(
name ->
name.substring(
0,
name.length() - "-sk-idx".length()))
.collect(Collectors.toList()));
}
/**
@@ -304,12 +326,11 @@ public class RedisMemoryStore implements MemoryStore {
Objects.requireNonNull(collectionName);
// ACS issues one query per key; redis has several possible calls, fastest will be hkeys
// followed by hvals
Collection<MemoryRecord> records =
keys.stream()
.map(key -> getAsync(collectionName, key, withEmbeddings).block())
.collect(Collectors.toList());
return Mono.just(records);
return Flux.fromIterable(keys)
.flatMap(key -> getAsync(collectionName, key, withEmbeddings))
.collectList()
.map(records -> records);
}
@Override
@@ -6,7 +6,7 @@
<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>samples</artifactId>
<version>0.2.10-alpha-SNAPSHOT</version>
<version>0.2.14-alpha-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
@@ -18,7 +18,7 @@ public class SKException extends RuntimeException {
*
* @param message A string that describes the error.
*/
protected SKException(@Nullable String message) {
public SKException(@Nullable String message) {
super(message);
}
+7
View File
@@ -171,6 +171,13 @@
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>4.8.3</version>
</dependency>
</dependencies>
</dependencyManagement>