@@ -20,7 +20,6 @@ ms.date: 07/31/2025
20
20
- [ Java 21 (LTS)] ( /java/openjdk/install ) .
21
21
- [ Maven] ( https://maven.apache.org/download.cgi ) .
22
22
23
-
24
23
## Configure access
25
24
26
25
Requests to the search endpoint must be authenticated and authorized. You can use API keys or roles for this task. Keys are easier to start with, but roles are more secure. This quickstart assumes roles.
@@ -335,23 +334,24 @@ Create a query script that uses the Azure AI Search index and the chat model to
335
334
.toList();
336
335
}
337
336
338
- private static String queryOpenAI (OpenAIClient openAIClient , List<SearchResult > sources ) {
337
+ private static String queryOpenAI (OpenAIClient openAIClient ,
338
+ String userQuery , List<SearchResult > sources ) {
339
339
String deploymentModel = System . getenv(" AZURE_DEPLOYMENT_MODEL" );
340
-
340
+
341
341
String sourcesText = sources. stream()
342
342
.map(source - > source. getDocument(Object . class). toString())
343
343
.collect(java.util.stream. Collectors . joining(" \n " ));
344
-
344
+
345
345
var messages = List . of(
346
346
new ChatRequestSystemMessage (" " "
347
347
You are an assistant that recommends hotels based on
348
348
search results." " " ),
349
349
new ChatRequestUserMessage (" " "
350
- Can you recommend a few hotels that offer
351
- complimentary breakfast? Here are the search results:
352
- %s" " " . formatted(sourcesText))
350
+ Can you recommend a few hotels that offer %s?
351
+ Here are the search results:
352
+ %s" " " . formatted(userQuery, sourcesText))
353
353
);
354
-
354
+
355
355
var chatOptions = new ChatCompletionsOptions (messages);
356
356
ChatCompletions response = openAIClient. getChatCompletions(deploymentModel, chatOptions);
357
357
@@ -362,10 +362,12 @@ Create a query script that uses the Azure AI Search index and the chat model to
362
362
SearchClient searchClient = getSearchClient();
363
363
OpenAIClient openAIClient = getOpenAIClient();
364
364
365
- List<SearchResult > sources = searchDocuments(searchClient);
365
+ String userQuery = " complimentary breakfast" ;
366
+ List<SearchResult > sources = searchDocuments(searchClient, userQuery);
366
367
String response = queryOpenAI(openAIClient, userQuery, sources);
367
368
368
369
System . out. println(response);
370
+ System . exit(0 );
369
371
}
370
372
}
371
373
```
@@ -485,25 +487,26 @@ Tell me their description, address, tags, and the rate for one room that sleeps
485
487
.toList();
486
488
}
487
489
488
- private static String queryOpenAI (OpenAIClient openAIClient , List<SearchResult > sources ) {
490
+ private static String queryOpenAI (OpenAIClient openAIClient ,
491
+ String userQuery , List<SearchResult > sources ) {
489
492
String deploymentModel = System . getenv(" AZURE_DEPLOYMENT_MODEL" );
490
-
493
+
491
494
String sourcesText = sources. stream()
492
495
.map(source - > source. getDocument(Object . class). toString())
493
496
.collect(java.util.stream. Collectors . joining(" \n " ));
494
-
497
+
495
498
var messages = List . of(
496
499
new ChatRequestSystemMessage (" " "
497
500
You are an assistant that recommends hotels based on
498
501
search results." " " ),
499
502
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))
503
+ Can you recommend a few hotels that offer %s?
504
+ Tell me their description, address, tags,
505
+ and the rate for one room that sleeps 4 people.
506
+ Here are the search results:
507
+ %s" " " . formatted(userQuery, sourcesText))
505
508
);
506
-
509
+
507
510
var chatOptions = new ChatCompletionsOptions (messages);
508
511
ChatCompletions response = openAIClient. getChatCompletions(
509
512
deploymentModel, chatOptions);
@@ -515,10 +518,12 @@ Tell me their description, address, tags, and the rate for one room that sleeps
515
518
SearchClient searchClient = getSearchClient();
516
519
OpenAIClient openAIClient = getOpenAIClient();
517
520
518
- List<SearchResult > sources = searchDocuments(searchClient);
521
+ String userQuery = " complimentary breakfast" ;
522
+ List<SearchResult > sources = searchDocuments(searchClient, userQuery);
519
523
String response = queryOpenAI(openAIClient, userQuery, sources);
520
524
521
525
System . out. println(response);
526
+ System . exit(0 );
522
527
}
523
528
}
524
529
```
0 commit comments