Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 77 additions & 39 deletions sdk/cognitiveservices/cognitiveservices-qnamaker/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## An isomorphic javascript sdk for - QnAMakerClient
## An isomorphic javascript sdk for - QnAMakerRuntimeClient

This package contains an isomorphic SDK for QnAMakerClient.
This package contains an isomorphic SDK for QnAMakerRuntimeClient.

### Currently supported environments

Expand All @@ -15,73 +15,111 @@ npm install @azure/cognitiveservices-qnamaker

### How to use

#### nodejs - Authentication, client creation and getKeys endpointKeys as an example written in TypeScript.
#### nodejs - Authentication, client creation and generateAnswer runtime as an example written in TypeScript.

##### Install @azure/ms-rest-azure-js
##### Install @azure/ms-rest-nodeauth

- Please install minimum version of `"@azure/ms-rest-nodeauth": "^3.0.0"`.
```bash
npm install @azure/ms-rest-azure-js
npm install @azure/ms-rest-nodeauth@"^3.0.0"
```

##### Sample code
The following sample lists the keys of the QnAMaker endpoint. To know more, refer to the [Azure Documentation on QnAMaker](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/)

```typescript
import { QnAMakerClient } from "@azure/cognitiveservices-qnamaker";
import { CognitiveServicesCredentials } from "@azure/ms-rest-azure-js";

const qnaMakerKey = process.env["YOUR_QNAMAKER_KEY"] || "<YOUR_QNAMAKER_KEY>";
const endpoint =
process.env["YOUR_QNAMAKER_ENDPOINT"] || "<YOUR_QNAMAKER_ENDPOINT>";

const creds = new CognitiveServicesCredentials(qnaMakerKey);

const client = new QnAMakerClient(creds, endpoint);
client.endpointKeys
.getKeys()
.then(result => {
import * as msRest from "@azure/ms-rest-js";
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
import { QnAMakerRuntimeClient, QnAMakerRuntimeModels, QnAMakerRuntimeMappers } from "@azure/cognitiveservices-qnamaker";
const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];

msRestNodeAuth.interactiveLogin().then((creds) => {
const client = new QnAMakerRuntimeClient(creds, subscriptionId);
const kbId = "testkbId";
const generateAnswerPayload: QnAMakerRuntimeModels.QueryDTO = {
qnaId: "testqnaId",
question: "testquestion",
top: 1,
userId: "testuserId",
isTest: true,
scoreThreshold: 1.01,
context: {
previousQnaId: "testpreviousQnaId",
previousUserQuery: "testpreviousUserQuery"
},
rankerType: "testrankerType",
strictFilters: [{
name: "testname",
value: "testvalue"
}]
};
client.runtime.generateAnswer(kbId, generateAnswerPayload).then((result) => {
console.log("The result is:");
console.log(result);
})
.catch(err => {
console.error(err);
});
}).catch((err) => {
console.error(err);
});
```

#### browser - Authentication, client creation and getKeys endpointKeys as an example written in JavaScript.
#### browser - Authentication, client creation and generateAnswer runtime as an example written in JavaScript.

##### Install @azure/ms-rest-browserauth

```bash
npm install @azure/ms-rest-browserauth
```

##### Sample code

See https://github.com/Azure/ms-rest-browserauth to learn how to authenticate to Azure in the browser.

- index.html
```html
<!DOCTYPE html>
<html lang="en">
<head>
<title>@azure/cognitiveservices-qnamaker sample</title>
<script src="node_modules/@azure/ms-rest-js/dist/msRest.browser.js"></script>
<script src="node_modules/@azure/ms-rest-browserauth/dist/msAuth.js"></script>
<script src="node_modules/@azure/cognitiveservices-qnamaker/dist/cognitiveservices-qnamaker.js"></script>
<script type="text/javascript">
const qnaMakerKey = "<YOUR_QNAMAKER_KEY>";
const endpoint = "<YOUR_QNAMAKER_ENDPOINT>";
const cognitiveServiceCredentials = new msRest.ApiKeyCredentials({
inHeader: {
"Ocp-Apim-Subscription-Key": qnaMakerKey
}
const subscriptionId = "<Subscription_Id>";
const authManager = new msAuth.AuthManager({
clientId: "<client id for your Azure AD app>",
tenant: "<optional tenant for your organization>"
});
const client = new Azure.CognitiveservicesQnamaker.QnAMakerClient(
cognitiveServiceCredentials,
endpoint
);

client.endpointKeys
.getKeys()
.then(result => {
authManager.finalizeLogin().then((res) => {
if (!res.isLoggedIn) {
// may cause redirects
authManager.login();
}
const client = new Azure.CognitiveservicesQnamaker.QnAMakerRuntimeClient(res.creds, subscriptionId);
const kbId = "testkbId";
const generateAnswerPayload = {
qnaId: "testqnaId",
question: "testquestion",
top: 1,
userId: "testuserId",
isTest: true,
scoreThreshold: 1.01,
context: {
previousQnaId: "testpreviousQnaId",
previousUserQuery: "testpreviousUserQuery"
},
rankerType: "testrankerType",
strictFilters: [{
name: "testname",
value: "testvalue"
}]
};
client.runtime.generateAnswer(kbId, generateAnswerPayload).then((result) => {
console.log("The result is:");
console.log(result);
})
.catch(err => {
}).catch((err) => {
console.log("An error occurred:");
console.error(err);
});
});
</script>
</head>
<body></body>
Expand Down
6 changes: 3 additions & 3 deletions sdk/cognitiveservices/cognitiveservices-qnamaker/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@azure/cognitiveservices-qnamaker",
"author": "Microsoft Corporation",
"description": "QnAMakerClient Library with typescript type definitions for node.js and browser.",
"description": "QnAMakerRuntimeClient Library with typescript type definitions for node.js and browser.",
"version": "3.0.0",
"dependencies": {
"@azure/ms-rest-js": "^2.0.4",
Expand All @@ -16,8 +16,8 @@
],
"license": "MIT",
"main": "./dist/cognitiveservices-qnamaker.js",
"module": "./esm/qnAMakerClient.js",
"types": "./esm/qnAMakerClient.d.ts",
"module": "./esm/qnAMakerRuntimeClient.js",
"types": "./esm/qnAMakerRuntimeClient.d.ts",
"devDependencies": {
"typescript": "^3.5.3",
"rollup": "^1.18.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import sourcemaps from "rollup-plugin-sourcemaps";
* @type {rollup.RollupFileOptions}
*/
const config = {
input: "./esm/qnAMakerClient.js",
input: "./esm/qnAMakerRuntimeClient.js",
external: [
"@azure/ms-rest-js",
"@azure/ms-rest-azure-js"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

export {
ActiveLearningSettingsDTO,
EndpointSettingsDTO,
EndpointSettingsDTOActiveLearning,
ErrorModel,
ErrorResponse,
ErrorResponseError,
InnerErrorModel
} from "../models/mappers";
Loading