@@ -320,31 +320,38 @@ Create a query script that uses the Azure AI Search index and the chat model to
320
320
.buildClient();
321
321
}
322
322
323
- private static List<SearchResult > searchDocuments (SearchClient searchClient , String query ) {
323
+ private static List<SearchResult > searchDocuments (
324
+ SearchClient searchClient , String query ) {
324
325
var searchOptions = new SearchOptions ()
325
326
.setTop(5 )
326
327
.setQueryType(com.azure.search.documents.models. QueryType . SEMANTIC )
327
328
.setSemanticSearchOptions(new com.azure.search.documents.models. SemanticSearchOptions ()
328
- .setSemanticConfigurationName(" semantic-config" ));
329
+ .setSemanticConfigurationName(" semantic-config" ))
330
+ .setSelect(" HotelName" , " Description" , " Tags" );
329
331
330
332
return searchClient. search(query, searchOptions, null )
331
333
.stream()
332
334
.limit(5 )
333
335
.toList();
334
336
}
335
337
336
- private static String queryOpenAI (OpenAIClient openAIClient , String userQuery , List<SearchResult > sources ) {
338
+ private static String queryOpenAI (OpenAIClient openAIClient , List<SearchResult > sources ) {
337
339
String deploymentModel = System . getenv(" AZURE_DEPLOYMENT_MODEL" );
338
-
340
+
339
341
String sourcesText = sources. stream()
340
342
.map(source - > source. getDocument(Object . class). toString())
341
343
.collect(java.util.stream. Collectors . joining(" \n " ));
342
-
344
+
343
345
var messages = List . of(
344
- new ChatRequestSystemMessage (" You are an assistant that recommends hotels based on search results." ),
345
- new ChatRequestUserMessage (" Can you recommend a few hotels that offer " + userQuery + " ? Here are the search results:\n " + sourcesText)
346
+ new ChatRequestSystemMessage (" " "
347
+ You are an assistant that recommends hotels based on
348
+ search results." " " ),
349
+ new ChatRequestUserMessage (" " "
350
+ Can you recommend a few hotels that offer
351
+ complimentary breakfast? Here are the search results:
352
+ %s" " " . formatted(sourcesText))
346
353
);
347
-
354
+
348
355
var chatOptions = new ChatCompletionsOptions (messages);
349
356
ChatCompletions response = openAIClient. getChatCompletions(deploymentModel, chatOptions);
350
357
@@ -355,8 +362,7 @@ Create a query script that uses the Azure AI Search index and the chat model to
355
362
SearchClient searchClient = getSearchClient();
356
363
OpenAIClient openAIClient = getOpenAIClient();
357
364
358
- String userQuery = " complimentary breakfast" ;
359
- List<SearchResult > sources = searchDocuments(searchClient, userQuery);
365
+ List<SearchResult > sources = searchDocuments(searchClient);
360
366
String response = queryOpenAI(openAIClient, userQuery, sources);
361
367
362
368
System . out. println(response);
@@ -464,33 +470,43 @@ Tell me their description, address, tags, and the rate for one room that sleeps
464
470
.buildClient();
465
471
}
466
472
467
- private static List<SearchResult > searchDocuments (SearchClient searchClient , String query ) {
473
+ private static List<SearchResult > searchDocuments (
474
+ SearchClient searchClient , String query ) {
468
475
var searchOptions = new SearchOptions ()
469
476
.setTop(5 )
470
477
.setQueryType(com.azure.search.documents.models. QueryType . SEMANTIC )
471
478
.setSemanticSearchOptions(new com.azure.search.documents.models. SemanticSearchOptions ()
472
- .setSemanticConfigurationName(" semantic-config" ));
479
+ .setSemanticConfigurationName(" semantic-config" ))
480
+ .setSelect(" HotelName" , " Description" , " Address" , " Rooms" , " Tags" );
473
481
474
482
return searchClient. search(query, searchOptions, null )
475
483
.stream()
476
484
.limit(5 )
477
485
.toList();
478
486
}
479
487
480
- private static String queryOpenAI (OpenAIClient openAIClient , String userQuery , List<SearchResult > sources ) {
488
+ private static String queryOpenAI (OpenAIClient openAIClient , List<SearchResult > sources ) {
481
489
String deploymentModel = System . getenv(" AZURE_DEPLOYMENT_MODEL" );
482
-
490
+
483
491
String sourcesText = sources. stream()
484
492
.map(source - > source. getDocument(Object . class). toString())
485
493
.collect(java.util.stream. Collectors . joining(" \n " ));
486
-
494
+
487
495
var messages = List . of(
488
- new ChatRequestSystemMessage (" You are an assistant that recommends hotels based on search results." ),
489
- new ChatRequestUserMessage (" Can you recommend a few hotels that offer " + userQuery + " ? Tell me their description, address, tags, and the rate for one room that sleeps 4 people. Here are the search results:\n " + sourcesText)
496
+ new ChatRequestSystemMessage (" " "
497
+ You are an assistant that recommends hotels based on
498
+ search results." " " ),
499
+ new ChatRequestUserMessage (" " "
500
+ Can you recommend a few hotels that offer
501
+ complimentary breakfast? Tell me their description,
502
+ address, tags, and the rate for one room that sleeps
503
+ 4 people. Here are the search results:
504
+ %s" " " . formatted(sourcesText))
490
505
);
491
-
506
+
492
507
var chatOptions = new ChatCompletionsOptions (messages);
493
- ChatCompletions response = openAIClient. getChatCompletions(deploymentModel, chatOptions);
508
+ ChatCompletions response = openAIClient. getChatCompletions(
509
+ deploymentModel, chatOptions);
494
510
495
511
return response. getChoices(). get(0 ). getMessage(). getContent();
496
512
}
@@ -499,8 +515,7 @@ Tell me their description, address, tags, and the rate for one room that sleeps
499
515
SearchClient searchClient = getSearchClient();
500
516
OpenAIClient openAIClient = getOpenAIClient();
501
517
502
- String userQuery = " complimentary breakfast" ;
503
- List<SearchResult > sources = searchDocuments(searchClient, userQuery);
518
+ List<SearchResult > sources = searchDocuments(searchClient);
504
519
String response = queryOpenAI(openAIClient, userQuery, sources);
505
520
506
521
System . out. println(response);
@@ -570,7 +585,8 @@ To debug Azure SDK errors, you can enable logging by adding the following depend
570
585
Then set the following system properties when running your application:
571
586
572
587
``` bash
573
- mvn compile exec:java -Dexec.mainClass=" com.example.rag.Query" -Dorg.slf4j.simpleLogger.defaultLogLevel=debug
588
+ mvn compile exec:java -Dexec.mainClass=" com.example.rag.Query" \
589
+ -Dorg.slf4j.simpleLogger.defaultLogLevel=debug
574
590
```
575
591
576
592
This will enable detailed logging for the Azure SDK, which can help identify [ issues with authentication] ( https://github.com/Azure/azure-sdk-for-java/wiki/Azure-Identity-Troubleshooting ) , network connectivity, or other problems.
0 commit comments