n8n-nodes-gigachat
Version:
A user-friendly GigaChat AI (Sber) nodes for n8n
191 lines • 9.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.gigaChatWithModel = gigaChatWithModel;
const Tools_1 = require("./Tools");
const GigaChatApiClient_1 = require("../GigaChatApiClient");
const Memory_1 = require("./Memory");
const remove_markdown_1 = __importDefault(require("remove-markdown"));
async function getOptionalMemory(ctx) {
return (await ctx.getInputConnectionData('ai_memory', 0));
}
async function gigaChatWithModel() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const items = this.getInputData();
const returnData = [];
const credentials = await this.getCredentials('gigaChatApi');
let accessToken = undefined;
const input = this.getInputData();
if ((_a = input[0]) === null || _a === void 0 ? void 0 : _a.json.accessToken) {
accessToken = String((_c = (_b = input[0]) === null || _b === void 0 ? void 0 : _b.json) === null || _c === void 0 ? void 0 : _c.accessToken);
}
const configUpdate = {
accessToken,
credentials: credentials.authorizationKey,
scope: credentials.scope,
baseUrl: credentials.base_back_url,
authUrl: credentials.base_url
? `${credentials.base_url}/api/v2/oauth`
: 'https://ngw.devices.sberbank.ru:9443/api/v2/oauth',
verbose: credentials.debug,
};
GigaChatApiClient_1.GigaChatApiClient.updateConfig(configUpdate, !accessToken);
const memory = await getOptionalMemory(this);
for (let i = 0; i < items.length; i++) {
const modelId = this.getNodeParameter('modelId', i);
const prompt = this.getNodeParameter('prompt', i);
const options = this.getNodeParameter('options', i);
const memoryResponse = await (0, Memory_1.convertMessagesGigachat)(this, i, memory);
const messages = memoryResponse.messages;
const sessionId = memoryResponse.sessionId;
const notStoredMessages = [
{
role: 'user',
content: prompt,
},
];
const { functions, tools } = await (0, Tools_1.prepareGigaTools)(this);
const maxIterations = options.maxIterations ? Number(options.maxIterations) : 5;
let iteration = 0;
let currentResponse;
const totalUsage = {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
precached_prompt_tokens: 0,
};
while (iteration < maxIterations) {
const chatRequest = {
model: modelId,
messages: [...messages, ...notStoredMessages],
stream: false,
};
if (options.temperature !== undefined)
chatRequest.temperature = options.temperature;
if (options.max_tokens)
chatRequest.max_tokens = options.max_tokens;
if (options.top_p !== undefined)
chatRequest.top_p = options.top_p;
if (options.repetition_penalty)
chatRequest.repetition_penalty = options.repetition_penalty;
if (functions && functions.length > 0) {
chatRequest.functions = functions;
if (options.function_call && options.function_call !== 'auto') {
if (options.function_call === 'none')
chatRequest.function_call = 'none';
chatRequest.function_call = { name: options.function_call };
}
}
currentResponse = await GigaChatApiClient_1.GigaChatApiClient.chatWithSession(chatRequest, sessionId);
if (currentResponse.usage) {
totalUsage.prompt_tokens += currentResponse.usage.prompt_tokens || 0;
totalUsage.completion_tokens += currentResponse.usage.completion_tokens || 0;
totalUsage.total_tokens += currentResponse.usage.total_tokens || 0;
totalUsage.precached_prompt_tokens += currentResponse.usage.precached_prompt_tokens || 0;
}
const responseMessage = (_d = currentResponse.choices[0]) === null || _d === void 0 ? void 0 : _d.message;
if (responseMessage.content)
notStoredMessages.push({
role: 'assistant',
content: responseMessage.content,
});
if (!responseMessage.function_call) {
break;
}
notStoredMessages.push({
role: 'assistant',
function_call: {
name: responseMessage.function_call.name,
arguments: responseMessage.function_call.arguments,
},
});
const matchingTool = tools.find((tool) => { var _a; return tool.name === ((_a = responseMessage.function_call) === null || _a === void 0 ? void 0 : _a.name); });
if (!matchingTool) {
notStoredMessages.push({
role: 'function',
name: (_e = responseMessage.function_call) === null || _e === void 0 ? void 0 : _e.name,
content: JSON.stringify({
error: `Tool ${(_f = responseMessage.function_call) === null || _f === void 0 ? void 0 : _f.name} not found`,
}),
});
continue;
}
try {
const toolResult = await (0, Tools_1.executeGigaTool)(matchingTool, responseMessage.function_call);
const formattedResult = (0, Tools_1.formatGigaToolResult)(toolResult, (_g = responseMessage.function_call) === null || _g === void 0 ? void 0 : _g.arguments);
notStoredMessages.push({
role: 'function',
name: matchingTool.name,
content: formattedResult,
});
}
catch (e) {
notStoredMessages.push({
role: 'function',
name: (_h = responseMessage.function_call) === null || _h === void 0 ? void 0 : _h.name,
content: JSON.stringify({
error: e.message || String(e),
}),
});
}
iteration++;
}
const simplifyOutput = this.getNodeParameter('simplifyOutput', i);
const removeMarkdown = this.getNodeParameter('removeMarkdown', i);
let responseContent = ((_j = currentResponse === null || currentResponse === void 0 ? void 0 : currentResponse.choices[0]) === null || _j === void 0 ? void 0 : _j.message.content) || '';
if (removeMarkdown) {
responseContent = (0, remove_markdown_1.default)(responseContent);
}
const outputData = simplifyOutput
? { response: responseContent }
: {
response: responseContent,
model: modelId,
usage: totalUsage,
sessionId: ((_k = currentResponse === null || currentResponse === void 0 ? void 0 : currentResponse.xHeaders) === null || _k === void 0 ? void 0 : _k.xSessionID) || '',
iterations: iteration,
};
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(outputData), {
itemData: { item: i },
});
const functionCallsData = [];
for (let idx = 0; idx < notStoredMessages.length; idx++) {
const msg = notStoredMessages[idx];
if (msg.role === 'assistant' && msg.function_call) {
const functionName = msg.function_call.name;
const functionArgs = typeof msg.function_call.arguments === 'string'
? msg.function_call.arguments
: JSON.stringify(msg.function_call.arguments || {});
const nextFunctionMsg = notStoredMessages
.slice(idx + 1)
.find((m) => m.role === 'function' && m.name === functionName);
if (nextFunctionMsg) {
functionCallsData.push({
name: functionName,
arguments: functionArgs,
result: nextFunctionMsg.content || '',
});
}
}
}
const functionCalls = functionCallsData
.map((fc) => `Tool: ${fc.name}, Input: ${fc.arguments}, Result: ${fc.result}`)
.join('; ');
const assistantResponses = notStoredMessages
.filter((m) => m.role === 'assistant' && m.content)
.map((m) => m.content);
const finalResponse = assistantResponses[assistantResponses.length - 1] || '';
let fullOutput = finalResponse;
if (functionCalls) {
fullOutput = `[Used tools: ${functionCalls}] ${finalResponse}`;
}
if (memory && prompt && fullOutput) {
await memory.saveContext({ input: prompt }, { output: fullOutput });
}
returnData.push(...executionData);
}
return [returnData];
}
//# sourceMappingURL=ChatWithModel.js.map