|
15 | 15 | import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStore; |
16 | 16 | import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStoreOptions; |
17 | 17 | import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStoreRecordCollectionOptions; |
| 18 | +import com.microsoft.semantickernel.data.textsearch.TextSearchResultValue; |
18 | 19 | import com.microsoft.semantickernel.data.vectorsearch.VectorSearchResult; |
| 20 | +import com.microsoft.semantickernel.data.vectorsearch.VectorizedSearch; |
19 | 21 | import com.microsoft.semantickernel.data.vectorstorage.VectorStoreRecordCollection; |
| 22 | +import com.microsoft.semantickernel.data.vectorstorage.VectorStoreTextSearch; |
20 | 23 | import com.microsoft.semantickernel.data.vectorstorage.annotations.VectorStoreRecordData; |
21 | 24 | import com.microsoft.semantickernel.data.vectorstorage.annotations.VectorStoreRecordKey; |
22 | 25 | import com.microsoft.semantickernel.data.vectorstorage.annotations.VectorStoreRecordVector; |
@@ -51,13 +54,12 @@ public class VectorStoreWithAzureAISearch { |
51 | 54 | private static final int EMBEDDING_DIMENSIONS = 1536; |
52 | 55 |
|
53 | 56 | static class GitHubFile { |
54 | | - |
55 | | - @JsonProperty("fileId") // Set a different name for the storage field if needed |
56 | 57 | @VectorStoreRecordKey() |
57 | 58 | private final String id; |
58 | 59 | @VectorStoreRecordData() |
59 | 60 | private final String description; |
60 | 61 | @VectorStoreRecordData |
| 62 | + @TextSearchResultValue |
61 | 63 | private final String link; |
62 | 64 | @VectorStoreRecordVector(dimensions = EMBEDDING_DIMENSIONS, indexKind = IndexKind.HNSW, distanceFunction = DistanceFunction.COSINE_SIMILARITY) |
63 | 65 | private final List<Float> embedding; |
@@ -111,7 +113,6 @@ public static void main(String[] args) { |
111 | 113 | var searchClient = new SearchIndexClientBuilder() |
112 | 114 | .endpoint(AZURE_AI_SEARCH_ENDPOINT) |
113 | 115 | .credential(new AzureKeyCredential(AZURE_AISEARCH_KEY)) |
114 | | - .clientOptions(clientOptions()) |
115 | 116 | .buildAsyncClient(); |
116 | 117 |
|
117 | 118 | storeAndSearch(searchClient, embeddingGeneration); |
@@ -141,27 +142,24 @@ public static void storeAndSearch( |
141 | 142 | .then(storeData(collection, embeddingGeneration, sampleData())) |
142 | 143 | .block(); |
143 | 144 |
|
| 145 | + // Build a vectorized search |
| 146 | + var vectorStoreTextSearch = VectorStoreTextSearch.<GitHubFile>builder() |
| 147 | + .withVectorizedSearch(collection) |
| 148 | + .withTextEmbeddingGenerationService(embeddingGeneration) |
| 149 | + .build(); |
| 150 | + |
144 | 151 | // Search for results |
145 | 152 | // Might need to wait for the data to be indexed |
146 | | - var results = search("How to get started", collection, embeddingGeneration).block(); |
| 153 | + String query = "How to get started?"; |
| 154 | + var results = vectorStoreTextSearch.searchAsync(query, null) |
| 155 | + .block(); |
147 | 156 |
|
148 | | - if (results == null || results.isEmpty()) { |
| 157 | + if (results == null || results.getTotalCount() == 0) { |
149 | 158 | System.out.println("No search results found."); |
150 | 159 | return; |
151 | 160 | } |
152 | | - var searchResult = results.get(0); |
153 | | - System.out.printf("Search result with score: %f.%n Link: %s, Description: %s%n", |
154 | | - searchResult.getScore(), searchResult.getRecord().link, |
155 | | - searchResult.getRecord().description); |
156 | | - } |
157 | 161 |
|
158 | | - private static Mono<List<VectorSearchResult<GitHubFile>>> search( |
159 | | - String searchText, |
160 | | - VectorStoreRecordCollection<String, GitHubFile> recordCollection, |
161 | | - OpenAITextEmbeddingGenerationService embeddingGeneration) { |
162 | | - // Generate embeddings for the search text and search for the closest records |
163 | | - return embeddingGeneration.generateEmbeddingAsync(searchText) |
164 | | - .flatMap(r -> recordCollection.searchAsync(r.getVector(), null)); |
| 162 | + System.out.printf("Best result for '%s': %s%n", query, results.getResults().get(0)); |
165 | 163 | } |
166 | 164 |
|
167 | 165 | private static Mono<List<String>> storeData( |
@@ -204,11 +202,4 @@ private static Map<String, String> sampleData() { |
204 | 202 | "README: README associated with a sample chat summary react-based webapp" }, |
205 | 203 | }).collect(Collectors.toMap(element -> element[0], element -> element[1])); |
206 | 204 | } |
207 | | - |
208 | | - private static ClientOptions clientOptions() { |
209 | | - return new ClientOptions() |
210 | | - .setTracingOptions(new TracingOptions()) |
211 | | - .setMetricsOptions(new MetricsOptions()) |
212 | | - .setApplicationId("Semantic-Kernel"); |
213 | | - } |
214 | 205 | } |
0 commit comments