UNPKG

@botonic/plugin-knowledge-bases

Version:

Use a Hubtype to make the bot respond through a knowledge base.

65 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const hubtype_knowledge_api_service_1 = require("./hubtype-knowledge-api-service"); const isProd = process.env.NODE_ENV === 'production'; const isDev = process.env.NODE_ENV === 'development'; class BotonicPluginKnowledgeBases { constructor(options) { this.apiService = new hubtype_knowledge_api_service_1.HubtypeApiService(options.host, options.timeout); this.authToken = options.authToken || ''; } pre(_request) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return; }); } getInference(request, sources, instructions, messageId, memoryLength) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const authToken = isProd ? request.session._access_token : this.authToken; if (isDev) { return this.getTestInference(authToken, request, instructions, sources); } if (!instructions) { return this.getInferenceV1(authToken, request, sources); } return this.getInferenceV2(authToken, sources, instructions, messageId, memoryLength); }); } getTestInference(authToken, request, instructions, sources) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const messages = [{ role: 'human', content: request.input.data }]; const response = yield this.apiService.testInference(authToken, instructions, messages, sources); return this.mapApiResponse(response); }); } getInferenceV1(authToken, request, sources) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield this.apiService.inferenceV1(authToken, request.input.data, sources); return { inferenceId: response.data.inference_id, answer: response.data.answer, hasKnowledge: response.data.has_knowledge, isFaithful: response.data.is_faithful, chunkIds: response.data.chunk_ids, }; }); } getInferenceV2(authToken, sources, instructions, messageId, memoryLength) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield this.apiService.inferenceV2(authToken, sources, instructions, messageId, memoryLength); return this.mapApiResponse(response); }); } mapApiResponse(response) { return { inferenceId: response.data.inference_id, chunkIds: response.data.chunks.map(chunk => chunk.id), hasKnowledge: response.data.has_knowledge, isFaithful: response.data.is_faithful, answer: response.data.answer, }; } } exports.default = BotonicPluginKnowledgeBases; //# sourceMappingURL=index.js.map