@cognigy/rest-api-client
Version:
Cognigy REST-Client
283 lines • 11.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LLM_ENTITY_EXTRACT = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../createNodeDescriptor");
const prompt_1 = require("../nlu/generativeSlotFiller/prompt");
const generativeAIPrompts_1 = require("../../helpers/generativeAI/generativeAIPrompts");
exports.LLM_ENTITY_EXTRACT = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "llmEntityExtract",
defaultLabel: "LLM Entity Extract",
summary: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__DESCRIPTION",
fields: [
{
key: "llmProviderReferenceId",
type: "llmSelect",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__LLM_SELECT__LABEL",
defaultValue: "default",
params: {
required: true
}
},
{
key: "entityName",
type: "cognigyText",
label: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__FIELDS__ENTITY_NAME__LABEL",
description: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__FIELDS__ENTITY_NAME__DESCRIPTION",
defaultValue: "customerID",
params: {
required: true
}
},
{
key: "examples",
type: "keyValuePairs",
label: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__FIELDS__EXAMPLES__LABEL",
description: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__FIELDS__EXAMPLES__DESCRIPTION",
defaultValue: {
"My ID is AB54EE, is that ok?": "AB54EE",
"That would be ah bee see double 4 three": "ABC443",
"I guess it's 49 A B 8 K": "49AB8K"
},
params: {
keyLabel: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__FIELDS__EXAMPLES__INPUT__KEY__LABEL",
valueLabel: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__FIELDS__EXAMPLES__INPUT__VALUE__LABEL"
}
},
{
key: "entityDescription",
type: "cognigyText",
label: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__FIELDS__ENTITY_DESCRIPTION__LABEL",
description: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__FIELDS__ENTITY_DESCRIPTION__DESCRIPTION",
defaultValue: "a customer ID, which has 6 alphanumeric characters (e.g. ABC123).",
params: {
required: true
}
},
{
key: "temperature",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TEMPERATURE__LABEL",
type: "slider",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TEMPERATURE__DESCRIPTION",
defaultValue: 0.7,
params: {
min: 0,
max: 1,
step: 0.1
}
},
{
key: "timeout",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TIMEOUT__LABEL",
defaultValue: 5000,
type: "number",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TIMEOUT__DESCRIPTION",
},
{
key: "storeLocation",
type: "select",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STORE_LOCATION__LABEL",
description: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__SECTIONS__STORAGE__DESCRIPTION",
defaultValue: "input",
params: {
options: [
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STORE_LOCATION__OPTIONS__INPUT__LABEL",
value: "input"
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STORE_LOCATION__OPTIONS__CONTEXT__LABEL",
value: "context"
}
],
required: true
},
},
{
key: "inputKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__INPUT_KEY__LABEL",
defaultValue: "extractedEntity",
condition: {
key: "storeLocation",
value: "input",
}
},
{
key: "contextKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__CONTEXT_KEY__LABEL",
defaultValue: "extractedEntity",
condition: {
key: "storeLocation",
value: "context",
}
},
{
key: "debugLogTokenCount",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUGLOGTOKENCOUNT__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUGLOGTOKENCOUNT__DESCRIPTION",
defaultValue: false
},
{
key: "debugLogRequestAndCompletion",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUGLOGREQUESTANDCOMPLETION__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUGLOGREQUESTANDCOMPLETION__DESCRIPTION",
defaultValue: false
},
{
key: "debugDescription",
type: "description",
label: " ",
params: {
text: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUG_DESCRIPTION__TEXT"
}
},
{
key: "responseFormat",
type: "select",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__DESCRIPTION",
defaultValue: "default",
params: {
options: [
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__OPTIONS__DEFAULT__LABEL",
value: "default"
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__OPTIONS__TEXT__LABEL",
value: "text"
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__OPTIONS__JSON__LABEL",
value: "json_object"
}
]
},
},
],
sections: [
{
key: "advanced",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__SECTIONS__ADVANCED__LABEL",
defaultCollapsed: true,
fields: [
"temperature",
"timeout",
"responseFormat"
]
},
{
key: "storage",
label: "UI__NODE_EDITOR__SERVICE__LLM_ENTITY_EXTRACT__SECTIONS__STORAGE__LABEL",
defaultCollapsed: true,
fields: [
"storeLocation",
"inputKey",
"contextKey",
]
},
{
key: "debugging",
label: "UI__NODE_EDITOR__SECTIONS__DEBUG_SETTINGS__LABEL",
defaultCollapsed: true,
fields: [
"debugDescription",
"debugLogTokenCount",
"debugLogRequestAndCompletion"
]
}
],
form: [
{ type: "field", key: "llmProviderReferenceId" },
{ type: "field", key: "entityName" },
{ type: "field", key: "entityDescription" },
{ type: "field", key: "examples" },
{ type: "section", key: "advanced" },
{ type: "section", key: "storage" },
{ type: "section", key: "debugging" }
],
appearance: {},
tags: ["ai", "llm", "gpt", "generative ai", "openai", "azure", "prompt", "entity", "extract"],
function: async ({ cognigy, config }) => {
var _a, _b;
const { api, input } = cognigy;
const { temperature, storeLocation, contextKey, inputKey, timeout, debugLogTokenCount, debugLogRequestAndCompletion, llmProviderReferenceId, entityName, entityDescription, examples, responseFormat } = config;
// check if entityName is a valid JSON key and only contains alphanumeric characters and underscores
if (!entityName.match(/^[a-zA-Z0-9_]+$/)) {
throw new Error("The entity name must only contain alphanumeric characters and underscores.");
}
try {
const prompt = (0, generativeAIPrompts_1.getLLMEntityExtractPrompt)(entityName, entityDescription, examples, input.text);
const options = {
prompt,
temperature,
maxTokens: 1000,
timeoutInMs: timeout,
useCase: "promptNode",
detailedResults: true
};
if (responseFormat && responseFormat !== "default") {
options["responseFormat"] = responseFormat;
}
if (llmProviderReferenceId && llmProviderReferenceId !== "default") {
options["llmProviderReferenceId"] = llmProviderReferenceId;
}
options["chat"] = (0, prompt_1.createLastConversationChatObject)(cognigy.lastConversationEntries, (0, generativeAIPrompts_1.getLLMEntityExtractSystemMessage)(entityName, entityDescription, examples), 3, true);
const response = await api.runGenerativeAIPrompt(options, "gptPromptNode");
(0, prompt_1.writeLLMDebugLogs)("LLM Entity Extract", prompt, response, debugLogTokenCount, debugLogRequestAndCompletion, cognigy);
// find the entity in the response
let result = (_a = response === null || response === void 0 ? void 0 : response.result) === null || _a === void 0 ? void 0 : _a[entityName];
if (!result) {
try {
result = JSON.parse(`{"${entityName}": ${response.result}`)[entityName];
}
catch (err) {
try {
result = JSON.parse(response.result)[entityName];
;
}
catch (err) {
result = null;
}
}
}
if (storeLocation === "context") {
api.addToContext(contextKey, result, "simple");
if (result) {
api.logDebugMessage(`context.${contextKey} = '${result}'`);
}
}
else if (storeLocation === "input") {
// @ts-ignore
api.addToInput(inputKey, result);
if (result) {
api.logDebugMessage(`input.${inputKey} = '${result}'`);
}
}
if (!result) {
api.logDebugMessage(`UI__DEBUG_MODE__LLM_ENTITY_EXTRACT__MESSAGE`);
}
}
catch (error) {
const errorDetails = {
name: error.name,
code: error.code,
message: error.message || ((_b = error.originalErrorDetails) === null || _b === void 0 ? void 0 : _b.message),
};
api.logDebugError(errorDetails);
if (storeLocation === "context") {
api.addToContext(contextKey, { error: errorDetails }, "simple");
}
else {
// @ts-ignore
api.addToInput(inputKey, { error: errorDetails });
}
throw error;
}
}
});
//# sourceMappingURL=LLMEntityExtract.js.map