|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +package com.microsoft.semantickernel.connectors.memory.azureaisearch; |
| 3 | + |
| 4 | +import com.azure.search.documents.SearchAsyncClient; |
| 5 | +import com.azure.search.documents.SearchDocument; |
| 6 | +import com.azure.search.documents.indexes.SearchIndexAsyncClient; |
| 7 | +import com.azure.search.documents.models.IndexDocumentsResult; |
| 8 | +import com.azure.search.documents.models.IndexingResult; |
| 9 | +import com.microsoft.semantickernel.exceptions.SKException; |
| 10 | +import com.microsoft.semantickernel.memory.VectorRecordStore; |
| 11 | +import com.microsoft.semantickernel.memory.VectorStoreRecordMapper; |
| 12 | +import com.microsoft.semantickernel.memory.recorddefinition.VectorStoreRecordDataField; |
| 13 | +import com.microsoft.semantickernel.memory.recorddefinition.VectorStoreRecordDefinition; |
| 14 | +import com.microsoft.semantickernel.memory.recordoptions.DeleteRecordOptions; |
| 15 | +import com.microsoft.semantickernel.memory.recordoptions.GetRecordOptions; |
| 16 | +import com.microsoft.semantickernel.memory.recordoptions.UpsertRecordOptions; |
| 17 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 18 | +import reactor.core.publisher.Flux; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | + |
| 21 | +import javax.annotation.Nonnull; |
| 22 | +import javax.annotation.Nullable; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.Iterator; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | +import java.util.stream.Collectors; |
| 31 | + |
| 32 | +public class AzureAISearchVectorRecordStore<Record> implements VectorRecordStore<String, Record> { |
| 33 | + private final SearchIndexAsyncClient client; |
| 34 | + private final Map<String, SearchAsyncClient> clientsByIndex = new ConcurrentHashMap<>(); |
| 35 | + private final AzureAISearchVectorStoreOptions<Record> options; |
| 36 | + private final List<String> nonVectorFields = new ArrayList<>(); |
| 37 | + |
| 38 | + @SuppressFBWarnings("EI_EXPOSE_REP2") |
| 39 | + public AzureAISearchVectorRecordStore( |
| 40 | + @Nonnull SearchIndexAsyncClient client, |
| 41 | + @Nonnull AzureAISearchVectorStoreOptions<Record> options) { |
| 42 | + this.client = client; |
| 43 | + |
| 44 | + // If record definition is not provided, create one from the record class |
| 45 | + if (options.getRecordDefinition() == null) { |
| 46 | + this.options = AzureAISearchVectorStoreOptions.<Record>builder() |
| 47 | + .withRecordClass(options.getRecordClass()) |
| 48 | + .withVectorStoreRecordMapper(options.getVectorStoreRecordMapper()) |
| 49 | + .withRecordDefinition(VectorStoreRecordDefinition.create(options.getRecordClass())) |
| 50 | + .build(); |
| 51 | + } else { |
| 52 | + this.options = options; |
| 53 | + } |
| 54 | + |
| 55 | + // Add non-vector fields to the list |
| 56 | + nonVectorFields.add(this.options.getRecordDefinition().getKeyField().getName()); |
| 57 | + nonVectorFields.addAll(this.options.getRecordDefinition().getDataFields().stream() |
| 58 | + .map(VectorStoreRecordDataField::getName) |
| 59 | + .collect(Collectors.toList())); |
| 60 | + } |
| 61 | + |
| 62 | + private String resolveCollectionName(@Nullable String collectionName) { |
| 63 | + if (collectionName != null) { |
| 64 | + return collectionName; |
| 65 | + } |
| 66 | + if (options.getDefaultCollectionName() != null) { |
| 67 | + return options.getDefaultCollectionName(); |
| 68 | + } |
| 69 | + throw new SKException("A collection name is required"); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Mono<Record> getAsync( |
| 74 | + @Nonnull String key, GetRecordOptions options) { |
| 75 | + String indexName = resolveCollectionName( |
| 76 | + options != null ? options.getCollectionName() : null); |
| 77 | + SearchAsyncClient client = this.getSearchClient(indexName); |
| 78 | + |
| 79 | + // If vectors are not requested, only fetch non-vector fields |
| 80 | + List<String> selectedFields = null; |
| 81 | + if (options != null && !options.includeVectors()) { |
| 82 | + selectedFields = Collections.unmodifiableList(nonVectorFields); |
| 83 | + } |
| 84 | + |
| 85 | + VectorStoreRecordMapper<Record, SearchDocument> mapper = this.options |
| 86 | + .getVectorStoreRecordMapper(); |
| 87 | + |
| 88 | + // Use custom mapper if available |
| 89 | + if (mapper != null && mapper.getToRecordMapper() != null) { |
| 90 | + return client.getDocument(key, SearchDocument.class) |
| 91 | + .map(this.options.getVectorStoreRecordMapper()::toRecord); |
| 92 | + } |
| 93 | + |
| 94 | + return client.getDocumentWithResponse(key, this.options.getRecordClass(), selectedFields) |
| 95 | + .map(response -> { |
| 96 | + if (response.getStatusCode() == 404) { |
| 97 | + throw new SKException("Record not found: " + key); |
| 98 | + } |
| 99 | + return response.getValue(); |
| 100 | + }); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Mono<Collection<Record>> getBatchAsync( |
| 106 | + @Nonnull Collection<String> keys, |
| 107 | + GetRecordOptions options) { |
| 108 | + return Flux.fromIterable(keys) |
| 109 | + .flatMap(key -> getAsync(key, options).flux()) |
| 110 | + .collect(Collectors.toList()); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Mono<String> upsertAsync(@Nonnull Record record, UpsertRecordOptions options) { |
| 115 | + return upsertBatchAsync(Collections.singletonList(record), options) |
| 116 | + .map(Collection::iterator) |
| 117 | + .map(Iterator::next); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Mono<Collection<String>> upsertBatchAsync( |
| 122 | + @Nonnull Collection<Record> records, UpsertRecordOptions options) { |
| 123 | + if (records.isEmpty()) { |
| 124 | + return Mono.just(Collections.emptyList()); |
| 125 | + } |
| 126 | + |
| 127 | + String indexName = resolveCollectionName( |
| 128 | + options != null ? options.getCollectionName() : null); |
| 129 | + SearchAsyncClient client = this.getSearchClient(indexName); |
| 130 | + |
| 131 | + VectorStoreRecordMapper<Record, SearchDocument> mapper = this.options |
| 132 | + .getVectorStoreRecordMapper(); |
| 133 | + Iterable<?> documents; |
| 134 | + |
| 135 | + // Use custom mapper if available |
| 136 | + if (mapper != null && mapper.getToStorageModelMapper() != null) { |
| 137 | + documents = records.stream() |
| 138 | + .map(this.options.getVectorStoreRecordMapper()::toStorageModel) |
| 139 | + .collect(Collectors.toList()); |
| 140 | + } else { |
| 141 | + documents = records; |
| 142 | + } |
| 143 | + |
| 144 | + return client.uploadDocuments(documents) |
| 145 | + .map(IndexDocumentsResult::getResults) |
| 146 | + .map( |
| 147 | + results -> results.stream() |
| 148 | + .map(IndexingResult::getKey) |
| 149 | + .collect(Collectors.toList())); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public Mono<Void> deleteAsync(String key, DeleteRecordOptions options) { |
| 154 | + return deleteBatchAsync(Collections.singletonList(key), options); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public Mono<Void> deleteBatchAsync(Collection<String> keys, DeleteRecordOptions options) { |
| 159 | + String indexName = resolveCollectionName( |
| 160 | + options != null ? options.getCollectionName() : null); |
| 161 | + SearchAsyncClient client = this.getSearchClient(indexName); |
| 162 | + |
| 163 | + return client.deleteDocuments(keys.stream().map(key -> { |
| 164 | + SearchDocument document = new SearchDocument(); |
| 165 | + document.put(this.options.getRecordDefinition().getKeyField().getName(), key); |
| 166 | + return document; |
| 167 | + }).collect(Collectors.toList())).then(); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Get a search client for the index specified. Note: the index might not exist, but we avoid |
| 172 | + * checking everytime and the extra latency. |
| 173 | + * |
| 174 | + * @param indexName Index name |
| 175 | + * @return Search client ready to read/write |
| 176 | + */ |
| 177 | + protected SearchAsyncClient getSearchClient(@Nonnull String indexName) { |
| 178 | + return clientsByIndex.computeIfAbsent( |
| 179 | + indexName, client::getSearchAsyncClient); |
| 180 | + } |
| 181 | +} |
0 commit comments