dtamind-components
Version:
Apps integration for Dtamind. Contain Nodes and Credentials.
136 lines (135 loc) • 5.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../../../src/utils");
const prompts_1 = require("@langchain/core/prompts");
const commonUtils_1 = require("../../sequentialagents/commonUtils");
const defaultFunc = `const { AIMessage, HumanMessage, ToolMessage } = require('@langchain/core/messages');
return [
new HumanMessage("What is 333382 🦜 1932?"),
new AIMessage({
content: "",
tool_calls: [
{
id: "12345",
name: "calulator",
args: {
number1: 333382,
number2: 1932,
operation: "divide",
},
},
],
}),
new ToolMessage({
tool_call_id: "12345",
content: "The answer is 172.558.",
}),
new AIMessage("The answer is 172.558."),
]`;
const TAB_IDENTIFIER = 'selectedMessagesTab';
class ChatPromptTemplate_Prompts {
constructor() {
this.label = 'Chat Prompt Template';
this.name = 'chatPromptTemplate';
this.version = 2.0;
this.type = 'ChatPromptTemplate';
this.icon = 'prompt.svg';
this.category = 'Prompts';
this.description = 'Schema to represent a chat prompt';
this.baseClasses = [this.type, ...(0, utils_1.getBaseClasses)(prompts_1.ChatPromptTemplate)];
this.inputs = [
{
label: 'System Message',
name: 'systemMessagePrompt',
type: 'string',
rows: 4,
placeholder: `You are a helpful assistant that translates {input_language} to {output_language}.`
},
{
label: 'Human Message',
name: 'humanMessagePrompt',
description: 'This prompt will be added at the end of the messages as human message',
type: 'string',
rows: 4,
placeholder: `{text}`
},
{
label: 'Format Prompt Values',
name: 'promptValues',
type: 'json',
optional: true,
acceptVariable: true,
list: true
},
{
label: 'Messages History',
name: 'messageHistory',
description: 'Add messages after System Message. This is useful when you want to provide few shot examples',
type: 'tabs',
tabIdentifier: TAB_IDENTIFIER,
additionalParams: true,
default: 'messageHistoryCode',
tabs: [
//TODO: add UI for messageHistory
{
label: 'Add Messages (Code)',
name: 'messageHistoryCode',
type: 'code',
hideCodeExecute: true,
codeExample: defaultFunc,
optional: true,
additionalParams: true
}
]
}
];
}
async init(nodeData, _, options) {
let systemMessagePrompt = nodeData.inputs?.systemMessagePrompt;
let humanMessagePrompt = nodeData.inputs?.humanMessagePrompt;
const promptValuesStr = nodeData.inputs?.promptValues;
const tabIdentifier = nodeData.inputs?.[`${TAB_IDENTIFIER}_${nodeData.id}`];
const selectedTab = tabIdentifier ? tabIdentifier.split(`_${nodeData.id}`)[0] : 'messageHistoryCode';
const messageHistoryCode = nodeData.inputs?.messageHistoryCode;
const messageHistory = nodeData.inputs?.messageHistory;
systemMessagePrompt = (0, utils_1.transformBracesWithColon)(systemMessagePrompt);
humanMessagePrompt = (0, utils_1.transformBracesWithColon)(humanMessagePrompt);
let prompt = prompts_1.ChatPromptTemplate.fromMessages([
prompts_1.SystemMessagePromptTemplate.fromTemplate(systemMessagePrompt),
prompts_1.HumanMessagePromptTemplate.fromTemplate(humanMessagePrompt)
]);
if ((messageHistory && messageHistory === 'messageHistoryCode' && messageHistoryCode) ||
(selectedTab === 'messageHistoryCode' && messageHistoryCode)) {
const appDataSource = options.appDataSource;
const databaseEntities = options.databaseEntities;
const vm = await (0, commonUtils_1.getVM)(appDataSource, databaseEntities, nodeData, options, {});
try {
const response = await vm.run(`module.exports = async function() {${messageHistoryCode}}()`, __dirname);
if (!Array.isArray(response))
throw new Error('Returned message history must be an array');
prompt = prompts_1.ChatPromptTemplate.fromMessages([
prompts_1.SystemMessagePromptTemplate.fromTemplate(systemMessagePrompt),
...response,
prompts_1.HumanMessagePromptTemplate.fromTemplate(humanMessagePrompt)
]);
}
catch (e) {
throw new Error(e);
}
}
let promptValues = {};
if (promptValuesStr) {
try {
promptValues = typeof promptValuesStr === 'object' ? promptValuesStr : JSON.parse(promptValuesStr);
}
catch (exception) {
throw new Error("Invalid JSON in the ChatPromptTemplate's promptValues: " + exception);
}
}
// @ts-ignore
prompt.promptValues = promptValues;
return prompt;
}
}
module.exports = { nodeClass: ChatPromptTemplate_Prompts };
//# sourceMappingURL=ChatPromptTemplate.js.map