You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: This REST-based quickstart walks you through creating a sample QnA Maker knowledge base, programmatically, that will appear in your Azure Dashboard of your Cognitive Services API account.
4
+
description: This C# REST-based quickstart walks you through creating a sample QnA Maker knowledge base, programmatically, that will appear in your Azure Dashboard of your Cognitive Services API account.
5
5
services: cognitive-services
6
6
author: diberry
7
7
manager: cgronlun
8
8
9
9
ms.service: cognitive-services
10
10
ms.component: qna-maker
11
11
ms.topic: quickstart
12
-
ms.date: 10/19/2018
12
+
ms.date: 11/6/2018
13
13
ms.author: diberry
14
14
#Customer intent: As an API or REST developer new to the QnA Maker service, I want to programmatically create a knowledge base using C#.
15
15
---
16
16
17
17
# Quickstart: Create a knowledge base in QnA Maker using C#
18
18
19
-
This quickstart walks you through programmatically creating a sample QnA Maker knowledge base. QnA Maker automatically extracts questions and answers from semi-structured content, like FAQs, from [data sources](../Concepts/data-sources-supported.md). The model for the knowledge base is defined in the JSON sent in the body of the API request.
19
+
This quickstart walks you through programmatically creating and publishing a sample QnA Maker knowledge base. QnA Maker automatically extracts questions and answers from semi-structured content, like FAQs, from [data sources](../Concepts/data-sources-supported.md). The model for the knowledge base is defined in the JSON sent in the body of the API request.
* Latest [**Visual Studio Community edition**](https://www.visualstudio.com/downloads/).
28
29
* You must have a [QnA Maker service](../How-To/set-up-qnamaker-service-azure.md). To retrieve your key, select **Keys** under **Resource Management** in your dashboard.
29
30
30
-
[!INCLUDE [Code is available in Azure-Samples Github repo](../../../../includes/cognitive-services-qnamaker-csharp-repo-note.md)]
31
+
> [!NOTE]
32
+
> The complete solution file(s) are available from the [**Azure-Samples/cognitive-services-qnamaker-csharp** Github repository](https://github.com/Azure-Samples/cognitive-services-qnamaker-csharp/tree/master/documentation-samples/quickstarts/create-and-publish-knowledge-base).
31
33
32
34
## Create a knowledge base project
33
35
34
36
[!INCLUDE [Create Visual Studio Project](../../../../includes/cognitive-services-qnamaker-quickstart-csharp-create-project.md)]
35
37
36
38
## Add the required dependencies
37
39
38
-
[!INCLUDE [Add required constants to code file](../../../../includes/cognitive-services-qnamaker-quickstart-csharp-required-dependencies.md)]
40
+
At the top of Program.cs, replace the single using statement with the following lines to add necessary dependencies to the project:
41
+
42
+
[!code-csharp[Add the required dependencies](~/samples-qnamaker-csharp/documentation-samples/quickstarts/create-knowledge-base/QnaQuickstartCreateKnowledgebase/Program.cs?range=1-11"Add the required dependencies")]
39
43
40
44
## Add the required constants
41
45
42
-
[!INCLUDE [Add required constants to code file](../../../../includes/cognitive-services-qnamaker-quickstart-csharp-required-constants.md)]
46
+
At the top of the Program class, add the following constants to access QnA Maker:
47
+
48
+
[!code-csharp[Add the required constants](~/samples-qnamaker-csharp/documentation-samples/quickstarts/create-knowledge-base/QnaQuickstartCreateKnowledgebase/Program.cs?range=17-24"Add the required constants")]
43
49
44
50
## Add the KB definition
45
51
46
52
After the constants, add the following KB definition:
47
53
48
-
```csharp
49
-
staticstringkb=@"
50
-
{
51
-
'name': 'QnA Maker FAQ from quickstart',
52
-
'qnaList': [
53
-
{
54
-
'id': 0,
55
-
'answer': 'You can use our REST APIs to manage your knowledge base. See here for details: https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a073d9e041ad42d9baa',
56
-
'source': 'Custom Editorial',
57
-
'questions': [
58
-
'How do I programmatically update my knowledge base?'
[!code-csharp[Add the required constants](~/samples-qnamaker-csharp/documentation-samples/quickstarts/create-knowledge-base/QnaQuickstartCreateKnowledgebase/Program.cs?range=32-57"Add the required constants")]
76
55
77
56
## Add supporting functions and structures
57
+
Add the following code block inside the Program class:
78
58
79
-
[!INCLUDE [Add supporting functions and structures](../../../../includes/cognitive-services-qnamaker-quickstart-csharp-support-functions.md)]
59
+
[!code-csharp[Add supporting functions and structures](~/samples-qnamaker-csharp/documentation-samples/quickstarts/create-knowledge-base/QnaQuickstartCreateKnowledgebase/Program.cs?range=62-82"Add supporting functions and structures")]
80
60
81
61
## Add a POST request to create KB
82
62
83
63
The following code makes an HTTPS request to the QnA Maker API to create a KB and receives the response:
84
64
85
-
```csharp
86
-
asyncstaticTask<Response>PostCreateKB(stringkb)
87
-
{
88
-
// Builds the HTTP request URI.
89
-
stringuri=host+service+method;
90
-
91
-
// Writes the HTTP request URI to the console, for display purposes.
92
-
Console.WriteLine("Calling "+uri+".");
93
-
94
-
// Asynchronously invokes the Post(string, string) method, using the
95
-
// HTTP request URI and the specified data source.
[!code-csharp[Add a POST request to create KB](~/samples-qnamaker-csharp/documentation-samples/quickstarts/create-knowledge-base/QnaQuickstartCreateKnowledgebase/Program.cs?range=91-105"Add a POST request to create KB")]
110
66
111
-
This API call returns a JSON response that includes the operation ID. Use the operation ID to determine if the KB is successfully created.
67
+
This API call returns a JSON response that includes the operation ID in the header field **Location**. Use the operation ID to determine if the KB is successfully created.
112
68
113
69
```JSON
114
70
{
@@ -124,30 +80,7 @@ This API call returns a JSON response that includes the operation ID. Use the op
[!code-csharp[Add GET request to determine creation status](~/samples-qnamaker-csharp/documentation-samples/quickstarts/create-knowledge-base/QnaQuickstartCreateKnowledgebase/Program.cs?range=159-170"Add GET request to determine creation status")]
151
84
152
85
This API call returns a JSON response that includes the operation status:
153
86
@@ -176,96 +109,25 @@ Repeat the call until success or failure:
176
109
177
110
## Add CreateKB method
178
111
179
-
The following method creates the KB and repeats checks on the status. Because the KB creation may take some time, you need to repeat calls to check the status until the status is either successful or fails.
112
+
The following method creates the KB and repeats checks on the status. The _create_**Operation ID** is returned in the POST response header field **Location**, then used as part of the route in the GET request. Because the KB creation may take some time, you need to repeat calls to check the status until the status is either successful or fails. When the operation succeeds, the KB ID is returned in **resourceLocation**.
180
113
181
-
```csharp
182
-
asyncstaticvoidCreateKB()
183
-
{
184
-
try
185
-
{
186
-
// Starts the QnA Maker operation to create the knowledge base.
187
-
varresponse=awaitPostCreateKB(kb);
188
-
189
-
// Retrieves the operation ID, so the operation's status can be
Build and run the program. It will automatically send the request to the QnA Maker API to create the KB, then it will poll for the results every 30 seconds. Each response is printed to the console window.
266
125
267
126
Once your knowledge base is created, you can view it in your QnA Maker Portal, [My knowledge bases](https://www.qnamaker.ai/Home/MyServices) page.
268
127
128
+
129
+
[!INCLUDE [Clean up files and KB](../../../../includes/cognitive-services-qnamaker-quickstart-cleanup-resources.md)]
0 commit comments