|
| 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 | +} |
0 commit comments