UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

87 lines 4.26 kB
import { __awaiter } from "tslib"; import { DISABLED_MEMORY_LENGTH, FlowKnowledgeBase, } from '../content-fields'; import { EventAction, KnowledgebaseFailReason, trackEvent } from '../tracking'; import { inputHasTextData, isKnowledgeBasesAllowed } from '../utils'; export function getContentsByKnowledgeBase({ cmsApi, flowBuilderPlugin, request, }) { return __awaiter(this, void 0, void 0, function* () { if (isKnowledgeBasesAllowed(request)) { const startNodeKnowledgeBaseFlow = cmsApi.getStartNodeKnowledgeBaseFlow(); const isKnowledgeBaseEnabled = cmsApi.isKnowledgeBaseEnabled(); if (!startNodeKnowledgeBaseFlow || !isKnowledgeBaseEnabled) { return []; } const contents = yield flowBuilderPlugin.getContentsByNode(startNodeKnowledgeBaseFlow); const knowledgeBaseContent = contents.find(content => content instanceof FlowKnowledgeBase); if (!knowledgeBaseContent) { return contents; } const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id); const flowId = cmsApi.getNodeById(knowledgeBaseContent.id).flow_id; if (flowBuilderPlugin.getKnowledgeBaseResponse && inputHasTextData(request.input) && sourceIds.length > 0) { const contentsWithKnowledgeResponse = yield getContentsWithKnowledgeResponse(flowBuilderPlugin.getKnowledgeBaseResponse, request, contents, knowledgeBaseContent, flowId); if (contentsWithKnowledgeResponse) { return contentsWithKnowledgeResponse; } } } return []; }); } function getContentsWithKnowledgeResponse(getKnowledgeBaseResponse, request, contents, knowledgeBaseContent, flowId) { return __awaiter(this, void 0, void 0, function* () { const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id); const instructions = knowledgeBaseContent.instructions; const messageId = request.input.message_id; const memoryLength = knowledgeBaseContent.hasMemory ? knowledgeBaseContent.memoryLength : DISABLED_MEMORY_LENGTH; const knowledgeBaseResponse = yield getKnowledgeBaseResponse(request, sourceIds, instructions, messageId, memoryLength); yield trackKnowledgeBase(knowledgeBaseResponse, request, knowledgeBaseContent, flowId); if (!knowledgeBaseResponse.hasKnowledge || !knowledgeBaseResponse.isFaithful) { return undefined; } return updateContentsWithResponse(contents, knowledgeBaseResponse); }); } function updateContentsWithResponse(contents, response) { return contents.map(content => { if (content instanceof FlowKnowledgeBase) { content.text = response.answer; content.inferenceId = response.inferenceId; } return content; }); } function trackKnowledgeBase(response, request, knowledgeBaseContent, flowId) { return __awaiter(this, void 0, void 0, function* () { const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id); const knowledgebaseInferenceId = response.inferenceId; const knowledgebaseSourcesIds = sourceIds; const knowledgebaseChunksIds = response.chunkIds; const knowledgebaseMessageId = request.input.message_id; const flowThreadId = request.session.flow_thread_id; const flowNodeId = knowledgeBaseContent.id; let knowledgebaseFailReason; if (!response.isFaithful) { knowledgebaseFailReason = KnowledgebaseFailReason.Hallucination; } if (!response.hasKnowledge) { knowledgebaseFailReason = KnowledgebaseFailReason.NoKnowledge; } yield trackEvent(request, EventAction.Knowledgebase, { knowledgebaseInferenceId, knowledgebaseFailReason, knowledgebaseSourcesIds, knowledgebaseChunksIds, knowledgebaseMessageId, userInput: request.input.data, flowThreadId, flowId, flowNodeId, }); }); } //# sourceMappingURL=knowledge-bases.js.map