55import com .azure .ai .openai .OpenAIClientBuilder ;
66import com .azure .core .credential .AzureKeyCredential ;
77import com .azure .core .credential .KeyCredential ;
8+ import com .azure .core .util .ClientOptions ;
9+ import com .azure .core .util .MetricsOptions ;
10+ import com .azure .core .util .TracingOptions ;
811import com .azure .search .documents .indexes .SearchIndexAsyncClient ;
912import com .azure .search .documents .indexes .SearchIndexClientBuilder ;
10- import com .fasterxml .jackson .annotation .JsonProperty ;
1113import com .microsoft .semantickernel .aiservices .openai .textembedding .OpenAITextEmbeddingGenerationService ;
1214import com .microsoft .semantickernel .data .azureaisearch .AzureAISearchVectorStore ;
1315import com .microsoft .semantickernel .data .azureaisearch .AzureAISearchVectorStoreOptions ;
1416import com .microsoft .semantickernel .data .azureaisearch .AzureAISearchVectorStoreRecordCollectionOptions ;
15- import com .microsoft .semantickernel .data .textsearch . TextSearchResultValue ;
17+ import com .microsoft .semantickernel .data .vectorsearch . VectorSearchResults ;
1618import com .microsoft .semantickernel .data .vectorstorage .VectorStoreRecordCollection ;
17- import com .microsoft .semantickernel .data .VectorStoreTextSearch ;
1819import com .microsoft .semantickernel .data .vectorstorage .annotations .VectorStoreRecordData ;
1920import com .microsoft .semantickernel .data .vectorstorage .annotations .VectorStoreRecordKey ;
2021import com .microsoft .semantickernel .data .vectorstorage .annotations .VectorStoreRecordVector ;
@@ -49,12 +50,11 @@ public class VectorStoreWithAzureAISearch {
4950 private static final int EMBEDDING_DIMENSIONS = 1536 ;
5051
5152 static class GitHubFile {
52- @ VectorStoreRecordKey ()
53+ @ VectorStoreRecordKey
5354 private final String id ;
54- @ VectorStoreRecordData ()
55+ @ VectorStoreRecordData
5556 private final String description ;
5657 @ VectorStoreRecordData
57- @ TextSearchResultValue
5858 private final String link ;
5959 @ VectorStoreRecordVector (dimensions = EMBEDDING_DIMENSIONS , indexKind = IndexKind .HNSW , distanceFunction = DistanceFunction .COSINE_SIMILARITY )
6060 private final List <Float > embedding ;
@@ -64,10 +64,10 @@ public GitHubFile() {
6464 }
6565
6666 public GitHubFile (
67- @ JsonProperty ( "fileId" ) String id ,
68- @ JsonProperty ( "description" ) String description ,
69- @ JsonProperty ( "link" ) String link ,
70- @ JsonProperty ( "embedding" ) List <Float > embedding ) {
67+ String id ,
68+ String description ,
69+ String link ,
70+ List <Float > embedding ) {
7171 this .id = id ;
7272 this .description = description ;
7373 this .link = link ;
@@ -108,6 +108,7 @@ public static void main(String[] args) {
108108 var searchClient = new SearchIndexClientBuilder ()
109109 .endpoint (AZURE_AI_SEARCH_ENDPOINT )
110110 .credential (new AzureKeyCredential (AZURE_AISEARCH_KEY ))
111+ .clientOptions (clientOptions ())
111112 .buildAsyncClient ();
112113
113114 storeAndSearch (searchClient , embeddingGeneration );
@@ -137,24 +138,27 @@ public static void storeAndSearch(
137138 .then (storeData (collection , embeddingGeneration , sampleData ()))
138139 .block ();
139140
140- // Build a vectorized search
141- var vectorStoreTextSearch = VectorStoreTextSearch .<GitHubFile >builder ()
142- .withVectorizedSearch (collection )
143- .withTextEmbeddingGenerationService (embeddingGeneration )
144- .build ();
145-
146141 // Search for results
147142 // Might need to wait for the data to be indexed
148- String query = "How to get started?" ;
149- var results = vectorStoreTextSearch .searchAsync (query , null )
150- .block ();
143+ var results = search ("How to get started" , collection , embeddingGeneration ).block ();
151144
152145 if (results == null || results .getTotalCount () == 0 ) {
153146 System .out .println ("No search results found." );
154147 return ;
155148 }
149+ var searchResult = results .getResults ().get (0 );
150+ System .out .printf ("Search result with score: %f.%n Link: %s, Description: %s%n" ,
151+ searchResult .getScore (), searchResult .getRecord ().link ,
152+ searchResult .getRecord ().description );
153+ }
156154
157- System .out .printf ("Best result for '%s': %s%n" , query , results .getResults ().get (0 ));
155+ private static Mono <VectorSearchResults <GitHubFile >> search (
156+ String searchText ,
157+ VectorStoreRecordCollection <String , GitHubFile > recordCollection ,
158+ OpenAITextEmbeddingGenerationService embeddingGeneration ) {
159+ // Generate embeddings for the search text and search for the closest records
160+ return embeddingGeneration .generateEmbeddingAsync (searchText )
161+ .flatMap (r -> recordCollection .searchAsync (r .getVector (), null ));
158162 }
159163
160164 private static Mono <List <String >> storeData (
@@ -197,4 +201,11 @@ private static Map<String, String> sampleData() {
197201 "README: README associated with a sample chat summary react-based webapp" },
198202 }).collect (Collectors .toMap (element -> element [0 ], element -> element [1 ]));
199203 }
204+
205+ private static ClientOptions clientOptions () {
206+ return new ClientOptions ()
207+ .setTracingOptions (new TracingOptions ())
208+ .setMetricsOptions (new MetricsOptions ())
209+ .setApplicationId ("Semantic-Kernel" );
210+ }
200211}
0 commit comments