Skip to content

Commit 4176a9a

Browse files
authored
Merge pull request #205 from sahithikkss/sahithikkss/SamplesCopy
[QnAMaker] Copy Sample to root folder QnAMaker/KnowledgebaseCrudSample
2 parents 6efa713 + 373baae commit 4176a9a

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>netcoreapp2.1;netstandard1.4</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker" Version="1.0.0" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27703.2042
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.CognitiveServices.Samples.QnAMaker", "Microsoft.Azure.CognitiveServices.Samples.QnAMaker.csproj", "{88276BFF-0C5C-4691-A81B-139F0C594451}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{88276BFF-0C5C-4691-A81B-139F0C594451}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{88276BFF-0C5C-4691-A81B-139F0C594451}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{88276BFF-0C5C-4691-A81B-139F0C594451}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{88276BFF-0C5C-4691-A81B-139F0C594451}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {BA833EFD-100C-4918-8A60-1248DA1BAF54}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Microsoft.Azure.CognitiveServices.Samples.QnAMaker
2+
{
3+
using System;
4+
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
// API details
10+
string apiKey = "API KEY HERE";
11+
string endpoint = "https://westus.api.cognitive.microsoft.com";
12+
13+
// Run Sample
14+
KnowledgebaseCrudSample.Run(apiKey, endpoint).Wait();
15+
16+
Console.WriteLine("\nPress ENTER to exit.");
17+
Console.ReadLine();
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)