Skip to content

Commit 84ae9bb

Browse files
committed
update samples
1 parent eb58404 commit 84ae9bb

10 files changed

Lines changed: 720 additions & 0 deletions

sdk/search/azure-search-documents/src/samples/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ The following sections provide several code snippets covering some of the most c
9999
- [Rewrite Request URL to replace OData URL syntax with standard syntax](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/SearchRequestUrlRewriterPolicy.java)
100100
- [Vector search using reduced embeddings](https://github.com/Azure/azure-sdk-for-java/blob/40261403b3a75aa56a3eeaf18c2ba0fd071c87a6/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/VectorSearchReducedEmbeddings.java)
101101

102+
### Knowledge Base & Knowledge Source Preview Samples
103+
- [Knowledge Base configuration (preview)](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/KnowledgeBasePreviewConfigurationExample.java)
104+
- [Knowledge Base retrieval response parsing (preview)](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/KnowledgeRetrievalPreviewResponseExample.java)
105+
- [Knowledge service statistics (preview)](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/KnowledgeServiceStatsPreviewExample.java)
106+
- [Knowledge Source freshness and defaults (preview)](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/KnowledgeSourceFreshnessPreviewExample.java)
107+
- [File Knowledge Source (preview)](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/KnowledgeSourceFilePreviewExample.java)
108+
- [MCP Server Knowledge Source (preview)](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/KnowledgeSourceMcpServerPreviewExample.java)
109+
- [Work IQ Knowledge Source (preview)](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/KnowledgeSourceWorkIqPreviewExample.java)
110+
- [Fabric Ontology Knowledge Source (preview)](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/KnowledgeSourceFabricOntologyPreviewExample.java)
111+
- [Fabric Data Agent Knowledge Source (preview)](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/KnowledgeSourceFabricDataAgentPreviewExample.java)
112+
102113
## Troubleshooting
103114
Troubleshooting steps can be found [here][SDK_README_TROUBLESHOOTING].
104115

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.search.documents;
5+
6+
import com.azure.core.credential.AzureKeyCredential;
7+
import com.azure.search.documents.indexes.SearchIndexClient;
8+
import com.azure.search.documents.indexes.SearchIndexClientBuilder;
9+
import com.azure.search.documents.indexes.models.AzureOpenAIModelName;
10+
import com.azure.search.documents.indexes.models.AzureOpenAIVectorizerParameters;
11+
import com.azure.search.documents.indexes.models.CorsOptions;
12+
import com.azure.search.documents.indexes.models.KnowledgeBase;
13+
import com.azure.search.documents.indexes.models.KnowledgeBaseAzureOpenAIModel;
14+
import com.azure.search.documents.indexes.models.KnowledgeSourceReference;
15+
import com.azure.search.documents.knowledgebases.models.KnowledgeRetrievalMinimalReasoningEffort;
16+
import com.azure.search.documents.knowledgebases.models.KnowledgeRetrievalOutputMode;
17+
18+
/**
19+
* This example shows how to create and update a knowledge base using preview-only configuration knobs.
20+
* <p>
21+
* It demonstrates:
22+
* <ul>
23+
* <li>Setting GPT-5.x model names for query planning</li>
24+
* <li>Configuring KB-level retrieval defaults (reasoning effort, output mode, instructions)</li>
25+
* <li>CORS options</li>
26+
* <li>Encryption key (CMK)</li>
27+
* <li>Knowledge source with preview-relevant defaults (image serving, freshness)</li>
28+
* <li>Updating an existing knowledge base</li>
29+
* </ul>
30+
* <p>
31+
* Set the following environment variables before running this sample:
32+
* <ul>
33+
* <li>SEARCH_ENDPOINT - the endpoint of your Azure AI Search service</li>
34+
* <li>SEARCH_API_KEY - the admin key of your Azure AI Search service</li>
35+
* </ul>
36+
*/
37+
public class KnowledgeBasePreviewConfigurationExample {
38+
39+
private static final String ENDPOINT = System.getenv("SEARCH_ENDPOINT");
40+
private static final String API_KEY = System.getenv("SEARCH_API_KEY");
41+
private static final String KB_NAME = "my-knowledge-base";
42+
43+
public static void main(String[] args) {
44+
SearchIndexClient searchIndexClient = new SearchIndexClientBuilder()
45+
.credential(new AzureKeyCredential(API_KEY))
46+
.endpoint(ENDPOINT)
47+
.buildClient();
48+
49+
try {
50+
// --- Create a knowledge base with preview configuration knobs ---
51+
52+
// Knowledge source reference with preview-relevant defaults
53+
KnowledgeSourceReference knowledgeSource = new KnowledgeSourceReference("my-knowledge-source")
54+
.setEnableImageServing(true)
55+
.setEnableFreshness(true);
56+
57+
KnowledgeBase knowledgeBase = new KnowledgeBase(KB_NAME, knowledgeSource);
58+
59+
// GPT-5.x model for query planning
60+
knowledgeBase.setModels(
61+
new KnowledgeBaseAzureOpenAIModel(
62+
new AzureOpenAIVectorizerParameters()
63+
.setModelName(AzureOpenAIModelName.GPT54)
64+
.setResourceUrl("https://my-openai-resource.openai.azure.com/")
65+
.setDeploymentName("my-deployment")
66+
)
67+
);
68+
69+
// KB-level retrieval defaults
70+
knowledgeBase.setRetrievalReasoningEffort(new KnowledgeRetrievalMinimalReasoningEffort());
71+
knowledgeBase.setOutputMode(KnowledgeRetrievalOutputMode.ANSWER_SYNTHESIS);
72+
knowledgeBase.setRetrievalInstructions("Focus on finding hotel listings with amenities and pricing information");
73+
knowledgeBase.setAnswerInstructions("Provide concise answers in bullet point format");
74+
75+
// CORS and encryption
76+
knowledgeBase.setCorsOptions(new CorsOptions("https://my-allowed-origin.com").setMaxAgeInSeconds(3600L));
77+
// Uncomment below if your service has managed identity configured for Key Vault access:
78+
// knowledgeBase.setEncryptionKey(new SearchResourceEncryptionKey("my-key", "https://my-key-vault.vault.azure.net"));
79+
knowledgeBase.setDescription("Knowledge base with custom configuration for retrieval and answer generation");
80+
81+
searchIndexClient.createOrUpdateKnowledgeBase(knowledgeBase);
82+
System.out.println("Knowledge base created.");
83+
84+
// --- Update an existing knowledge base ---
85+
86+
KnowledgeBase existingKb = searchIndexClient.getKnowledgeBase(KB_NAME);
87+
existingKb.setRetrievalInstructions("Updated: prioritize results from the last 30 days");
88+
existingKb.setAnswerInstructions("Updated: answer in full sentences with citations");
89+
existingKb.setOutputMode(KnowledgeRetrievalOutputMode.EXTRACTIVE_DATA);
90+
91+
searchIndexClient.createOrUpdateKnowledgeBase(existingKb);
92+
System.out.println("Knowledge base updated.");
93+
} finally {
94+
searchIndexClient.deleteKnowledgeBase(KB_NAME);
95+
}
96+
}
97+
}
98+
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.search.documents;
5+
6+
import com.azure.core.credential.AzureKeyCredential;
7+
import com.azure.search.documents.indexes.SearchIndexClient;
8+
import com.azure.search.documents.indexes.SearchIndexClientBuilder;
9+
import com.azure.search.documents.indexes.models.KnowledgeBase;
10+
import com.azure.search.documents.indexes.models.KnowledgeSourceReference;
11+
import com.azure.search.documents.knowledgebases.KnowledgeBaseRetrievalClient;
12+
import com.azure.search.documents.knowledgebases.KnowledgeBaseRetrievalClientBuilder;
13+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseActivityRecord;
14+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseMessage;
15+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseMessageContent;
16+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseMessageTextContent;
17+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseModelAnswerSynthesisActivityRecord;
18+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseReference;
19+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseRetrievalOptions;
20+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseRetrievalResult;
21+
import com.azure.search.documents.knowledgebases.models.KnowledgeRetrievalOutputMode;
22+
import com.azure.search.documents.knowledgebases.models.KnowledgeRetrievalSemanticIntent;
23+
import com.azure.search.documents.knowledgebases.models.PurviewSensitivityLabelInfo;
24+
25+
public class KnowledgeRetrievalPreviewResponseExample {
26+
private static final String ENDPOINT = System.getenv("SEARCH_ENDPOINT");
27+
private static final String API_KEY = System.getenv("SEARCH_API_KEY");
28+
private static final String KB_NAME = "my-knowledge-base";
29+
30+
public static void main(String[] args) {
31+
SearchIndexClient searchIndexClient = new SearchIndexClientBuilder()
32+
.credential(new AzureKeyCredential(API_KEY))
33+
.endpoint(ENDPOINT)
34+
.buildClient();
35+
try {
36+
KnowledgeSourceReference knowledgeSource = new KnowledgeSourceReference("my-knowledge-source")
37+
.setEnableImageServing(true)
38+
.setEnableFreshness(true);
39+
40+
KnowledgeBase knowledgeBase = new KnowledgeBase(KB_NAME, knowledgeSource);
41+
searchIndexClient.createOrUpdateKnowledgeBase(knowledgeBase);
42+
43+
//build retrieval client
44+
KnowledgeBaseRetrievalClient retrievalClient = new KnowledgeBaseRetrievalClientBuilder()
45+
.credential(new AzureKeyCredential(API_KEY))
46+
.endpoint(ENDPOINT)
47+
.knowledgeBaseName(KB_NAME)
48+
.buildClient();
49+
50+
//build retrieval request with preview options
51+
KnowledgeBaseRetrievalOptions options = new KnowledgeBaseRetrievalOptions()
52+
.setMaxOutputDocuments(5)
53+
.setIncludeActivity(true)
54+
.setOutputMode(KnowledgeRetrievalOutputMode.ANSWER_SYNTHESIS)
55+
.setIntents(new KnowledgeRetrievalSemanticIntent("What hotels have free wifi?"));
56+
57+
//send request
58+
KnowledgeBaseRetrievalResult result = retrievalClient.retrieve(KB_NAME, options);
59+
60+
//parse result
61+
62+
for (KnowledgeBaseMessage message : result.getResponse()) {
63+
System.out.println("Message role: " + message.getRole());
64+
for (KnowledgeBaseMessageContent content : message.getContent()) {
65+
if (content instanceof KnowledgeBaseMessageTextContent) {
66+
System.out.println("Text content: " + ((KnowledgeBaseMessageTextContent) content).getText());
67+
}
68+
}
69+
}
70+
71+
for (KnowledgeBaseActivityRecord record : result.getActivity()) {
72+
System.out.println("Activity [" + record.getType() + "] id=" + record.getId()
73+
+ " elapsed=" + record.getElapsedMs() + "ms");
74+
if (record instanceof KnowledgeBaseModelAnswerSynthesisActivityRecord) {
75+
KnowledgeBaseModelAnswerSynthesisActivityRecord synthesis =
76+
(KnowledgeBaseModelAnswerSynthesisActivityRecord) record;
77+
System.out.println(" Model: " + synthesis.getModelName());
78+
System.out.println(" Input tokens: " + synthesis.getInputTokens());
79+
System.out.println(" Output tokens: " + synthesis.getOutputTokens());
80+
}
81+
}
82+
83+
for (KnowledgeBaseReference ref : result.getReferences()) {
84+
System.out.println("Reference id=" + ref.getId() + " type=" + ref.getType()
85+
+ " rerankerScore=" + ref.getRerankerScore());
86+
if (ref.getSourceData() != null) {
87+
System.out.println(" Source data: " + ref.getSourceData().toString());
88+
}
89+
}
90+
91+
PurviewSensitivityLabelInfo labelInfo = result.getResponseSensitivityLabelInfo();
92+
if (labelInfo != null) {
93+
System.out.println("Sensitivity Label " + labelInfo.getDisplayName());
94+
System.out.println(" Label ID: " + labelInfo.getSensitivityLabelId());
95+
System.out.println(" Priority: " + labelInfo.getPriority());
96+
System.out.println(" Color: " + labelInfo.getColor());
97+
}
98+
99+
} finally {
100+
searchIndexClient.deleteKnowledgeBase(KB_NAME);
101+
}
102+
}
103+
104+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.search.documents;
5+
6+
import com.azure.core.credential.AzureKeyCredential;
7+
import com.azure.search.documents.indexes.SearchIndexClient;
8+
import com.azure.search.documents.indexes.SearchIndexClientBuilder;
9+
import com.azure.search.documents.indexes.models.ResourceCounter;
10+
import com.azure.search.documents.indexes.models.SearchServiceCounters;
11+
import com.azure.search.documents.indexes.models.SearchServiceStatistics;
12+
13+
/**
14+
* Demonstrates retrieving service-level knowledge base and knowledge source counters
15+
* introduced in the preview API.
16+
*
17+
* These counters provide visibility into Knowledge Retrieval resource usage and quotas
18+
* on your Azure AI Search service.
19+
*/
20+
public class KnowledgeServiceStatsPreviewExample {
21+
22+
public static void main(String[] args) {
23+
SearchIndexClient client = new SearchIndexClientBuilder()
24+
.endpoint(System.getenv("SEARCH_ENDPOINT"))
25+
.credential(new AzureKeyCredential(System.getenv("SEARCH_API_KEY")))
26+
.buildClient();
27+
28+
// Retrieve service statistics
29+
SearchServiceStatistics stats = client.getServiceStatistics();
30+
SearchServiceCounters counters = stats.getCounters();
31+
32+
// New preview counters for Knowledge Retrieval objects
33+
ResourceCounter kbCounter = counters.getKnowledgeBaseCounter();
34+
ResourceCounter ksCounter = counters.getKnowledgeSourceCounter();
35+
36+
System.out.println("Knowledge Bases — usage: " + kbCounter.getUsage() + ", quota: " + kbCounter.getQuota());
37+
System.out.println("Knowledge Sources — usage: " + ksCounter.getUsage() + ", quota: " + ksCounter.getQuota());
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.search.documents;
5+
6+
import com.azure.core.credential.AzureKeyCredential;
7+
import com.azure.search.documents.indexes.SearchIndexClient;
8+
import com.azure.search.documents.indexes.SearchIndexClientBuilder;
9+
import com.azure.search.documents.indexes.models.FabricDataAgentKnowledgeSource;
10+
import com.azure.search.documents.indexes.models.FabricDataAgentKnowledgeSourceParameters;
11+
import com.azure.search.documents.indexes.models.KnowledgeBase;
12+
import com.azure.search.documents.indexes.models.KnowledgeSource;
13+
import com.azure.search.documents.indexes.models.KnowledgeSourceReference;
14+
import com.azure.search.documents.knowledgebases.KnowledgeBaseRetrievalClient;
15+
import com.azure.search.documents.knowledgebases.KnowledgeBaseRetrievalClientBuilder;
16+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseRetrievalOptions;
17+
import com.azure.search.documents.knowledgebases.models.KnowledgeBaseRetrievalResult;
18+
import com.azure.search.documents.knowledgebases.models.KnowledgeRetrievalSemanticIntent;
19+
20+
/**
21+
* Demonstrates creating and using a Fabric Data Agent knowledge source in the preview API.
22+
*/
23+
public class KnowledgeSourceFabricDataAgentPreviewExample {
24+
25+
private static final String KB_NAME = "fabric-data-agent-kind-sample-kb";
26+
private static final String KS_NAME = "fabric-data-agent-kind-sample-ks";
27+
28+
public static void main(String[] args) {
29+
String endpoint = System.getenv("SEARCH_ENDPOINT");
30+
String apiKey = System.getenv("SEARCH_API_KEY");
31+
String workspaceId = System.getenv("FABRIC_WORKSPACE_ID");
32+
String dataAgentId = System.getenv("FABRIC_DATA_AGENT_ID");
33+
34+
SearchIndexClient searchIndexClient = new SearchIndexClientBuilder()
35+
.endpoint(endpoint)
36+
.credential(new AzureKeyCredential(apiKey))
37+
.buildClient();
38+
39+
KnowledgeBaseRetrievalClient retrievalClient = new KnowledgeBaseRetrievalClientBuilder()
40+
.endpoint(endpoint)
41+
.credential(new AzureKeyCredential(apiKey))
42+
.knowledgeBaseName(KB_NAME)
43+
.buildClient();
44+
45+
try {
46+
// Create KS — Fabric Data Agent requires a workspace ID and data agent ID
47+
FabricDataAgentKnowledgeSource knowledgeSource = new FabricDataAgentKnowledgeSource(
48+
KS_NAME,
49+
new FabricDataAgentKnowledgeSourceParameters(workspaceId, dataAgentId)
50+
);
51+
searchIndexClient.createOrUpdateKnowledgeSource(knowledgeSource);
52+
53+
// Verify KS kind
54+
KnowledgeSource retrieved = searchIndexClient.getKnowledgeSource(KS_NAME);
55+
System.out.println("KnowledgeSource kind = " + retrieved.getKind());
56+
57+
// Hook up KS to a KB
58+
KnowledgeSourceReference ref = new KnowledgeSourceReference(KS_NAME);
59+
KnowledgeBase knowledgeBase = new KnowledgeBase(KB_NAME, ref);
60+
searchIndexClient.createOrUpdateKnowledgeBase(knowledgeBase);
61+
System.out.println("Created KnowledgeBase " + KB_NAME + " referencing " + KS_NAME);
62+
63+
// Issue retrieval request to verify everything is wired up end-to-end
64+
KnowledgeBaseRetrievalOptions options = new KnowledgeBaseRetrievalOptions()
65+
.setIntents(new KnowledgeRetrievalSemanticIntent("What data is the agent managing?"));
66+
KnowledgeBaseRetrievalResult result = retrievalClient.retrieve(options);
67+
System.out.println("Response messages: " + result.getResponse().size());
68+
69+
} finally {
70+
searchIndexClient.deleteKnowledgeBase(KB_NAME);
71+
searchIndexClient.deleteKnowledgeSource(KS_NAME);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)