@botonic/plugin-knowledge-bases 0.33.0-alpha.0 → 0.33.0-alpha.1
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/lib/cjs/hubtype-knowledge-api-service.d.ts +14 -4
- package/lib/cjs/hubtype-knowledge-api-service.js +36 -1
- package/lib/cjs/hubtype-knowledge-api-service.js.map +1 -1
- package/lib/cjs/index.d.ts +6 -2
- package/lib/cjs/index.js +39 -5
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/types.d.ts +1 -1
- package/lib/esm/hubtype-knowledge-api-service.d.ts +14 -4
- package/lib/esm/hubtype-knowledge-api-service.js +36 -1
- package/lib/esm/hubtype-knowledge-api-service.js.map +1 -1
- package/lib/esm/index.d.ts +6 -2
- package/lib/esm/index.js +40 -6
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/types.d.ts +1 -1
- package/package.json +1 -1
- package/src/hubtype-knowledge-api-service.ts +58 -8
- package/src/index.ts +77 -6
- package/src/types.ts +1 -1
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
import { AxiosResponse } from 'axios';
|
|
2
2
|
import { Chunk } from './types';
|
|
3
|
+
export interface HtApiKnowledgeBaseResponse {
|
|
4
|
+
inference_id: string;
|
|
5
|
+
query: string;
|
|
6
|
+
chunks: Chunk[];
|
|
7
|
+
has_knowledge: boolean;
|
|
8
|
+
is_faithful: boolean;
|
|
9
|
+
answer: string;
|
|
10
|
+
}
|
|
3
11
|
export declare class HubtypeApiService {
|
|
4
12
|
private host;
|
|
5
13
|
private timeout;
|
|
6
14
|
constructor(host: string, timeout?: number);
|
|
7
|
-
|
|
15
|
+
inferenceV2(authToken: string, sources: string[], instructions: string, messageId: string, memoryLength: number): Promise<AxiosResponse<HtApiKnowledgeBaseResponse>>;
|
|
16
|
+
testInference(authToken: string, instructions: string, messages: any[], sources: string[]): Promise<AxiosResponse<HtApiKnowledgeBaseResponse>>;
|
|
17
|
+
inferenceV1(authToken: string, question: string, sources: string[]): Promise<AxiosResponse<{
|
|
8
18
|
inference_id: string;
|
|
9
|
-
|
|
10
|
-
|
|
19
|
+
question: string;
|
|
20
|
+
answer: string;
|
|
11
21
|
has_knowledge: boolean;
|
|
12
22
|
is_faithful: boolean;
|
|
13
|
-
|
|
23
|
+
chunk_ids: string[];
|
|
14
24
|
}>>;
|
|
15
25
|
}
|
|
@@ -10,7 +10,7 @@ class HubtypeApiService {
|
|
|
10
10
|
this.host = host;
|
|
11
11
|
this.timeout = timeout || DEFAULT_TIMEOUT;
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
inferenceV2(authToken, sources, instructions, messageId, memoryLength) {
|
|
14
14
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
15
15
|
return yield (0, axios_1.default)({
|
|
16
16
|
method: 'POST',
|
|
@@ -29,6 +29,41 @@ class HubtypeApiService {
|
|
|
29
29
|
});
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
|
+
testInference(authToken, instructions, messages, sources) {
|
|
33
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
return yield (0, axios_1.default)({
|
|
35
|
+
method: 'POST',
|
|
36
|
+
url: `${this.host}/external/v1/ai/knowledge_base/test/`,
|
|
37
|
+
headers: {
|
|
38
|
+
Authorization: `Bearer ${authToken}`,
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
},
|
|
41
|
+
data: {
|
|
42
|
+
sources,
|
|
43
|
+
instructions,
|
|
44
|
+
messages,
|
|
45
|
+
},
|
|
46
|
+
timeout: this.timeout,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
inferenceV1(authToken, question, sources) {
|
|
51
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
return yield (0, axios_1.default)({
|
|
53
|
+
method: 'POST',
|
|
54
|
+
url: `${this.host}/external/v1/ai/knowledge_base/inference/`,
|
|
55
|
+
headers: {
|
|
56
|
+
Authorization: `Bearer ${authToken}`,
|
|
57
|
+
'Content-Type': 'application/json',
|
|
58
|
+
},
|
|
59
|
+
data: {
|
|
60
|
+
question,
|
|
61
|
+
sources,
|
|
62
|
+
},
|
|
63
|
+
timeout: this.timeout,
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
32
67
|
}
|
|
33
68
|
exports.HubtypeApiService = HubtypeApiService;
|
|
34
69
|
//# sourceMappingURL=hubtype-knowledge-api-service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hubtype-knowledge-api-service.js","sourceRoot":"","sources":["../../src/hubtype-knowledge-api-service.ts"],"names":[],"mappings":";;;;AAAA,yDAAyD;AACzD,0DAA4C;AAI5C,MAAM,eAAe,GAAG,KAAK,CAAA;
|
|
1
|
+
{"version":3,"file":"hubtype-knowledge-api-service.js","sourceRoot":"","sources":["../../src/hubtype-knowledge-api-service.ts"],"names":[],"mappings":";;;;AAAA,yDAAyD;AACzD,0DAA4C;AAI5C,MAAM,eAAe,GAAG,KAAK,CAAA;AAU7B,MAAa,iBAAiB;IAI5B,YAAY,IAAY,EAAE,OAAgB;QACxC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,eAAe,CAAA;IAC3C,CAAC;IAEK,WAAW,CACf,SAAiB,EACjB,OAAiB,EACjB,YAAoB,EACpB,SAAiB,EACjB,YAAoB;;YAEpB,OAAO,MAAM,IAAA,eAAK,EAAC;gBACjB,MAAM,EAAE,MAAM;gBACd,GAAG,EAAE,GAAG,IAAI,CAAC,IAAI,2CAA2C;gBAC5D,OAAO,EAAE;oBACP,aAAa,EAAE,UAAU,SAAS,EAAE;oBACpC,cAAc,EAAE,kBAAkB;iBACnC;gBACD,IAAI,EAAE;oBACJ,OAAO;oBACP,YAAY;oBACZ,OAAO,EAAE,SAAS;oBAClB,aAAa,EAAE,YAAY;iBAC5B;gBACD,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAA;QACJ,CAAC;KAAA;IAEK,aAAa,CACjB,SAAiB,EACjB,YAAoB,EACpB,QAAe,EACf,OAAiB;;YAEjB,OAAO,MAAM,IAAA,eAAK,EAAC;gBACjB,MAAM,EAAE,MAAM;gBACd,GAAG,EAAE,GAAG,IAAI,CAAC,IAAI,sCAAsC;gBACvD,OAAO,EAAE;oBACP,aAAa,EAAE,UAAU,SAAS,EAAE;oBACpC,cAAc,EAAE,kBAAkB;iBACnC;gBACD,IAAI,EAAE;oBACJ,OAAO;oBACP,YAAY;oBACZ,QAAQ;iBACT;gBACD,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAA;QACJ,CAAC;KAAA;IAEK,WAAW,CACf,SAAiB,EACjB,QAAgB,EAChB,OAAiB;;YAWjB,OAAO,MAAM,IAAA,eAAK,EAAC;gBACjB,MAAM,EAAE,MAAM;gBACd,GAAG,EAAE,GAAG,IAAI,CAAC,IAAI,2CAA2C;gBAC5D,OAAO,EAAE;oBACP,aAAa,EAAE,UAAU,SAAS,EAAE;oBACpC,cAAc,EAAE,kBAAkB;iBACnC;gBACD,IAAI,EAAE;oBACJ,QAAQ;oBACR,OAAO;iBACR;gBACD,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAA;QACJ,CAAC;KAAA;CACF;AAnFD,8CAmFC"}
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import type { Plugin, PluginPreRequest
|
|
1
|
+
import type { BotRequest, Plugin, PluginPreRequest } from '@botonic/core';
|
|
2
2
|
import { KnowledgeBaseResponse, PluginKnowledgeBaseOptions } from './types';
|
|
3
3
|
export default class BotonicPluginKnowledgeBases implements Plugin {
|
|
4
4
|
private readonly apiService;
|
|
5
5
|
private readonly authToken;
|
|
6
6
|
constructor(options: PluginKnowledgeBaseOptions);
|
|
7
7
|
pre(_request: PluginPreRequest): Promise<void>;
|
|
8
|
-
getInference(
|
|
8
|
+
getInference(request: BotRequest, sources: string[], instructions: string, messageId: string, memoryLength: number): Promise<KnowledgeBaseResponse>;
|
|
9
|
+
getTestInference(request: BotRequest, instructions: string, sources: string[]): Promise<KnowledgeBaseResponse>;
|
|
10
|
+
getInferenceV1(authToken: string, request: BotRequest, sources: string[]): Promise<KnowledgeBaseResponse>;
|
|
11
|
+
getInferenceV2(authToken: string, sources: string[], instructions: string, messageId: string, memoryLength: number): Promise<KnowledgeBaseResponse>;
|
|
12
|
+
private mapApiResponse;
|
|
9
13
|
}
|
|
10
14
|
export { PluginKnowledgeBaseOptions } from './types';
|
package/lib/cjs/index.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const hubtype_knowledge_api_service_1 = require("./hubtype-knowledge-api-service");
|
|
5
5
|
const isProd = process.env.NODE_ENV === 'production';
|
|
6
|
+
const isDev = process.env.NODE_ENV === 'development';
|
|
6
7
|
class BotonicPluginKnowledgeBases {
|
|
7
8
|
constructor(options) {
|
|
8
9
|
this.apiService = new hubtype_knowledge_api_service_1.HubtypeApiService(options.host, options.timeout);
|
|
@@ -13,19 +14,52 @@ class BotonicPluginKnowledgeBases {
|
|
|
13
14
|
return;
|
|
14
15
|
});
|
|
15
16
|
}
|
|
16
|
-
getInference(
|
|
17
|
+
getInference(request, sources, instructions, messageId, memoryLength) {
|
|
17
18
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
18
|
-
const authToken = isProd ? session._access_token : this.authToken;
|
|
19
|
-
|
|
19
|
+
const authToken = isProd ? request.session._access_token : this.authToken;
|
|
20
|
+
if (isDev) {
|
|
21
|
+
return this.getTestInference(request, instructions, sources);
|
|
22
|
+
}
|
|
23
|
+
if (!instructions) {
|
|
24
|
+
return this.getInferenceV1(authToken, request, sources);
|
|
25
|
+
}
|
|
26
|
+
return this.getInferenceV2(authToken, sources, instructions, messageId, memoryLength);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
getTestInference(request, instructions, sources) {
|
|
30
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
const messages = [{ role: 'human', content: request.input.data }];
|
|
32
|
+
const response = yield this.apiService.testInference(request.session._access_token, instructions, messages, sources);
|
|
33
|
+
return this.mapApiResponse(response);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
getInferenceV1(authToken, request, sources) {
|
|
37
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const response = yield this.apiService.inferenceV1(authToken, request.input.data, sources);
|
|
20
39
|
return {
|
|
21
40
|
inferenceId: response.data.inference_id,
|
|
22
|
-
|
|
41
|
+
answer: response.data.answer,
|
|
23
42
|
hasKnowledge: response.data.has_knowledge,
|
|
24
43
|
isFaithful: response.data.is_faithful,
|
|
25
|
-
|
|
44
|
+
chunkIds: response.data.chunk_ids,
|
|
26
45
|
};
|
|
27
46
|
});
|
|
28
47
|
}
|
|
48
|
+
getInferenceV2(authToken, sources, instructions, messageId, memoryLength) {
|
|
49
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
const response = yield this.apiService.inferenceV2(authToken, sources, instructions, messageId, memoryLength);
|
|
51
|
+
return this.mapApiResponse(response);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
mapApiResponse(response) {
|
|
55
|
+
return {
|
|
56
|
+
inferenceId: response.data.inference_id,
|
|
57
|
+
chunkIds: response.data.chunks.map(chunk => chunk.id),
|
|
58
|
+
hasKnowledge: response.data.has_knowledge,
|
|
59
|
+
isFaithful: response.data.is_faithful,
|
|
60
|
+
answer: response.data.answer,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
29
63
|
}
|
|
30
64
|
exports.default = BotonicPluginKnowledgeBases;
|
|
31
65
|
//# sourceMappingURL=index.js.map
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAGA,mFAGwC;AAGxC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAA;AACpD,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAA;AAEpD,MAAqB,2BAA2B;IAI9C,YAAY,OAAmC;QAC7C,IAAI,CAAC,UAAU,GAAG,IAAI,iDAAiB,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;QACtE,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAA;IAC1C,CAAC;IAEK,GAAG,CAAC,QAA0B;;YAClC,OAAM;QACR,CAAC;KAAA;IAEK,YAAY,CAChB,OAAmB,EACnB,OAAiB,EACjB,YAAoB,EACpB,SAAiB,EACjB,YAAoB;;YAEpB,MAAM,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAA;YAEzE,IAAI,KAAK,EAAE;gBACT,OAAO,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,YAAY,EAAE,OAAO,CAAC,CAAA;aAC7D;YAED,IAAI,CAAC,YAAY,EAAE;gBACjB,OAAO,IAAI,CAAC,cAAc,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;aACxD;YAED,OAAO,IAAI,CAAC,cAAc,CACxB,SAAS,EACT,OAAO,EACP,YAAY,EACZ,SAAS,EACT,YAAY,CACb,CAAA;QACH,CAAC;KAAA;IAEK,gBAAgB,CACpB,OAAmB,EACnB,YAAoB,EACpB,OAAiB;;YAEjB,MAAM,QAAQ,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,EAA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,EAAE,CAAC;YACrD,YAAY,EAAE,QAAQ,CAAC,IAAI,CAAC,aAAa;YACzC,UAAU,EAAE,QAAQ,CAAC,IAAI,CAAC,WAAW;YACrC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM;SAC7B,CAAA;IACH,CAAC;CACF;AAxGD,8CAwGC"}
|
package/lib/cjs/types.d.ts
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
import { AxiosResponse } from 'axios';
|
|
2
2
|
import { Chunk } from './types';
|
|
3
|
+
export interface HtApiKnowledgeBaseResponse {
|
|
4
|
+
inference_id: string;
|
|
5
|
+
query: string;
|
|
6
|
+
chunks: Chunk[];
|
|
7
|
+
has_knowledge: boolean;
|
|
8
|
+
is_faithful: boolean;
|
|
9
|
+
answer: string;
|
|
10
|
+
}
|
|
3
11
|
export declare class HubtypeApiService {
|
|
4
12
|
private host;
|
|
5
13
|
private timeout;
|
|
6
14
|
constructor(host: string, timeout?: number);
|
|
7
|
-
|
|
15
|
+
inferenceV2(authToken: string, sources: string[], instructions: string, messageId: string, memoryLength: number): Promise<AxiosResponse<HtApiKnowledgeBaseResponse>>;
|
|
16
|
+
testInference(authToken: string, instructions: string, messages: any[], sources: string[]): Promise<AxiosResponse<HtApiKnowledgeBaseResponse>>;
|
|
17
|
+
inferenceV1(authToken: string, question: string, sources: string[]): Promise<AxiosResponse<{
|
|
8
18
|
inference_id: string;
|
|
9
|
-
|
|
10
|
-
|
|
19
|
+
question: string;
|
|
20
|
+
answer: string;
|
|
11
21
|
has_knowledge: boolean;
|
|
12
22
|
is_faithful: boolean;
|
|
13
|
-
|
|
23
|
+
chunk_ids: string[];
|
|
14
24
|
}>>;
|
|
15
25
|
}
|
|
@@ -7,7 +7,7 @@ export class HubtypeApiService {
|
|
|
7
7
|
this.host = host;
|
|
8
8
|
this.timeout = timeout || DEFAULT_TIMEOUT;
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
inferenceV2(authToken, sources, instructions, messageId, memoryLength) {
|
|
11
11
|
return __awaiter(this, void 0, void 0, function* () {
|
|
12
12
|
return yield axios({
|
|
13
13
|
method: 'POST',
|
|
@@ -26,5 +26,40 @@ export class HubtypeApiService {
|
|
|
26
26
|
});
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
+
testInference(authToken, instructions, messages, sources) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
return yield axios({
|
|
32
|
+
method: 'POST',
|
|
33
|
+
url: `${this.host}/external/v1/ai/knowledge_base/test/`,
|
|
34
|
+
headers: {
|
|
35
|
+
Authorization: `Bearer ${authToken}`,
|
|
36
|
+
'Content-Type': 'application/json',
|
|
37
|
+
},
|
|
38
|
+
data: {
|
|
39
|
+
sources,
|
|
40
|
+
instructions,
|
|
41
|
+
messages,
|
|
42
|
+
},
|
|
43
|
+
timeout: this.timeout,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
inferenceV1(authToken, question, sources) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
return yield axios({
|
|
50
|
+
method: 'POST',
|
|
51
|
+
url: `${this.host}/external/v1/ai/knowledge_base/inference/`,
|
|
52
|
+
headers: {
|
|
53
|
+
Authorization: `Bearer ${authToken}`,
|
|
54
|
+
'Content-Type': 'application/json',
|
|
55
|
+
},
|
|
56
|
+
data: {
|
|
57
|
+
question,
|
|
58
|
+
sources,
|
|
59
|
+
},
|
|
60
|
+
timeout: this.timeout,
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
29
64
|
}
|
|
30
65
|
//# sourceMappingURL=hubtype-knowledge-api-service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hubtype-knowledge-api-service.js","sourceRoot":"","sources":["../../src/hubtype-knowledge-api-service.ts"],"names":[],"mappings":";AAAA,yDAAyD;AACzD,OAAO,KAAwB,MAAM,OAAO,CAAA;AAI5C,MAAM,eAAe,GAAG,KAAK,CAAA;
|
|
1
|
+
{"version":3,"file":"hubtype-knowledge-api-service.js","sourceRoot":"","sources":["../../src/hubtype-knowledge-api-service.ts"],"names":[],"mappings":";AAAA,yDAAyD;AACzD,OAAO,KAAwB,MAAM,OAAO,CAAA;AAI5C,MAAM,eAAe,GAAG,KAAK,CAAA;AAU7B,MAAM,OAAO,iBAAiB;IAI5B,YAAY,IAAY,EAAE,OAAgB;QACxC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,eAAe,CAAA;IAC3C,CAAC;IAEK,WAAW,CACf,SAAiB,EACjB,OAAiB,EACjB,YAAoB,EACpB,SAAiB,EACjB,YAAoB;;YAEpB,OAAO,MAAM,KAAK,CAAC;gBACjB,MAAM,EAAE,MAAM;gBACd,GAAG,EAAE,GAAG,IAAI,CAAC,IAAI,2CAA2C;gBAC5D,OAAO,EAAE;oBACP,aAAa,EAAE,UAAU,SAAS,EAAE;oBACpC,cAAc,EAAE,kBAAkB;iBACnC;gBACD,IAAI,EAAE;oBACJ,OAAO;oBACP,YAAY;oBACZ,OAAO,EAAE,SAAS;oBAClB,aAAa,EAAE,YAAY;iBAC5B;gBACD,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAA;QACJ,CAAC;KAAA;IAEK,aAAa,CACjB,SAAiB,EACjB,YAAoB,EACpB,QAAe,EACf,OAAiB;;YAEjB,OAAO,MAAM,KAAK,CAAC;gBACjB,MAAM,EAAE,MAAM;gBACd,GAAG,EAAE,GAAG,IAAI,CAAC,IAAI,sCAAsC;gBACvD,OAAO,EAAE;oBACP,aAAa,EAAE,UAAU,SAAS,EAAE;oBACpC,cAAc,EAAE,kBAAkB;iBACnC;gBACD,IAAI,EAAE;oBACJ,OAAO;oBACP,YAAY;oBACZ,QAAQ;iBACT;gBACD,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAA;QACJ,CAAC;KAAA;IAEK,WAAW,CACf,SAAiB,EACjB,QAAgB,EAChB,OAAiB;;YAWjB,OAAO,MAAM,KAAK,CAAC;gBACjB,MAAM,EAAE,MAAM;gBACd,GAAG,EAAE,GAAG,IAAI,CAAC,IAAI,2CAA2C;gBAC5D,OAAO,EAAE;oBACP,aAAa,EAAE,UAAU,SAAS,EAAE;oBACpC,cAAc,EAAE,kBAAkB;iBACnC;gBACD,IAAI,EAAE;oBACJ,QAAQ;oBACR,OAAO;iBACR;gBACD,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAA;QACJ,CAAC;KAAA;CACF"}
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import type { Plugin, PluginPreRequest
|
|
1
|
+
import type { BotRequest, Plugin, PluginPreRequest } from '@botonic/core';
|
|
2
2
|
import { KnowledgeBaseResponse, PluginKnowledgeBaseOptions } from './types';
|
|
3
3
|
export default class BotonicPluginKnowledgeBases implements Plugin {
|
|
4
4
|
private readonly apiService;
|
|
5
5
|
private readonly authToken;
|
|
6
6
|
constructor(options: PluginKnowledgeBaseOptions);
|
|
7
7
|
pre(_request: PluginPreRequest): Promise<void>;
|
|
8
|
-
getInference(
|
|
8
|
+
getInference(request: BotRequest, sources: string[], instructions: string, messageId: string, memoryLength: number): Promise<KnowledgeBaseResponse>;
|
|
9
|
+
getTestInference(request: BotRequest, instructions: string, sources: string[]): Promise<KnowledgeBaseResponse>;
|
|
10
|
+
getInferenceV1(authToken: string, request: BotRequest, sources: string[]): Promise<KnowledgeBaseResponse>;
|
|
11
|
+
getInferenceV2(authToken: string, sources: string[], instructions: string, messageId: string, memoryLength: number): Promise<KnowledgeBaseResponse>;
|
|
12
|
+
private mapApiResponse;
|
|
9
13
|
}
|
|
10
14
|
export { PluginKnowledgeBaseOptions } from './types';
|
package/lib/esm/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { __awaiter } from "tslib";
|
|
2
|
-
import { HubtypeApiService } from './hubtype-knowledge-api-service';
|
|
2
|
+
import { HubtypeApiService, } from './hubtype-knowledge-api-service';
|
|
3
3
|
const isProd = process.env.NODE_ENV === 'production';
|
|
4
|
+
const isDev = process.env.NODE_ENV === 'development';
|
|
4
5
|
export default class BotonicPluginKnowledgeBases {
|
|
5
6
|
constructor(options) {
|
|
6
7
|
this.apiService = new HubtypeApiService(options.host, options.timeout);
|
|
@@ -11,18 +12,51 @@ export default class BotonicPluginKnowledgeBases {
|
|
|
11
12
|
return;
|
|
12
13
|
});
|
|
13
14
|
}
|
|
14
|
-
getInference(
|
|
15
|
+
getInference(request, sources, instructions, messageId, memoryLength) {
|
|
15
16
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
const authToken = isProd ? session._access_token : this.authToken;
|
|
17
|
-
|
|
17
|
+
const authToken = isProd ? request.session._access_token : this.authToken;
|
|
18
|
+
if (isDev) {
|
|
19
|
+
return this.getTestInference(request, instructions, sources);
|
|
20
|
+
}
|
|
21
|
+
if (!instructions) {
|
|
22
|
+
return this.getInferenceV1(authToken, request, sources);
|
|
23
|
+
}
|
|
24
|
+
return this.getInferenceV2(authToken, sources, instructions, messageId, memoryLength);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
getTestInference(request, instructions, sources) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
const messages = [{ role: 'human', content: request.input.data }];
|
|
30
|
+
const response = yield this.apiService.testInference(request.session._access_token, instructions, messages, sources);
|
|
31
|
+
return this.mapApiResponse(response);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
getInferenceV1(authToken, request, sources) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const response = yield this.apiService.inferenceV1(authToken, request.input.data, sources);
|
|
18
37
|
return {
|
|
19
38
|
inferenceId: response.data.inference_id,
|
|
20
|
-
|
|
39
|
+
answer: response.data.answer,
|
|
21
40
|
hasKnowledge: response.data.has_knowledge,
|
|
22
41
|
isFaithful: response.data.is_faithful,
|
|
23
|
-
|
|
42
|
+
chunkIds: response.data.chunk_ids,
|
|
24
43
|
};
|
|
25
44
|
});
|
|
26
45
|
}
|
|
46
|
+
getInferenceV2(authToken, sources, instructions, messageId, memoryLength) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
const response = yield this.apiService.inferenceV2(authToken, sources, instructions, messageId, memoryLength);
|
|
49
|
+
return this.mapApiResponse(response);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
mapApiResponse(response) {
|
|
53
|
+
return {
|
|
54
|
+
inferenceId: response.data.inference_id,
|
|
55
|
+
chunkIds: response.data.chunks.map(chunk => chunk.id),
|
|
56
|
+
hasKnowledge: response.data.has_knowledge,
|
|
57
|
+
isFaithful: response.data.is_faithful,
|
|
58
|
+
answer: response.data.answer,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
27
61
|
}
|
|
28
62
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAGA,OAAO,EAEL,iBAAiB,GAClB,MAAM,iCAAiC,CAAA;AAGxC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAA;AACpD,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAA;AAEpD,MAAM,CAAC,OAAO,OAAO,2BAA2B;IAI9C,YAAY,OAAmC;QAC7C,IAAI,CAAC,UAAU,GAAG,IAAI,iBAAiB,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;QACtE,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAA;IAC1C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I,CAAC,YAAY;gBACvC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM;gBAC5B,YAAY,EAAE,QAAQ,CAAC,IAAI,CAAC,aAAa;gBACzC,UAAU,EAAE,QAAQ,CAAC,IAAI,CAAC,WAAW;gBACrC,QAAQ,EAAE,QAAQ,CAAC,IAAI,CAAC,SAAS;aAClC,CAAA;QACH,CAAC;KAAA;IAEK,cAAc,CAClB,SAAiB,EACjB,OAAiB,EACjB,YAAoB,EACpB,SAAiB,EACjB,YAAoB;;YAEpB,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAChD,SAAS,EACT,OAAO,EACP,YAAY,EACZ,SAAS,EACT,YAAY,CACb,CAAA;YAED,OAAO,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAA;QACtC,CAAC;KAAA;IAEO,cAAc,CACpB,QAAmD;QAEnD,OAAO;YACL,WAAW,EAAE,QAAQ,CAAC,IAAI,CAAC,YAAY;YACvC,QAAQ,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC;YACrD,YAAY,EAAE,QAAQ,CAAC,IAAI,CAAC,aAAa;YACzC,UAAU,EAAE,QAAQ,CAAC,IAAI,CAAC,WAAW;YACrC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM;SAC7B,CAAA;IACH,CAAC;CACF"}
|
package/lib/esm/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -5,6 +5,14 @@ import { Chunk } from './types'
|
|
|
5
5
|
|
|
6
6
|
const DEFAULT_TIMEOUT = 10000
|
|
7
7
|
|
|
8
|
+
export interface HtApiKnowledgeBaseResponse {
|
|
9
|
+
inference_id: string
|
|
10
|
+
query: string
|
|
11
|
+
chunks: Chunk[]
|
|
12
|
+
has_knowledge: boolean
|
|
13
|
+
is_faithful: boolean
|
|
14
|
+
answer: string
|
|
15
|
+
}
|
|
8
16
|
export class HubtypeApiService {
|
|
9
17
|
private host: string
|
|
10
18
|
private timeout: number
|
|
@@ -14,34 +22,76 @@ export class HubtypeApiService {
|
|
|
14
22
|
this.timeout = timeout || DEFAULT_TIMEOUT
|
|
15
23
|
}
|
|
16
24
|
|
|
17
|
-
async
|
|
25
|
+
async inferenceV2(
|
|
18
26
|
authToken: string,
|
|
19
27
|
sources: string[],
|
|
20
28
|
instructions: string,
|
|
21
29
|
messageId: string,
|
|
22
30
|
memoryLength: number
|
|
31
|
+
): Promise<AxiosResponse<HtApiKnowledgeBaseResponse>> {
|
|
32
|
+
return await axios({
|
|
33
|
+
method: 'POST',
|
|
34
|
+
url: `${this.host}/external/v2/ai/knowledge_base/inference/`,
|
|
35
|
+
headers: {
|
|
36
|
+
Authorization: `Bearer ${authToken}`,
|
|
37
|
+
'Content-Type': 'application/json',
|
|
38
|
+
},
|
|
39
|
+
data: {
|
|
40
|
+
sources,
|
|
41
|
+
instructions,
|
|
42
|
+
message: messageId,
|
|
43
|
+
memory_length: memoryLength,
|
|
44
|
+
},
|
|
45
|
+
timeout: this.timeout,
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async testInference(
|
|
50
|
+
authToken: string,
|
|
51
|
+
instructions: string,
|
|
52
|
+
messages: any[],
|
|
53
|
+
sources: string[]
|
|
54
|
+
): Promise<AxiosResponse<HtApiKnowledgeBaseResponse>> {
|
|
55
|
+
return await axios({
|
|
56
|
+
method: 'POST',
|
|
57
|
+
url: `${this.host}/external/v1/ai/knowledge_base/test/`,
|
|
58
|
+
headers: {
|
|
59
|
+
Authorization: `Bearer ${authToken}`,
|
|
60
|
+
'Content-Type': 'application/json',
|
|
61
|
+
},
|
|
62
|
+
data: {
|
|
63
|
+
sources,
|
|
64
|
+
instructions,
|
|
65
|
+
messages,
|
|
66
|
+
},
|
|
67
|
+
timeout: this.timeout,
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async inferenceV1(
|
|
72
|
+
authToken: string,
|
|
73
|
+
question: string,
|
|
74
|
+
sources: string[]
|
|
23
75
|
): Promise<
|
|
24
76
|
AxiosResponse<{
|
|
25
77
|
inference_id: string
|
|
26
|
-
|
|
27
|
-
|
|
78
|
+
question: string
|
|
79
|
+
answer: string
|
|
28
80
|
has_knowledge: boolean
|
|
29
81
|
is_faithful: boolean
|
|
30
|
-
|
|
82
|
+
chunk_ids: string[]
|
|
31
83
|
}>
|
|
32
84
|
> {
|
|
33
85
|
return await axios({
|
|
34
86
|
method: 'POST',
|
|
35
|
-
url: `${this.host}/external/
|
|
87
|
+
url: `${this.host}/external/v1/ai/knowledge_base/inference/`,
|
|
36
88
|
headers: {
|
|
37
89
|
Authorization: `Bearer ${authToken}`,
|
|
38
90
|
'Content-Type': 'application/json',
|
|
39
91
|
},
|
|
40
92
|
data: {
|
|
93
|
+
question,
|
|
41
94
|
sources,
|
|
42
|
-
instructions,
|
|
43
|
-
message: messageId,
|
|
44
|
-
memory_length: memoryLength,
|
|
45
95
|
},
|
|
46
96
|
timeout: this.timeout,
|
|
47
97
|
})
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import type { Plugin, PluginPreRequest
|
|
1
|
+
import type { BotRequest, Plugin, PluginPreRequest } from '@botonic/core'
|
|
2
|
+
import { AxiosResponse } from 'axios'
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
HtApiKnowledgeBaseResponse,
|
|
6
|
+
HubtypeApiService,
|
|
7
|
+
} from './hubtype-knowledge-api-service'
|
|
4
8
|
import { KnowledgeBaseResponse, PluginKnowledgeBaseOptions } from './types'
|
|
5
9
|
|
|
6
10
|
const isProd = process.env.NODE_ENV === 'production'
|
|
11
|
+
const isDev = process.env.NODE_ENV === 'development'
|
|
7
12
|
|
|
8
13
|
export default class BotonicPluginKnowledgeBases implements Plugin {
|
|
9
14
|
private readonly apiService: HubtypeApiService
|
|
@@ -19,15 +24,75 @@ export default class BotonicPluginKnowledgeBases implements Plugin {
|
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
async getInference(
|
|
22
|
-
|
|
27
|
+
request: BotRequest,
|
|
23
28
|
sources: string[],
|
|
24
29
|
instructions: string,
|
|
25
30
|
messageId: string,
|
|
26
31
|
memoryLength: number
|
|
27
32
|
): Promise<KnowledgeBaseResponse> {
|
|
28
|
-
const authToken = isProd ? session._access_token : this.authToken
|
|
33
|
+
const authToken = isProd ? request.session._access_token : this.authToken
|
|
29
34
|
|
|
30
|
-
|
|
35
|
+
if (isDev) {
|
|
36
|
+
return this.getTestInference(request, instructions, sources)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!instructions) {
|
|
40
|
+
return this.getInferenceV1(authToken, request, sources)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return this.getInferenceV2(
|
|
44
|
+
authToken,
|
|
45
|
+
sources,
|
|
46
|
+
instructions,
|
|
47
|
+
messageId,
|
|
48
|
+
memoryLength
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getTestInference(
|
|
53
|
+
request: BotRequest,
|
|
54
|
+
instructions: string,
|
|
55
|
+
sources: string[]
|
|
56
|
+
): Promise<KnowledgeBaseResponse> {
|
|
57
|
+
const messages = [{ role: 'human', content: request.input.data }]
|
|
58
|
+
|
|
59
|
+
const response = await this.apiService.testInference(
|
|
60
|
+
request.session._access_token,
|
|
61
|
+
instructions,
|
|
62
|
+
messages,
|
|
63
|
+
sources
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
return this.mapApiResponse(response)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async getInferenceV1(
|
|
70
|
+
authToken: string,
|
|
71
|
+
request: BotRequest,
|
|
72
|
+
sources: string[]
|
|
73
|
+
): Promise<KnowledgeBaseResponse> {
|
|
74
|
+
const response = await this.apiService.inferenceV1(
|
|
75
|
+
authToken,
|
|
76
|
+
request.input.data!,
|
|
77
|
+
sources
|
|
78
|
+
)
|
|
79
|
+
return {
|
|
80
|
+
inferenceId: response.data.inference_id,
|
|
81
|
+
answer: response.data.answer,
|
|
82
|
+
hasKnowledge: response.data.has_knowledge,
|
|
83
|
+
isFaithful: response.data.is_faithful,
|
|
84
|
+
chunkIds: response.data.chunk_ids,
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async getInferenceV2(
|
|
89
|
+
authToken: string,
|
|
90
|
+
sources: string[],
|
|
91
|
+
instructions: string,
|
|
92
|
+
messageId: string,
|
|
93
|
+
memoryLength: number
|
|
94
|
+
): Promise<KnowledgeBaseResponse> {
|
|
95
|
+
const response = await this.apiService.inferenceV2(
|
|
31
96
|
authToken,
|
|
32
97
|
sources,
|
|
33
98
|
instructions,
|
|
@@ -35,9 +100,15 @@ export default class BotonicPluginKnowledgeBases implements Plugin {
|
|
|
35
100
|
memoryLength
|
|
36
101
|
)
|
|
37
102
|
|
|
103
|
+
return this.mapApiResponse(response)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private mapApiResponse(
|
|
107
|
+
response: AxiosResponse<HtApiKnowledgeBaseResponse>
|
|
108
|
+
): KnowledgeBaseResponse {
|
|
38
109
|
return {
|
|
39
110
|
inferenceId: response.data.inference_id,
|
|
40
|
-
|
|
111
|
+
chunkIds: response.data.chunks.map(chunk => chunk.id),
|
|
41
112
|
hasKnowledge: response.data.has_knowledge,
|
|
42
113
|
isFaithful: response.data.is_faithful,
|
|
43
114
|
answer: response.data.answer,
|