@botonic/plugin-knowledge-bases
Version:
Use a Hubtype to make the bot respond through a knowledge base.
62 lines • 2.76 kB
JavaScript
import { __awaiter } from "tslib";
import { HubtypeApiService, } from './hubtype-knowledge-api-service';
const isProd = process.env.NODE_ENV === 'production';
const isDev = process.env.NODE_ENV === 'development';
export default class BotonicPluginKnowledgeBases {
constructor(options) {
this.apiService = new HubtypeApiService(options.host, options.timeout);
this.authToken = options.authToken || '';
}
pre(_request) {
return __awaiter(this, void 0, void 0, function* () {
return;
});
}
getInference(request, sources, instructions, messageId, memoryLength) {
return __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 __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 __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 __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,
};
}
}
//# sourceMappingURL=index.js.map