@vraksha/bottender-qna-maker 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017-present Yoctol (github.com/Yoctol/bottender)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # @vraksha/bottender-qna-maker
2
+
3
+ [QnA Maker](https://www.qnamaker.ai/) integration for Bottender.
4
+
5
+ ## Installation
6
+
7
+ You can install it with npm:
8
+
9
+ ```sh
10
+ npm install @vraksha/bottender-qna-maker
11
+ ```
12
+
13
+ or Yarn:
14
+
15
+ ```sh
16
+ yarn add @vraksha/bottender-qna-maker
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```js
22
+ const { chain } = require('bottender');
23
+ const qnaMaker = require('@vraksha/bottender-qna-maker');
24
+
25
+ async function Unknown(context) {
26
+ await context.sendText('Sorry, I don’t know what you say.');
27
+ }
28
+
29
+ const QnaMaker = qnaMaker({
30
+ resourceName: 'RESOURCE_NAME',
31
+ knowledgeBaseId: 'KNOWLEDGE_BASE_ID',
32
+ endpointKey: 'ENDPOINT_KEY',
33
+ scoreThreshold: 70,
34
+ });
35
+
36
+ module.exports = async function App() {
37
+ return chain([
38
+ QnaMaker, //
39
+ Unknown,
40
+ ]);
41
+ };
42
+ ```
43
+
44
+ ## Reference
45
+
46
+ ### resourceName
47
+
48
+ Name of Azure Resource.
49
+
50
+ Type: `string`
51
+ Required.
52
+
53
+ ### knowledgeBaseId
54
+
55
+ Knowledgebase id.
56
+
57
+ Type: `string`
58
+ Required.
59
+
60
+ ### endpointKey
61
+
62
+ API key.
63
+
64
+ Type: `string`
65
+ Required.
66
+
67
+ ### isTest
68
+
69
+ Query against the test index.
70
+
71
+ Type: `boolean`
72
+ Optional.
73
+
74
+ ### qnaId
75
+
76
+ Exact qnaId to fetch from the knowledgebase, this field takes priority over question.
77
+
78
+ Type: `string`
79
+ Optional.
80
+
81
+ ### scoreThreshold
82
+
83
+ Threshold for answers returned based on score.
84
+
85
+ Type: `number`
86
+ Optional.
87
+
88
+ ### strictFilters
89
+
90
+ Find only answers that contain these metadata.
91
+
92
+ Type: `MetadataDTO[]`
93
+ Optional.
94
+
95
+ ## License
96
+
97
+ MIT © [Yoctol](https://github.com/Yoctol/bottender)
@@ -0,0 +1,13 @@
1
+ import { Action, Context } from '@vraksha/bottender';
2
+ import { MetadataDTO } from './types';
3
+ declare const _default: ({ resourceName, knowledgeBaseId, endpointKey, isTest, qnaId, scoreThreshold, strictFilters, }: {
4
+ resourceName: string;
5
+ knowledgeBaseId: string;
6
+ endpointKey: string;
7
+ isTest?: string | undefined;
8
+ qnaId?: string | undefined;
9
+ scoreThreshold?: number | undefined;
10
+ strictFilters?: MetadataDTO[] | undefined;
11
+ }) => Action<Context>;
12
+ export = _default;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,EAAE,WAAW,EAAuB,MAAM,SAAS,CAAC;;kBA0B3C,MAAM;qBACH,MAAM;iBACV,MAAM;;;;;MAKjB,OAAO,OAAO,CAAC;AAhBnB,kBAgFE"}
package/dist/index.js ADDED
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ const axios_1 = __importDefault(require("axios"));
15
+ const invariant_1 = __importDefault(require("invariant"));
16
+ const RECOMMENDED_THRESHOLD = 50;
17
+ module.exports = function qnaMaker({ resourceName, knowledgeBaseId, endpointKey, isTest, qnaId, scoreThreshold = RECOMMENDED_THRESHOLD, strictFilters, }) {
18
+ (0, invariant_1.default)(typeof resourceName === 'string' && resourceName.length > 0, 'qna-maker: `resourceName` is a required parameter.');
19
+ (0, invariant_1.default)(typeof knowledgeBaseId === 'string' && knowledgeBaseId.length > 0, 'qna-maker: `knowledgeBaseId` is a required parameter.');
20
+ (0, invariant_1.default)(typeof endpointKey === 'string' && endpointKey.length > 0, 'qna-maker: `endpointKey` is a required parameter.');
21
+ return function QnaMaker(context, { next }) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ if (!context.event.isText || !context.session) {
24
+ return next;
25
+ }
26
+ const { data } = yield axios_1.default.post(`https://${resourceName}.azurewebsites.net/qnamaker/knowledgebases/${knowledgeBaseId}/generateAnswer`, {
27
+ isTest,
28
+ qnaId,
29
+ question: context.event.text,
30
+ scoreThreshold,
31
+ strictFilters,
32
+ top: 1,
33
+ userId: context.session.user ? context.session.user.id : undefined,
34
+ }, {
35
+ headers: {
36
+ Authorization: `EndpointKey ${endpointKey}`,
37
+ },
38
+ });
39
+ const topAnswer = data.answers[0];
40
+ if (!topAnswer || (next && topAnswer.id === -1)) {
41
+ context.setAsNotHandled();
42
+ return next;
43
+ }
44
+ return function TopAnswer() {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ if (topAnswer.id !== -1) {
47
+ context.setIntent(`qna_${topAnswer.id}`);
48
+ context.setAsHandled();
49
+ }
50
+ else {
51
+ context.setAsNotHandled();
52
+ }
53
+ yield context.sendText(topAnswer.answer);
54
+ });
55
+ };
56
+ });
57
+ };
58
+ };
59
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,kDAA0B;AAC1B,0DAAkC;AAOlC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAajC,iBAAS,SAAS,QAAQ,CAAC,EACzB,YAAY,EACZ,eAAe,EACf,WAAW,EACX,MAAM,EACN,KAAK,EACL,cAAc,GAAG,qBAAqB,EACtC,aAAa,GASd;IACC,IAAA,mBAAS,EACP,OAAO,YAAY,KAAK,QAAQ,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,EAC3D,oDAAoD,CACrD,CAAC;IAEF,IAAA,mBAAS,EACP,OAAO,eAAe,KAAK,QAAQ,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EACjE,uDAAuD,CACxD,CAAC;IAEF,IAAA,mBAAS,EACP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QAAQ,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;gBAC3C,CAAC;aAAA,CAAC;QACJ,CAAC;KAAA,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1,40 @@
1
+ export type QnaContext = {
2
+ isContextOnly: boolean;
3
+ prompts: PromptDTO[];
4
+ };
5
+ export type MetadataDTO = {
6
+ name: string;
7
+ value: string;
8
+ };
9
+ export type PromptDTO = {
10
+ displayOrder: number;
11
+ displayText: string;
12
+ qna: Qna;
13
+ qnaId: number;
14
+ };
15
+ export type Qna = {
16
+ answer: string;
17
+ context: QnaContext;
18
+ id: number;
19
+ metadata: MetadataDTO[];
20
+ questions: string[];
21
+ source: string;
22
+ };
23
+ export type QnaSearchResult = Qna & {
24
+ score: number;
25
+ };
26
+ export type QnaSearchResultList = {
27
+ answers: QnaSearchResult[];
28
+ };
29
+ export type QueryDTO = {
30
+ context?: QnaContext;
31
+ isTest?: boolean;
32
+ qnaId?: string;
33
+ question: string;
34
+ rankerType?: string;
35
+ scoreThreshold?: number;
36
+ strictFilters?: MetadataDTO[];
37
+ top?: number;
38
+ userId?: string;
39
+ };
40
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG;IACvB,aAAa,EAAE,OAAO,CAAC;IACvB,OAAO,EAAE,SAAS,EAAE,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,GAAG,CAAC;IACT,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,GAAG,GAAG;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,UAAU,CAAC;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG,GAAG,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtD,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,EAAE,eAAe,EAAE,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB,OAAO,CAAC,EAAE,UAAU,CAAC;IACrB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,WAAW,EAAE,CAAC;IAC9B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@vraksha/bottender-qna-maker",
3
+ "version": "1.7.0",
4
+ "description": "QnA Maker integration for Bottender.",
5
+ "license": "MIT",
6
+ "homepage": "https://bottender.js.org/",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/Yoctol/bottender.git"
10
+ },
11
+ "main": "dist/index.js",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "types": "dist/index.d.ts",
16
+ "dependencies": {
17
+ "axios": "^0.21.4",
18
+ "invariant": "^2.2.4"
19
+ },
20
+ "peerDependencies": {
21
+ "@vraksha/bottender": ">= 1.2.0-0"
22
+ },
23
+ "devDependencies": {
24
+ "@vraksha/bottender": "1.7.0"
25
+ },
26
+ "keywords": [
27
+ "bot",
28
+ "bottender",
29
+ "chatbot",
30
+ "language",
31
+ "messaging",
32
+ "nlu",
33
+ "qna-maker",
34
+ "understanding"
35
+ ],
36
+ "publishConfig": {
37
+ "access": "public"
38
+ }
39
+ }