Skip to content

Commit cf48068

Browse files
committed
edits
1 parent 5babb13 commit cf48068

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

articles/cognitive-services/QnAMaker/includes/quickstart-sdk-nodejs.md

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Azure Cognitive Services are represented by Azure resources that you subscribe t
3030

3131
After getting the key and endpoint from your resource, get the values from the Azure portal, for your new resource, on the Quickstart page.
3232

33-
[Create environment variables](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account#configure-an-environment-variable-for-authentication), named `QNAMAKER_AUTHORING_KEY` and `QNAMAKER_ENDPOINT`. You can copy the `.env.sample` file to `.env` and use the environment variables in that file.
33+
[Create environment variables](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account#configure-an-environment-variable-for-authentication), named `QNAMAKER_AUTHORING_KEY` and `QNAMAKER_ENDPOINT`. You can copy the `[.env.sample](https://github.com/Azure-Samples/cognitive-services-quickstart-code/blob/master/javascript/QnAMaker/sdk/.env.sample)` file to `.env` and use the environment variables in that file.
3434

3535
### Create a new Node.js application
3636

@@ -48,10 +48,10 @@ npm init -y
4848

4949
### Install the client library
5050

51-
Install the required NPM packages:
51+
Install the required and optional NPM packages:
5252

5353
```console
54-
npm install azure-cognitiveservices--qnamaker ms-rest-azure dotenv
54+
npm install @azure/cognitiveservices-qnamaker @azure/ms-rest-js dotenv
5555
```
5656

5757
Your app's `package.json` file is updated with the dependencies. The `dotenv` is optional and is used to allow you to set the environment variables in a text file. Do not check the `.env` into your source control.
@@ -79,7 +79,7 @@ These code snippets show you how to do the following with the QnA Maker client l
7979

8080
## Add the dependencies
8181

82-
Create a file named `index.js`. Add the QnA Maker library and the Azure REST library to the file.
82+
Create a file named `index.js`. Add the QnA Maker library and the dependencies to the file.
8383

8484
[!code-javascript[Require statements](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=dependencies)]
8585

@@ -95,7 +95,7 @@ Create variables for your resource's Azure endpoint and key. If you created the
9595

9696
## Authenticate the client
9797

98-
Next, create a ServiceClientCredentials object with your key, and use it with your endpoint to create an [QnAMakerClient](https://docs.microsoft.com/javascript/api/%40azure/cognitiveservices-qnamaker/qnamakerclient?view=azure-node-latest#qnamakerclient-serviceclientcredentials--string--msrest-serviceclientoptions-) object. Use the client object to get a [knowledge base](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/knowledgebase?view=azure-node-latest) object.
98+
Next, create a ApiKeyCredentials object with your key, and use it with your endpoint to create an [QnAMakerClient](https://docs.microsoft.com/javascript/api/%40azure/cognitiveservices-qnamaker/qnamakerclient?view=azure-node-latest#qnamakerclient-serviceclientcredentials--string--msrest-serviceclientoptions-) object. Use the client object to get a [knowledge base client](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/knowledgebase?view=azure-node-latest) object.
9999

100100

101101
[!code-javascript[Authorization to resource key](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=authorization)]
@@ -108,46 +108,50 @@ A knowledge base stores question and answer pairs for the [CreateKbDTO](https://
108108
* For **files**, use the [FileDTO](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/filedto?view=azure-node-latest) object.
109109
* For **URLs**, use a list of strings.
110110

111-
Call the [create](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/knowledgebase?view=azure-node-latest#create-createkbdto--servicecallback-operation--) method then pass the returned operation ID to the [Operations.getDetails](#get-status-of-an-operation) method to poll for status.
111+
Call the [create](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/knowledgebase?view=azure-node-latest#create-createkbdto--servicecallback-operation--) method with the knowledge base information. The knowledge base information is basically a JSON object.
112112

113-
[!code-javascript[Create a knowledge base](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=createKnowledgeBase&highlight=15)]
113+
When the create method returns, pass the returned operation ID to the [wait_for_operation](#get-status-of-an-operation) method to poll for status. The wait_for_operation method returns when the operation completes. Parse the `resourceLocation` header value of the returned operation to get the new knowledge base ID.
114+
115+
[!code-javascript[Create a knowledge base](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=createKnowledgeBase&highlight=15,30)]
114116

115117
Make sure the include the [`wait_for_operation`](#get-status-of-an-operation) function, referenced in the above code, in order to successfully create a knowledge base.
116118

117119
## Update a knowledge base
118120

119-
You can update a knowledge base by passing in the knowledge base ID and an [UpdateKbOperationDTO](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/updatekboperationdto?view=azure-node-latest) containing [add](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/updatekboperationdto?view=azure-node-latest#add), [update](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/updatekboperationdto?view=azure-node-latest#update), and [delete](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/updatekboperationdto?view=azure-node-latest#deleteproperty) DTO objects to the [update](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/knowledgebase?view=azure-node-latest#update-string--updatekboperationdto--msrest-requestoptionsbase-) method. Use the [Operation.getDetail](#get-status-of-an-operation) method to determine if the update succeeded.
121+
You can update a knowledge base by passing in the knowledge base ID and an [UpdateKbOperationDTO](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/updatekboperationdto?view=azure-node-latest) containing [add](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/updatekboperationdto?view=azure-node-latest#add), [update](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/updatekboperationdto?view=azure-node-latest#update), and [delete](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/updatekboperationdto?view=azure-node-latest#deleteproperty) DTO objects to the [update](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/knowledgebase?view=azure-node-latest#update-string--updatekboperationdto--msrest-requestoptionsbase-) method. The DTOs are also basically JSON objects. Use the [wait_for_operation](#get-status-of-an-operation) method to determine if the update succeeded.
120122

121-
[!code-javascript[Update a knowledge base](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=updateKnowledgeBase&highlight=19)]
123+
[!code-javascript[Update a knowledge base](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=updateKnowledgeBase&highlight=28)]
122124

123125
Make sure the include the [`wait_for_operation`](#get-status-of-an-operation) function, referenced in the above code, in order to successfully update a knowledge base.
124126

125127
## Publish a knowledge base
126128

127-
Publish the knowledge base using the [publish](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/knowledgebase?view=azure-node-latest#publish-string--msrest-requestoptionsbase-) method. This takes the current saved and trained model, referenced by the knowledge base ID, and publishes that at an endpoint.
129+
Publish the knowledge base using the [publish](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/knowledgebase?view=azure-node-latest#publish-string--msrest-requestoptionsbase-) method. This takes the current saved and trained model, referenced by the knowledge base ID, and publishes that at an endpoint. Check the HTTP response code to validate publish succeeded.
130+
131+
[!code-javascript[Publish a knowledge base](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=publishKnowledgeBase&highlight=3)]
128132

129-
[!code-javascript[Publish a knowledge base](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=publishKnowledgeBase&highlight=2)]
130133

131134
# Get published endpoint
132-
Once the knowledge base is published, access the published knowledge via the query prediction runtime' generateAnswer API. In order to do this, you need the runtime's endpoint key. This is different than the authoring key.
133135

134-
[!code-javascript[Get endpoint keys](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=getEndpointKeys&highlight=2)]
136+
Once the knowledge base is published, access the published knowledge base via the query prediction runtime' generateAnswer API. In order to do this, you need the runtime's endpoint key. This is different than the authoring key.
137+
138+
[!code-javascript[Get endpoint keys](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=getEndpointKeys&highlight=3)]
135139

136140
Two endpoint keys are returned from the call. Only one is necessary to access the runtime endpoint.
137141

138142
## Delete a knowledge base
139143

140144
Delete the knowledge base using the [delete](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/knowledgebase?view=azure-node-latest#deletemethod-string--msrest-requestoptionsbase-) method with a parameter of the knowledge base ID.
141145

142-
[!code-javascript[Delete a knowledge base](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=deleteKnowledgeBase&highlight=2)]
146+
[!code-javascript[Delete a knowledge base](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=deleteKnowledgeBase&highlight=3)]
143147

144148
## Get status of an operation
145149

146150
Some methods, such as create and update, can take enough time that instead of waiting for the process to finish, an [operation](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/operations?view=azure-node-latest) is returned. Use the [operation ID](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-qnamaker/operation?view=azure-node-latest#operationid) from the operation to poll (with retry logic) to determine the status of the original method.
147151

148-
The _delayTimer_ call in the following code block is used to simulate asynchronous code. Replace this with retry logic.
152+
The _delayTimer_ call in the following code block is used to simulate the retry logic. Replace this with your own retry logic.
149153

150-
[!code-javascript[Monitor an operation](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=monitorOperation&highlight=2,17)]
154+
[!code-javascript[Monitor an operation](~/cognitive-services-quickstart-code/javascript/QnAMaker/sdk/qnamaker_quickstart.js?name=monitorOperation&highlight=8,13)]
151155

152156
## Run the application
153157

@@ -159,6 +163,32 @@ All of the code snippets in this article are [available](https://github.com/Azur
159163
node index.js
160164
```
161165

166+
The program prints status to the console:
167+
168+
```console
169+
[email protected] start C:\samples\cognitive-services-quickstart-code\javascript\QnAMaker\sdk> node index.js
170+
171+
Operation state - Running
172+
Operation state - Running
173+
Operation state - Running
174+
Operation state - Running
175+
Operation state - Running
176+
Operation state - Succeeded
177+
Create operation 200, KB ID 99df758d-f23f-4931-ab83-e738fe978e69
178+
Operation state - Running
179+
Operation state - Running
180+
Operation state - Running
181+
Operation state - Succeeded
182+
Update operation state 200 - HTTP status 200
183+
Publish request succeeded - HTTP status 204
184+
GetEndpointKeys request succeeded - HTTP status 200 - primary key 8482830b-681e-400e-b8a3-4016278aba64
185+
QnA Maker FAQ stored in English language with 1 sources, last updated 2020-01-12T16:54:40Z
186+
New KB name stored in English language with 1 sources, last updated 2020-01-12T17:32:16Z
187+
New KB name stored in English language with 1 sources, last updated 2020-01-13T00:27:46Z
188+
Delete operation state succeeded - HTTP status 204
189+
done
190+
```
191+
162192
## Clean up resources
163193

164194
If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.

0 commit comments

Comments
 (0)