|
| 1 | +using Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker; |
| 2 | +using Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker.Models; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace Microsoft.Azure.CognitiveServices.Samples.QnAMaker |
| 10 | +{ |
| 11 | + public static class KnowledgebaseCrudSample |
| 12 | + { |
| 13 | + public static async Task Run(string key, string endpoint) |
| 14 | + { |
| 15 | + var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; |
| 16 | + // Create a KB |
| 17 | + Console.WriteLine("Creating KB..."); |
| 18 | + var kbId = await CreateSampleKb(client); |
| 19 | + Console.WriteLine("Created KB with ID : {0}", kbId); |
| 20 | + |
| 21 | + // Update the KB |
| 22 | + Console.WriteLine("Updating KB..."); |
| 23 | + await UpdateKB(client, kbId); |
| 24 | + Console.WriteLine("KB Updated."); |
| 25 | + |
| 26 | + // Publish the KB |
| 27 | + Console.Write("Publishing KB..."); |
| 28 | + await client.Knowledgebase.PublishAsync(kbId); |
| 29 | + Console.WriteLine("KB Published."); |
| 30 | + |
| 31 | + |
| 32 | + // Download the KB |
| 33 | + Console.Write("Downloading KB..."); |
| 34 | + var kbData = await client.Knowledgebase.DownloadAsync(kbId, EnvironmentType.Prod); |
| 35 | + Console.WriteLine("KB Downloaded. It has {0} QnAs.", kbData.QnaDocuments.Count); |
| 36 | + |
| 37 | + // Delete the KB |
| 38 | + Console.Write("Deleting KB..."); |
| 39 | + await client.Knowledgebase.DeleteAsync(kbId); |
| 40 | + Console.WriteLine("KB Deleted."); |
| 41 | + } |
| 42 | + |
| 43 | + private static async Task UpdateKB(IQnAMakerClient client, string kbId) |
| 44 | + { |
| 45 | + var updateOp = await client.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO |
| 46 | + { |
| 47 | + Add = new UpdateKbOperationDTOAdd { QnaList = new List<QnADTO> { new QnADTO { Questions = new List<string> { "bye" }, Answer = "goodbye" } } } |
| 48 | + }); |
| 49 | + |
| 50 | + // Loop while operation is success |
| 51 | + updateOp = await MonitorOperation(client, updateOp); |
| 52 | + } |
| 53 | + |
| 54 | + private static async Task<string> CreateSampleKb(IQnAMakerClient client) |
| 55 | + { |
| 56 | + var qna = new QnADTO |
| 57 | + { |
| 58 | + Answer = "You can use our REST APIs to manage your knowledge base.", |
| 59 | + Questions = new List<string> { "How do I manage my knowledgebase?" }, |
| 60 | + Metadata = new List<MetadataDTO> { new MetadataDTO { Name = "Category", Value = "api" } } |
| 61 | + }; |
| 62 | + |
| 63 | + var urls = new List<string> { "https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs" }; |
| 64 | + var createKbDto = new CreateKbDTO |
| 65 | + { |
| 66 | + Name = "QnA Maker FAQ from quickstart", |
| 67 | + QnaList = new List<QnADTO> { qna }, |
| 68 | + Urls = urls |
| 69 | + }; |
| 70 | + |
| 71 | + var createOp = await client.Knowledgebase.CreateAsync(createKbDto); |
| 72 | + createOp = await MonitorOperation(client, createOp); |
| 73 | + |
| 74 | + return createOp.ResourceLocation.Replace("/knowledgebases/", string.Empty); |
| 75 | + } |
| 76 | + |
| 77 | + private static async Task<Operation> MonitorOperation(IQnAMakerClient client, Operation operation) |
| 78 | + { |
| 79 | + // Loop while operation is success |
| 80 | + for (int i = 0; |
| 81 | + i < 20 && (operation.OperationState == OperationStateType.NotStarted || operation.OperationState == OperationStateType.Running); |
| 82 | + i++) |
| 83 | + { |
| 84 | + Console.WriteLine("Waiting for operation: {0} to complete.", operation.OperationId); |
| 85 | + await Task.Delay(5000); |
| 86 | + operation = await client.Operations.GetDetailsAsync(operation.OperationId); |
| 87 | + } |
| 88 | + |
| 89 | + if (operation.OperationState != OperationStateType.Succeeded) |
| 90 | + { |
| 91 | + throw new Exception($"Operation {operation.OperationId} failed to completed."); |
| 92 | + } |
| 93 | + return operation; |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | +} |
0 commit comments