Skip to content

Commit 1046ad6

Browse files
committed
formatting fixes and small tweaks
1 parent 5eb9fc0 commit 1046ad6

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

articles/search/includes/quickstarts/search-get-started-rag-java.md

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -320,31 +320,38 @@ Create a query script that uses the Azure AI Search index and the chat model to
320320
.buildClient();
321321
}
322322

323-
private static List<SearchResult> searchDocuments(SearchClient searchClient, String query) {
323+
private static List<SearchResult> searchDocuments(
324+
SearchClient searchClient, String query) {
324325
var searchOptions = new SearchOptions()
325326
.setTop(5)
326327
.setQueryType(com.azure.search.documents.models.QueryType.SEMANTIC)
327328
.setSemanticSearchOptions(new com.azure.search.documents.models.SemanticSearchOptions()
328-
.setSemanticConfigurationName("semantic-config"));
329+
.setSemanticConfigurationName("semantic-config"))
330+
.setSelect("HotelName", "Description", "Tags");
329331

330332
return searchClient.search(query, searchOptions, null)
331333
.stream()
332334
.limit(5)
333335
.toList();
334336
}
335337

336-
private static String queryOpenAI(OpenAIClient openAIClient, String userQuery, List<SearchResult> sources) {
338+
private static String queryOpenAI(OpenAIClient openAIClient, List<SearchResult> sources) {
337339
String deploymentModel = System.getenv("AZURE_DEPLOYMENT_MODEL");
338-
340+
339341
String sourcesText = sources.stream()
340342
.map(source -> source.getDocument(Object.class).toString())
341343
.collect(java.util.stream.Collectors.joining("\n"));
342-
344+
343345
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))
346353
);
347-
354+
348355
var chatOptions = new ChatCompletionsOptions(messages);
349356
ChatCompletions response = openAIClient.getChatCompletions(deploymentModel, chatOptions);
350357

@@ -355,8 +362,7 @@ Create a query script that uses the Azure AI Search index and the chat model to
355362
SearchClient searchClient = getSearchClient();
356363
OpenAIClient openAIClient = getOpenAIClient();
357364

358-
String userQuery = "complimentary breakfast";
359-
List<SearchResult> sources = searchDocuments(searchClient, userQuery);
365+
List<SearchResult> sources = searchDocuments(searchClient);
360366
String response = queryOpenAI(openAIClient, userQuery, sources);
361367

362368
System.out.println(response);
@@ -464,33 +470,43 @@ Tell me their description, address, tags, and the rate for one room that sleeps
464470
.buildClient();
465471
}
466472

467-
private static List<SearchResult> searchDocuments(SearchClient searchClient, String query) {
473+
private static List<SearchResult> searchDocuments(
474+
SearchClient searchClient, String query) {
468475
var searchOptions = new SearchOptions()
469476
.setTop(5)
470477
.setQueryType(com.azure.search.documents.models.QueryType.SEMANTIC)
471478
.setSemanticSearchOptions(new com.azure.search.documents.models.SemanticSearchOptions()
472-
.setSemanticConfigurationName("semantic-config"));
479+
.setSemanticConfigurationName("semantic-config"))
480+
.setSelect("HotelName", "Description", "Address", "Rooms", "Tags");
473481

474482
return searchClient.search(query, searchOptions, null)
475483
.stream()
476484
.limit(5)
477485
.toList();
478486
}
479487

480-
private static String queryOpenAI(OpenAIClient openAIClient, String userQuery, List<SearchResult> sources) {
488+
private static String queryOpenAI(OpenAIClient openAIClient, List<SearchResult> sources) {
481489
String deploymentModel = System.getenv("AZURE_DEPLOYMENT_MODEL");
482-
490+
483491
String sourcesText = sources.stream()
484492
.map(source -> source.getDocument(Object.class).toString())
485493
.collect(java.util.stream.Collectors.joining("\n"));
486-
494+
487495
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))
490505
);
491-
506+
492507
var chatOptions = new ChatCompletionsOptions(messages);
493-
ChatCompletions response = openAIClient.getChatCompletions(deploymentModel, chatOptions);
508+
ChatCompletions response = openAIClient.getChatCompletions(
509+
deploymentModel, chatOptions);
494510

495511
return response.getChoices().get(0).getMessage().getContent();
496512
}
@@ -499,8 +515,7 @@ Tell me their description, address, tags, and the rate for one room that sleeps
499515
SearchClient searchClient = getSearchClient();
500516
OpenAIClient openAIClient = getOpenAIClient();
501517

502-
String userQuery = "complimentary breakfast";
503-
List<SearchResult> sources = searchDocuments(searchClient, userQuery);
518+
List<SearchResult> sources = searchDocuments(searchClient);
504519
String response = queryOpenAI(openAIClient, userQuery, sources);
505520

506521
System.out.println(response);
@@ -570,7 +585,8 @@ To debug Azure SDK errors, you can enable logging by adding the following depend
570585
Then set the following system properties when running your application:
571586

572587
```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
574590
```
575591

576592
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

Comments
 (0)