UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

151 lines 6.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KNOWLEDGE_SEARCH_V2 = void 0; /* Custom modules */ const createNodeDescriptor_1 = require("../../createNodeDescriptor"); const errors_1 = require("../../../errors"); const TOP_K_MIN_VALUE = 1; const TOP_K_MAX_VALUE = 10; /** * Node name: 'knowledgeSearchV2' * * Purpose: * Searches internal knowledge database for answers based on the user's input. */ exports.KNOWLEDGE_SEARCH_V2 = (0, createNodeDescriptor_1.createNodeDescriptor)({ type: "knowledgeSearchV2", defaultLabel: "Knowledge Search", summary: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__SUMMARY", appearance: {}, fields: [ { key: "knowledgeStoreId", type: "knowledgeStoreSelect", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__KNOWLEDGE_STORE_ID__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__KNOWLEDGE_STORE_ID__DESCRIPTION", params: { required: true }, }, { key: "topK", type: "number", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__TOP_K__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__TOP_K__DESCRIPTION", defaultValue: 3, params: { required: true, min: TOP_K_MIN_VALUE, max: TOP_K_MAX_VALUE } }, { key: "storeLocation", type: "select", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__STORE_LOCATION__LABEL", params: { options: [ { label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__STORE_LOCATION__INPUT__LABEL", value: "input" }, { label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__STORE_LOCATION__CONTEXT__LABEL", value: "context" } ], required: true }, defaultValue: "input" }, { key: "inputKey", type: "cognigyText", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__INPUT_KEY__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__INPUT_KEY__DESCRIPTION", defaultValue: "knowledgeSearch", condition: { key: "storeLocation", value: "input" } }, { key: "contextKey", type: "cognigyText", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__CONTEXT_KEY__LABEL", description: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__CONTEXT_KEY__DESCRIPTION", defaultValue: "knowledgeSearch", condition: { key: "storeLocation", value: "context" } } ], sections: [ { key: "searchSettings", label: "UI__NODE_EDITOR__KNOWLEDGE_SEARCH__SECTION_SEARCH__LABEL", defaultCollapsed: true, fields: ["topK", "storeLocation", "inputKey", "contextKey"] } ], form: [ { type: "field", key: "knowledgeStoreId" }, { type: "section", key: "searchSettings" } ], preview: { key: "text", type: "text" }, tags: ["ai", "knowledgeSearch", "knowledge", "search"], function: async (knowledgeSearchParams) => { var _a; const { cognigy, config } = knowledgeSearchParams; const { input, api } = cognigy; const { topK, storeLocation, contextKey, inputKey } = config; const knowledgeStoreId = (_a = config === null || config === void 0 ? void 0 : config.knowledgeStoreId) === null || _a === void 0 ? void 0 : _a.trim(); const { language, text, traceId } = input; if (!topK || topK < TOP_K_MIN_VALUE || topK > TOP_K_MAX_VALUE) { throw new Error(`Unable to perform knowledge search. topK value of "${topK}" is invalid. Must be between or equal to ${TOP_K_MIN_VALUE} and ${TOP_K_MAX_VALUE}.`); } if (storeLocation === "context" && !contextKey) { throw new Error("Unable to perform knowledge search. Context Key is missing."); } if (storeLocation === "input" && !inputKey) { throw new Error("Unable to perform knowledge search. Input Key is missing."); } const data = { language: language, query: text, topK: topK, traceId: traceId, disableSensitiveLogging: false }; // Add knowledgeStoreIds to data if (knowledgeStoreId) { data.knowledgeStoreIds = [knowledgeStoreId]; } // Perform knowledge search const response = await api.knowledgeSearch(data); // Handle possible errors if ((response === null || response === void 0 ? void 0 : response.status) !== "success") { const errorMessage = (response === null || response === void 0 ? void 0 : response.error) || "empty"; throw new errors_1.InternalServerError(`Error while performing knowledge search. Remote returned error: ${errorMessage}`, { traceId, disableSensitiveLogging: data === null || data === void 0 ? void 0 : data.disableSensitiveLogging }); } if (!(response === null || response === void 0 ? void 0 : response.data)) { throw new errors_1.InternalServerError("Error while performing knowledge search. Remote returned empty response.", { traceId, disableSensitiveLogging: data === null || data === void 0 ? void 0 : data.disableSensitiveLogging }); } // Save result switch (storeLocation) { case "context": api.addToContext(contextKey, response.data, "simple"); break; case "input": api.addToInput(inputKey, response.data); break; default: throw new errors_1.InternalServerError(`Error while performing knowledge search. Store location was invalid. Value was: ${storeLocation}`, { traceId, disableSensitiveLogging: data === null || data === void 0 ? void 0 : data.disableSensitiveLogging }); } } }); //# sourceMappingURL=knowledgeSearchV2.js.map