Skip to content

Commit bce97c7

Browse files
authored
Merge pull request #54318 from diberry/1004-qnamaker-quickstart-create
[Cogsvcs] QnaMaker - create quickstart - go, java, python
2 parents 951bf07 + 2e72daa commit bce97c7

File tree

7 files changed

+319
-775
lines changed

7 files changed

+319
-775
lines changed

.openpublishing.publish.config.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,29 @@
224224
"branch": "master",
225225
"branch_mapping": {}
226226
},
227+
{
228+
"path_to_root": "samples-qnamaker-go",
229+
"url": "https://github.com/Azure-Samples/cognitive-services-qnamaker-go",
230+
"branch": "master",
231+
"branch_mapping": {}
232+
},
227233
{
228234
"path_to_root": "samples-qnamaker-csharp",
229235
"url": "https://github.com/Azure-Samples/cognitive-services-qnamaker-csharp",
230236
"branch": "master",
231237
"branch_mapping": {}
238+
},
239+
{
240+
"path_to_root": "samples-qnamaker-java",
241+
"url": "https://github.com/Azure-Samples/cognitive-services-qnamaker-java",
242+
"branch": "master",
243+
"branch_mapping": {}
244+
},
245+
{
246+
"path_to_root": "samples-qnamaker-python",
247+
"url": "https://github.com/Azure-Samples/cognitive-services-qnamaker-python",
248+
"branch": "master",
249+
"branch_mapping": {}
232250
}
233251
],
234252
"branch_target_mapping": {

articles/cognitive-services/QnAMaker/Quickstarts/create-new-kb-csharp.md

Lines changed: 24 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,70 @@
11
---
22
title: "Quickstart: Create knowledge base - REST, C# - QnA Maker"
33
titlesuffix: Azure Cognitive Services
4-
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.
55
services: cognitive-services
66
author: diberry
77
manager: cgronlun
88

99
ms.service: cognitive-services
1010
ms.component: qna-maker
1111
ms.topic: quickstart
12-
ms.date: 10/19/2018
12+
ms.date: 11/6/2018
1313
ms.author: diberry
1414
#Customer intent: As an API or REST developer new to the QnA Maker service, I want to programmatically create a knowledge base using C#.
1515
---
1616

1717
# Quickstart: Create a knowledge base in QnA Maker using C#
1818

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.
2020

2121
This quickstart calls QnA Maker APIs:
2222
* [Create KB](https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/5ac266295b4ccd1554da75ff)
2323
* [Get Operation Details](https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/operations_getoperationdetails)
24+
* [Publish](https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/5ac266295b4ccd1554da75fe)
2425

2526
## Prerequisites
2627

2728
* Latest [**Visual Studio Community edition**](https://www.visualstudio.com/downloads/).
2829
* 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.
2930

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).
3133
3234
## Create a knowledge base project
3335

3436
[!INCLUDE [Create Visual Studio Project](../../../../includes/cognitive-services-qnamaker-quickstart-csharp-create-project.md)]
3537

3638
## Add the required dependencies
3739

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")]
3943

4044
## Add the required constants
4145

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")]
4349

4450
## Add the KB definition
4551

4652
After the constants, add the following KB definition:
4753

48-
```csharp
49-
static string kb = @"
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?'
59-
],
60-
'metadata': [
61-
{
62-
'name': 'category',
63-
'value': 'api'
64-
}
65-
]
66-
}
67-
],
68-
'urls': [
69-
'https://docs.microsoft.com/azure/cognitive-services/qnamaker/faqs',
70-
'https://docs.microsoft.com/bot-framework/resources-bot-framework-faq'
71-
],
72-
'files': []
73-
}
74-
";
75-
```
54+
[!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")]
7655

7756
## Add supporting functions and structures
57+
Add the following code block inside the Program class:
7858

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")]
8060

8161
## Add a POST request to create KB
8262

8363
The following code makes an HTTPS request to the QnA Maker API to create a KB and receives the response:
8464

85-
```csharp
86-
async static Task<Response> PostCreateKB(string kb)
87-
{
88-
// Builds the HTTP request URI.
89-
string uri = 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.
96-
using (var client = new HttpClient())
97-
using (var request = new HttpRequestMessage())
98-
{
99-
request.Method = HttpMethod.Post;
100-
request.RequestUri = new Uri(uri);
101-
request.Content = new StringContent(kb, Encoding.UTF8, "application/json");
102-
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
103-
104-
var response = await client.SendAsync(request);
105-
var responseBody = await response.Content.ReadAsStringAsync();
106-
return new Response(response.Headers, responseBody);
107-
}
108-
}
109-
```
65+
[!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")]
11066

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.
11268

11369
```JSON
11470
{
@@ -124,30 +80,7 @@ This API call returns a JSON response that includes the operation ID. Use the op
12480

12581
Check the status of the operation.
12682

127-
```csharp
128-
async static Task<Response> GetStatus(string operationID)
129-
{
130-
// Builds the HTTP request URI.
131-
string uri = host + service + operationID;
132-
133-
// Writes the HTTP request URI to the console, for display purposes.
134-
Console.WriteLine("Calling " + uri + ".");
135-
136-
// Asynchronously invokes the Get(string) method, using the
137-
// HTTP request URI.
138-
using (var client = new HttpClient())
139-
using (var request = new HttpRequestMessage())
140-
{
141-
request.Method = HttpMethod.Get;
142-
request.RequestUri = new Uri(uri);
143-
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
144-
145-
var response = await client.SendAsync(request);
146-
var responseBody = await response.Content.ReadAsStringAsync();
147-
return new Response(response.Headers, responseBody);
148-
}
149-
}
150-
```
83+
[!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")]
15184

15285
This API call returns a JSON response that includes the operation status:
15386

@@ -176,96 +109,25 @@ Repeat the call until success or failure:
176109

177110
## Add CreateKB method
178111

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**.
180113

181-
```csharp
182-
async static void CreateKB()
183-
{
184-
try
185-
{
186-
// Starts the QnA Maker operation to create the knowledge base.
187-
var response = await PostCreateKB(kb);
188-
189-
// Retrieves the operation ID, so the operation's status can be
190-
// checked periodically.
191-
var operation = response.headers.GetValues("Location").First();
192-
193-
// Displays the JSON in the HTTP response returned by the
194-
// PostCreateKB(string) method.
195-
Console.WriteLine(PrettyPrint(response.response));
196-
197-
// Iteratively gets the state of the operation creating the
198-
// knowledge base. Once the operation state is set to something other
199-
// than "Running" or "NotStarted", the loop ends.
200-
var done = false;
201-
while (true != done)
202-
{
203-
// Gets the status of the operation.
204-
response = await GetStatus(operation);
205-
206-
// Displays the JSON in the HTTP response returned by the
207-
// GetStatus(string) method.
208-
Console.WriteLine(PrettyPrint(response.response));
209-
210-
// Deserialize the JSON into key-value pairs, to retrieve the
211-
// state of the operation.
212-
var fields = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.response);
213-
214-
// Gets and checks the state of the operation.
215-
String state = fields["operationState"];
216-
if (state.CompareTo("Running") == 0 || state.CompareTo("NotStarted") == 0)
217-
{
218-
// QnA Maker is still creating the knowledge base. The thread is
219-
// paused for a number of seconds equal to the Retry-After header value,
220-
// and then the loop continues.
221-
var wait = response.headers.GetValues("Retry-After").First();
222-
Console.WriteLine("Waiting " + wait + " seconds...");
223-
Thread.Sleep(Int32.Parse(wait) * 1000);
224-
}
225-
else
226-
{
227-
// QnA Maker has completed creating the knowledge base.
228-
done = true;
229-
}
230-
}
231-
}
232-
catch
233-
{
234-
// An error occurred while creating the knowledge base. Ensure that
235-
// you included your QnA Maker subscription key where directed in the sample.
236-
Console.WriteLine("An error occurred while creating the knowledge base.");
237-
}
238-
finally
239-
{
240-
Console.WriteLine("Press any key to continue.");
241-
}
242-
243-
}
244-
```
114+
[!code-csharp[Add CreateKB method](~/samples-qnamaker-csharp/documentation-samples/quickstarts/create-knowledge-base/QnaQuickstartCreateKnowledgebase/Program.cs?range=176-237 "Add CreateKB method")]
245115

246116
## Add the CreateKB method to Main
247117

248118
Change the Main method to call the CreateKB method:
249119

250-
```csharp
251-
static void Main(string[] args)
252-
{
253-
// Call the CreateKB() method to create a knowledge base, periodically
254-
// checking the status of the QnA Maker operation until the
255-
// knowledge base is created.
256-
CreateKB();
257-
258-
// The console waits for a key to be pressed before closing.
259-
Console.ReadLine();
260-
}
261-
```
120+
[!code-csharp[Add CreateKB method](~/samples-qnamaker-csharp/documentation-samples/quickstarts/create-knowledge-base/QnaQuickstartCreateKnowledgebase/Program.cs?range=239-248 "Add CreateKB method")]
262121

263122
## Build and run the program
264123

265124
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.
266125

267126
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.
268127

128+
129+
[!INCLUDE [Clean up files and KB](../../../../includes/cognitive-services-qnamaker-quickstart-cleanup-resources.md)]
130+
269131
## Next steps
270132

271133
> [!div class="nextstepaction"]

0 commit comments

Comments
 (0)