Skip to content

Commit 6d8f8a7

Browse files
Milder Hernandezjohnoliver
andauthored
Remove type from Vector Stores (#97)
* Rename memory to data Add RedisVectorStoreRecordCollection builder() method * add redis example * Fix name * Updates * Updates * Fix null away warnings * Fix null away warnings * Remove type from VectorStores * Remove unnecessary json conversion --------- Co-authored-by: John Oliver <[email protected]>
1 parent 69d852e commit 6d8f8a7

File tree

11 files changed

+148
-137
lines changed

11 files changed

+148
-137
lines changed

api-test/integration-tests/src/test/java/com/microsoft/semantickernel/tests/connectors/memory/redis/RedisVectorStoreTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public static void setUp() {
3131

3232
@Test
3333
public void getCollectionNamesAsync() {
34-
RedisVectorStore<Hotel> vectorStore = new RedisVectorStore<>(jedis, new RedisVectorStoreOptions<>(Hotel.class, null));
34+
RedisVectorStore vectorStore = new RedisVectorStore(jedis, new RedisVectorStoreOptions());
3535
List<String> collectionNames = Arrays.asList("collection1", "collection2", "collection3");
3636

3737
for (String collectionName : collectionNames) {
38-
vectorStore.getCollection(collectionName, VectorStoreRecordDefinition.fromRecordClass(Hotel.class)).createCollectionAsync().block();
38+
vectorStore.getCollection(collectionName, Hotel.class, null).createCollectionAsync().block();
3939
}
4040

4141
List<String> retrievedCollectionNames = vectorStore.getCollectionNamesAsync().block();

samples/semantickernel-concepts/semantickernel-syntax-examples/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/memory/AzureAISearch_DataStorage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ public static void dataStorageWithAzureAISearch(
114114
OpenAITextEmbeddingGenerationService embeddingGeneration) {
115115

116116
// Create a new Azure AI Search vector store
117-
var azureAISearchVectorStore = new AzureAISearchVectorStore<>(searchClient,
118-
AzureAISearchVectorStoreOptions.<GitHubFile>builder()
119-
.withRecordClass(GitHubFile.class)
120-
.build());
117+
var azureAISearchVectorStore = AzureAISearchVectorStore.builder()
118+
.withClient(searchClient)
119+
.withOptions(new AzureAISearchVectorStoreOptions())
120+
.build();
121121

122122
String collectionName = "skgithubfiles";
123-
var collection = azureAISearchVectorStore.getCollection(collectionName, null);
123+
var collection = azureAISearchVectorStore.getCollection(collectionName, GitHubFile.class, null);
124124

125125
// Create collection if it does not exist and store data
126126
collection

samples/semantickernel-concepts/semantickernel-syntax-examples/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/memory/Redis_DataStorage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ public static void dataStorageWithRedis(
114114

115115
JedisPooled jedis = new JedisPooled(REDIS_URL);
116116

117-
RedisVectorStore<GitHubFile> vectorStore = RedisVectorStore.<GitHubFile>builder()
117+
RedisVectorStore vectorStore = RedisVectorStore.builder()
118118
.withClient(jedis)
119-
.withOptions(new RedisVectorStoreOptions<>(GitHubFile.class, null))
119+
.withOptions(new RedisVectorStoreOptions())
120120
.build();
121121

122122
String collectionName = "skgithubfiles";
123-
var collection = vectorStore.getCollection(collectionName, null);
123+
var collection = vectorStore.getCollection(collectionName, GitHubFile.class, null);
124124

125125
// Create collection if it does not exist and store data
126126
List<String> ids = collection

semantickernel-experimental/src/main/java/com/microsoft/semantickernel/connectors/data/azureaisearch/AzureAISearchVectorStore.java

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
import reactor.core.publisher.Mono;
1010

1111
import javax.annotation.Nonnull;
12+
import javax.annotation.Nullable;
1213
import java.util.List;
1314

14-
public class AzureAISearchVectorStore<Record>
15-
implements VectorStore<String, Record, AzureAISearchVectorStoreRecordCollection<Record>> {
15+
public class AzureAISearchVectorStore
16+
implements VectorStore<AzureAISearchVectorStoreRecordCollection<?>> {
1617

1718
private final SearchIndexAsyncClient client;
18-
private final AzureAISearchVectorStoreOptions<Record> options;
19+
private final AzureAISearchVectorStoreOptions options;
1920

2021
/**
2122
* Creates a new instance of {@link AzureAISearchVectorStore}.
@@ -25,7 +26,7 @@ public class AzureAISearchVectorStore<Record>
2526
*/
2627
@SuppressFBWarnings("EI_EXPOSE_REP2")
2728
public AzureAISearchVectorStore(@Nonnull SearchIndexAsyncClient client,
28-
@Nonnull AzureAISearchVectorStoreOptions<Record> options) {
29+
@Nonnull AzureAISearchVectorStoreOptions options) {
2930
this.client = client;
3031
this.options = options;
3132
}
@@ -34,12 +35,14 @@ public AzureAISearchVectorStore(@Nonnull SearchIndexAsyncClient client,
3435
* Gets a new instance of {@link AzureAISearchVectorStoreRecordCollection}
3536
*
3637
* @param collectionName The name of the collection.
38+
* @param recordClass The class type of the record.
3739
* @param recordDefinition The record definition.
3840
* @return The collection.
3941
*/
4042
@Override
41-
public AzureAISearchVectorStoreRecordCollection<Record> getCollection(
43+
public <Key, Record> AzureAISearchVectorStoreRecordCollection<Record> getCollection(
4244
@Nonnull String collectionName,
45+
@Nonnull Class<Record> recordClass,
4346
VectorStoreRecordDefinition recordDefinition) {
4447

4548
if (options.getVectorStoreRecordCollectionFactory() != null) {
@@ -48,14 +51,14 @@ public AzureAISearchVectorStoreRecordCollection<Record> getCollection(
4851
client,
4952
collectionName,
5053
AzureAISearchVectorStoreRecordCollectionOptions.<Record>builder()
51-
.withRecordClass(options.getRecordClass())
54+
.withRecordClass(recordClass)
5255
.withRecordDefinition(recordDefinition)
5356
.build());
5457
}
5558

5659
return new AzureAISearchVectorStoreRecordCollection<>(client, collectionName,
5760
AzureAISearchVectorStoreRecordCollectionOptions.<Record>builder()
58-
.withRecordClass(options.getRecordClass())
61+
.withRecordClass(recordClass)
5962
.withRecordDefinition(recordDefinition)
6063
.build());
6164
}
@@ -69,4 +72,62 @@ public AzureAISearchVectorStoreRecordCollection<Record> getCollection(
6972
public Mono<List<String>> getCollectionNamesAsync() {
7073
return client.listIndexes().map(SearchIndex::getName).collectList();
7174
}
75+
76+
/**
77+
* Creates a new {@link Builder} instance.
78+
*
79+
* @return The new builder instance.
80+
*/
81+
public static Builder builder() {
82+
return new Builder();
83+
}
84+
85+
/**
86+
* Builder for {@link AzureAISearchVectorStore}.
87+
*/
88+
public static class Builder {
89+
@Nullable
90+
private SearchIndexAsyncClient client;
91+
@Nullable
92+
private AzureAISearchVectorStoreOptions options;
93+
94+
/**
95+
* Sets the Azure AI Search client.
96+
*
97+
* @param client The Azure AI Search client.
98+
* @return The updated builder instance.
99+
*/
100+
@SuppressFBWarnings("EI_EXPOSE_REP2")
101+
public Builder withClient(@Nonnull SearchIndexAsyncClient client) {
102+
this.client = client;
103+
return this;
104+
}
105+
106+
/**
107+
* Sets the options for the Azure AI Search vector store.
108+
*
109+
* @param options The options for the Azure AI Search vector store.
110+
* @return The updated builder instance.
111+
*/
112+
public Builder withOptions(@Nonnull AzureAISearchVectorStoreOptions options) {
113+
this.options = options;
114+
return this;
115+
}
116+
117+
/**
118+
* Builds the Azure AI Search vector store.
119+
*
120+
* @return The Azure AI Search vector store.
121+
*/
122+
public AzureAISearchVectorStore build() {
123+
if (client == null) {
124+
throw new IllegalStateException("client is required");
125+
}
126+
if (options == null) {
127+
throw new IllegalStateException("options is required");
128+
}
129+
130+
return new AzureAISearchVectorStore(client, options);
131+
}
132+
}
72133
}

semantickernel-experimental/src/main/java/com/microsoft/semantickernel/connectors/data/azureaisearch/AzureAISearchVectorStoreOptions.java

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,34 @@
44
import javax.annotation.Nonnull;
55
import javax.annotation.Nullable;
66

7-
public class AzureAISearchVectorStoreOptions<Record> {
8-
private final Class<Record> recordClass;
7+
public class AzureAISearchVectorStoreOptions {
98
@Nullable
10-
private final AzureAISearchVectorStoreRecordCollectionFactory<Record> vectorStoreRecordCollectionFactory;
9+
private final AzureAISearchVectorStoreRecordCollectionFactory vectorStoreRecordCollectionFactory;
1110

1211
/**
1312
* Creates a new instance of the Azure AI Search vector store options.
1413
*
15-
* @param recordClass The record class.
1614
* @param vectorStoreRecordCollectionFactory The vector store record collection factory.
1715
*/
18-
public AzureAISearchVectorStoreOptions(@Nonnull Class<Record> recordClass,
19-
@Nullable AzureAISearchVectorStoreRecordCollectionFactory<Record> vectorStoreRecordCollectionFactory) {
20-
this.recordClass = recordClass;
16+
public AzureAISearchVectorStoreOptions(
17+
@Nullable AzureAISearchVectorStoreRecordCollectionFactory vectorStoreRecordCollectionFactory) {
2118
this.vectorStoreRecordCollectionFactory = vectorStoreRecordCollectionFactory;
2219
}
2320

2421
/**
25-
* Creates a new builder.
26-
*
27-
* @param <Record> the record type
28-
* @return the builder
22+
* Creates a new instance of the Azure AI Search vector store options.
2923
*/
30-
public static <Record> Builder<Record> builder() {
31-
return new Builder<>();
24+
public AzureAISearchVectorStoreOptions() {
25+
this(null);
3226
}
3327

3428
/**
35-
* Gets the record class.
29+
* Creates a new builder.
3630
*
37-
* @return the record class
31+
* @return the builder
3832
*/
39-
public Class<Record> getRecordClass() {
40-
return recordClass;
33+
public static Builder builder() {
34+
return new Builder();
4135
}
4236

4337
/**
@@ -46,40 +40,26 @@ public Class<Record> getRecordClass() {
4640
* @return the vector store record collection factory
4741
*/
4842
@Nullable
49-
public AzureAISearchVectorStoreRecordCollectionFactory<Record> getVectorStoreRecordCollectionFactory() {
43+
public AzureAISearchVectorStoreRecordCollectionFactory getVectorStoreRecordCollectionFactory() {
5044
return vectorStoreRecordCollectionFactory;
5145
}
5246

5347
/**
5448
* Builder for Azure AI Search vector store options.
5549
*
56-
* @param <Record> the record type
5750
*/
58-
public static class Builder<Record> {
51+
public static class Builder {
5952
@Nullable
60-
private Class<Record> recordClass;
61-
@Nullable
62-
private AzureAISearchVectorStoreRecordCollectionFactory<Record> vectorStoreRecordCollectionFactory;
63-
64-
/**
65-
* Sets the record class.
66-
*
67-
* @param recordClass The record class.
68-
* @return The updated builder instance.
69-
*/
70-
public Builder<Record> withRecordClass(Class<Record> recordClass) {
71-
this.recordClass = recordClass;
72-
return this;
73-
}
53+
private AzureAISearchVectorStoreRecordCollectionFactory vectorStoreRecordCollectionFactory;
7454

7555
/**
7656
* Sets the vector store record collection factory.
7757
*
7858
* @param vectorStoreRecordCollectionFactory The vector store record collection factory.
7959
* @return The updated builder instance.
8060
*/
81-
public Builder<Record> withVectorStoreRecordCollectionFactory(
82-
AzureAISearchVectorStoreRecordCollectionFactory<Record> vectorStoreRecordCollectionFactory) {
61+
public Builder withVectorStoreRecordCollectionFactory(
62+
AzureAISearchVectorStoreRecordCollectionFactory vectorStoreRecordCollectionFactory) {
8363
this.vectorStoreRecordCollectionFactory = vectorStoreRecordCollectionFactory;
8464
return this;
8565
}
@@ -89,13 +69,8 @@ public Builder<Record> withVectorStoreRecordCollectionFactory(
8969
*
9070
* @return The Azure AI Search vector store options.
9171
*/
92-
public AzureAISearchVectorStoreOptions<Record> build() {
93-
if (recordClass == null) {
94-
throw new IllegalArgumentException("recordClass is required");
95-
}
96-
97-
return new AzureAISearchVectorStoreOptions<>(recordClass,
98-
vectorStoreRecordCollectionFactory);
72+
public AzureAISearchVectorStoreOptions build() {
73+
return new AzureAISearchVectorStoreOptions(vectorStoreRecordCollectionFactory);
9974
}
10075
}
10176
}

semantickernel-experimental/src/main/java/com/microsoft/semantickernel/connectors/data/azureaisearch/AzureAISearchVectorStoreRecordCollectionFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
/**
77
* Factory for creating Azure AI Search vector store record collections.
88
*
9-
* @param <Record> the record type
109
*/
11-
public interface AzureAISearchVectorStoreRecordCollectionFactory<Record> {
10+
public interface AzureAISearchVectorStoreRecordCollectionFactory {
1211

1312
/**
1413
* Creates a new Azure AI Search vector store record collection.
@@ -18,7 +17,7 @@ public interface AzureAISearchVectorStoreRecordCollectionFactory<Record> {
1817
* @param options The options for the collection.
1918
* @return The new Azure AI Search vector store record collection.
2019
*/
21-
AzureAISearchVectorStoreRecordCollection<Record> createVectorStoreRecordCollection(
20+
<Record> AzureAISearchVectorStoreRecordCollection<Record> createVectorStoreRecordCollection(
2221
SearchIndexAsyncClient client,
2322
String collectionName,
2423
AzureAISearchVectorStoreRecordCollectionOptions<Record> options);

0 commit comments

Comments
 (0)