Skip to content

Commit 067ad6e

Browse files
authored
Merge pull request #7404 from KarlErickson/karler-quickstarts
replaced inline Java code with external references
2 parents ab48d89 + 3a645c8 commit 067ad6e

File tree

6 files changed

+42
-1671
lines changed

6 files changed

+42
-1671
lines changed

.openpublishing.publish.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@
200200
"branch": "main",
201201
"branch_mapping": {}
202202
},
203+
{
204+
"path_to_root": "azure-search-java-samples",
205+
"url": "https://github.com/Azure-Samples/azure-search-java-samples",
206+
"branch": "main",
207+
"branch_mapping": {}
208+
},
203209
{
204210
"path_to_root": "azure-search-dotnet-samples",
205211
"url": "https://github.com/Azure-Samples/azure-search-dotnet-samples",

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

Lines changed: 3 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -238,140 +238,15 @@ Set up a Maven project with the required dependencies.
238238

239239
1. Create a `pom.xml` file with the following content:
240240

241-
```xml
242-
<?xml version="1.0" encoding="UTF-8"?>
243-
<project xmlns="http://maven.apache.org/POM/4.0.0"
244-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
245-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
246-
http://maven.apache.org/xsd/maven-4.0.0.xsd">
247-
<modelVersion>4.0.0</modelVersion>
248-
249-
<groupId>com.example.rag</groupId>
250-
<artifactId>rag-quickstart</artifactId>
251-
<version>1.0-SNAPSHOT</version>
252-
253-
<properties>
254-
<maven.compiler.source>21</maven.compiler.source>
255-
<maven.compiler.target>21</maven.compiler.target>
256-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
257-
</properties>
258-
259-
<dependencies>
260-
<dependency>
261-
<groupId>com.azure</groupId>
262-
<artifactId>azure-search-documents</artifactId>
263-
<version>11.7.8</version>
264-
</dependency>
265-
<dependency>
266-
<groupId>com.azure</groupId>
267-
<artifactId>azure-identity</artifactId>
268-
<version>1.16.3</version>
269-
</dependency>
270-
<dependency>
271-
<groupId>com.azure</groupId>
272-
<artifactId>azure-ai-openai</artifactId>
273-
<version>1.0.0-beta.16</version>
274-
</dependency>
275-
</dependencies>
276-
</project>
277-
```
241+
:::code language="xml" source="~/azure-search-java-samples/rag-quickstart/pom.xml" :::
278242

279243
## Set up query and chat thread
280244

281245
Create a query script that uses the Azure AI Search index and the chat model to generate responses based on grounding data. The following steps guide you through setting up the query script.
282246

283247
1. Create a `Query.java` file in the `src/main/java/com/example/rag` directory with the following code:
284248

285-
```java
286-
package com.example.rag;
287-
288-
import com.azure.ai.openai.OpenAIClient;
289-
import com.azure.ai.openai.OpenAIClientBuilder;
290-
import com.azure.ai.openai.models.ChatCompletions;
291-
import com.azure.ai.openai.models.ChatCompletionsOptions;
292-
import com.azure.ai.openai.models.ChatRequestSystemMessage;
293-
import com.azure.ai.openai.models.ChatRequestUserMessage;
294-
import com.azure.identity.DefaultAzureCredentialBuilder;
295-
import com.azure.search.documents.SearchClient;
296-
import com.azure.search.documents.SearchClientBuilder;
297-
import com.azure.search.documents.models.SearchOptions;
298-
import com.azure.search.documents.models.SearchResult;
299-
300-
import java.util.List;
301-
302-
public class Query {
303-
private static SearchClient getSearchClient() {
304-
String searchEndpoint = System.getenv("AZURE_SEARCH_ENDPOINT");
305-
String searchIndex = System.getenv("AZURE_SEARCH_INDEX_NAME");
306-
307-
return new SearchClientBuilder()
308-
.endpoint(searchEndpoint)
309-
.indexName(searchIndex)
310-
.credential(new DefaultAzureCredentialBuilder().build())
311-
.buildClient();
312-
}
313-
314-
private static OpenAIClient getOpenAIClient() {
315-
String openaiEndpoint = System.getenv("AZURE_OPENAI_ENDPOINT");
316-
317-
return new OpenAIClientBuilder()
318-
.endpoint(openaiEndpoint)
319-
.credential(new DefaultAzureCredentialBuilder().build())
320-
.buildClient();
321-
}
322-
323-
private static List<SearchResult> searchDocuments(
324-
SearchClient searchClient, String query) {
325-
var searchOptions = new SearchOptions()
326-
.setTop(5)
327-
.setQueryType(com.azure.search.documents.models.QueryType.SEMANTIC)
328-
.setSemanticSearchOptions(new com.azure.search.documents.models.SemanticSearchOptions()
329-
.setSemanticConfigurationName("semantic-config"))
330-
.setSelect("HotelName", "Description", "Tags");
331-
332-
return searchClient.search(query, searchOptions, null)
333-
.stream()
334-
.limit(5)
335-
.toList();
336-
}
337-
338-
private static String queryOpenAI(OpenAIClient openAIClient,
339-
String userQuery, List<SearchResult> sources) {
340-
String deploymentModel = System.getenv("AZURE_DEPLOYMENT_MODEL");
341-
342-
String sourcesText = sources.stream()
343-
.map(source -> source.getDocument(Object.class).toString())
344-
.collect(java.util.stream.Collectors.joining("\n"));
345-
346-
var messages = List.of(
347-
new ChatRequestSystemMessage("""
348-
You are an assistant that recommends hotels based on
349-
search results."""),
350-
new ChatRequestUserMessage("""
351-
Can you recommend a few hotels that offer %s?
352-
Here are the search results:
353-
%s""".formatted(userQuery, sourcesText))
354-
);
355-
356-
var chatOptions = new ChatCompletionsOptions(messages);
357-
ChatCompletions response = openAIClient.getChatCompletions(deploymentModel, chatOptions);
358-
359-
return response.getChoices().get(0).getMessage().getContent();
360-
}
361-
362-
public static void main(String[] args) {
363-
SearchClient searchClient = getSearchClient();
364-
OpenAIClient openAIClient = getOpenAIClient();
365-
366-
String userQuery = "complimentary breakfast";
367-
List<SearchResult> sources = searchDocuments(searchClient, userQuery);
368-
String response = queryOpenAI(openAIClient, userQuery, sources);
369-
370-
System.out.println(response);
371-
System.exit(0);
372-
}
373-
}
374-
```
249+
:::code language="java" source="~/azure-search-java-samples/rag-quickstart/src/main/java/com/example/rag/Query.java" :::
375250

376251
The preceding code does the following steps:
377252
- Loads environment variables by using `System.getenv`.
@@ -435,99 +310,7 @@ Tell me their description, address, tags, and the rate for one room that sleeps
435310
1. Create a new file `QueryComplex.java` in the `src/main/java/com/example/rag` directory.
436311
1. Copy the following code to the file:
437312
438-
```java
439-
package com.example.rag;
440-
441-
import com.azure.ai.openai.OpenAIClient;
442-
import com.azure.ai.openai.OpenAIClientBuilder;
443-
import com.azure.ai.openai.models.ChatCompletions;
444-
import com.azure.ai.openai.models.ChatCompletionsOptions;
445-
import com.azure.ai.openai.models.ChatRequestSystemMessage;
446-
import com.azure.ai.openai.models.ChatRequestUserMessage;
447-
import com.azure.identity.DefaultAzureCredentialBuilder;
448-
import com.azure.search.documents.SearchClient;
449-
import com.azure.search.documents.SearchClientBuilder;
450-
import com.azure.search.documents.models.SearchOptions;
451-
import com.azure.search.documents.models.SearchResult;
452-
453-
import java.util.List;
454-
455-
public class QueryComplex {
456-
private static SearchClient getSearchClient() {
457-
String searchEndpoint = System.getenv("AZURE_SEARCH_ENDPOINT");
458-
String searchIndex = System.getenv("AZURE_SEARCH_INDEX_NAME");
459-
460-
return new SearchClientBuilder()
461-
.endpoint(searchEndpoint)
462-
.indexName(searchIndex)
463-
.credential(new DefaultAzureCredentialBuilder().build())
464-
.buildClient();
465-
}
466-
467-
private static OpenAIClient getOpenAIClient() {
468-
String openaiEndpoint = System.getenv("AZURE_OPENAI_ENDPOINT");
469-
470-
return new OpenAIClientBuilder()
471-
.endpoint(openaiEndpoint)
472-
.credential(new DefaultAzureCredentialBuilder().build())
473-
.buildClient();
474-
}
475-
476-
private static List<SearchResult> searchDocuments(
477-
SearchClient searchClient, String query) {
478-
var searchOptions = new SearchOptions()
479-
.setTop(5)
480-
.setQueryType(com.azure.search.documents.models.QueryType.SEMANTIC)
481-
.setSemanticSearchOptions(new com.azure.search.documents.models.SemanticSearchOptions()
482-
.setSemanticConfigurationName("semantic-config"))
483-
.setSelect("HotelName", "Description", "Address", "Rooms", "Tags");
484-
485-
return searchClient.search(query, searchOptions, null)
486-
.stream()
487-
.limit(5)
488-
.toList();
489-
}
490-
491-
private static String queryOpenAI(OpenAIClient openAIClient,
492-
String userQuery, List<SearchResult> sources) {
493-
String deploymentModel = System.getenv("AZURE_DEPLOYMENT_MODEL");
494-
495-
String sourcesText = sources.stream()
496-
.map(source -> source.getDocument(Object.class).toString())
497-
.collect(java.util.stream.Collectors.joining("\n"));
498-
499-
var messages = List.of(
500-
new ChatRequestSystemMessage("""
501-
You are an assistant that recommends hotels based on
502-
search results."""),
503-
new ChatRequestUserMessage("""
504-
Can you recommend a few hotels that offer %s?
505-
Tell me their description, address, tags,
506-
and the rate for one room that sleeps 4 people.
507-
Here are the search results:
508-
%s""".formatted(userQuery, sourcesText))
509-
);
510-
511-
var chatOptions = new ChatCompletionsOptions(messages);
512-
ChatCompletions response = openAIClient.getChatCompletions(
513-
deploymentModel, chatOptions);
514-
515-
return response.getChoices().get(0).getMessage().getContent();
516-
}
517-
518-
public static void main(String[] args) {
519-
SearchClient searchClient = getSearchClient();
520-
OpenAIClient openAIClient = getOpenAIClient();
521-
522-
String userQuery = "complimentary breakfast";
523-
List<SearchResult> sources = searchDocuments(searchClient, userQuery);
524-
String response = queryOpenAI(openAIClient, userQuery, sources);
525-
526-
System.out.println(response);
527-
System.exit(0);
528-
}
529-
}
530-
```
313+
:::code language="java" source="~/azure-search-java-samples/rag-quickstart/src/main/java/com/example/rag/QueryComplex.java" :::
531314
532315
1. Run the following command in a terminal to execute the query script:
533316

0 commit comments

Comments
 (0)