UNPKG

n8n-nodes-wanted-laas

Version:
330 lines 15.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WantedLaaS = void 0; const n8n_workflow_1 = require("n8n-workflow"); class WantedLaaS { constructor() { this.description = { displayName: 'Wanted LaaS', name: 'wantedLaaS', icon: { light: 'file:WantedLaaS.svg', dark: 'file:WantedLaaS-dark.svg' }, group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Interact with Wanted LaaS API', defaults: { name: 'Wanted LaaS', }, inputs: '={{["main"]}}', outputs: '={{["main"]}}', credentials: [ { name: 'wantedLaaSApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Chat', value: 'chat', description: 'Send a chat message', action: 'Send a chat message', }, ], default: 'chat', }, { displayName: 'Preset Hash', name: 'hash', type: 'string', noDataExpression: true, required: true, default: '', description: 'The hash of the model to use. You can find this in the URL when viewing a model in the Wanted LaaS dashboard.', }, { displayName: 'System Prompt', name: 'system_prompt', type: 'string', typeOptions: { rows: 4, }, default: '', description: 'System message to set the behavior of the assistant', placeholder: 'You are a helpful assistant...', }, { displayName: 'Message', name: 'message', type: 'string', typeOptions: { rows: 4, }, default: '={{ $json.chatInput }}', description: 'The message to send to the chat model', }, { displayName: 'Params', name: 'params', type: 'json', default: '={}', description: 'Additional params to pass to the API', typeOptions: { alwaysOpenEditWindow: true, }, }, { displayName: 'Temperature', name: 'temperature', type: 'number', default: 0.9, description: 'What sampling temperature to use', }, { displayName: 'Additional Fields', name: 'additionalFields', type: 'collection', placeholder: 'Add Field', default: {}, options: [ { displayName: 'Frequency Penalty', name: 'frequency_penalty', type: 'number', default: 0, description: 'Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency.', }, { displayName: 'Max Tokens', name: 'max_tokens', type: 'number', default: 1000, description: 'The maximum number of tokens to generate', }, { displayName: 'Presence Penalty', name: 'presence_penalty', type: 'number', default: 0, description: 'Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.', }, { displayName: 'Response Format', name: 'response_format', type: 'options', options: [ { name: 'Text', value: 'text', description: 'Text format response', }, { name: 'JSON Object', value: 'json_object', description: 'JSON object format response', }, ], default: 'text', description: 'The format of the response', }, { displayName: 'Top P', name: 'top_p', type: 'number', default: 1, description: 'An alternative to sampling with temperature, called nucleus sampling', }, ], }, ], }; this.methods = {}; } async execute() { var _a, _b, _c, _d, _e, _f, _g; const items = this.getInputData(); const returnData = []; const credentials = await this.getCredentials('wantedLaaSApi'); if (!(credentials === null || credentials === void 0 ? void 0 : credentials.apiKey)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No valid API key provided'); } if (!(credentials === null || credentials === void 0 ? void 0 : credentials.project)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No valid project provided'); } for (let i = 0; i < items.length; i++) { const hash = this.getNodeParameter('hash', i); const operation = this.getNodeParameter('operation', i); const params = this.getNodeParameter('params', i, {}); const systemPrompt = this.getNodeParameter('system_prompt', i, ''); const message = this.getNodeParameter('message', i, ''); const temperature = this.getNodeParameter('temperature', i); const additionalFields = this.getNodeParameter('additionalFields', i, {}); let requestBody = {}; try { if (operation === 'chat') { const messages = []; if (systemPrompt) { messages.push({ role: 'system', content: systemPrompt, }); } if (message) { messages.push({ role: 'user', content: message, }); } if (temperature !== null && temperature !== undefined) { if (temperature < 0 || temperature > 2) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Temperature must be between 0 and 2'); } } if (!hash.trim()) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Preset Hash cannot be empty'); } const top_p = additionalFields.top_p; if (top_p !== undefined) { if (typeof top_p !== 'number' || top_p < 0 || top_p > 1) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Top P must be a number between 0 and 1'); } } const frequency_penalty = additionalFields.frequency_penalty; if (frequency_penalty !== undefined) { if (typeof frequency_penalty !== 'number' || frequency_penalty < -2 || frequency_penalty > 2) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Frequency penalty must be a number between -2 and 2'); } } const presence_penalty = additionalFields.presence_penalty; if (presence_penalty !== undefined) { if (typeof presence_penalty !== 'number' || presence_penalty < -2 || presence_penalty > 2) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Presence penalty must be a number between -2 and 2'); } } if (additionalFields.response_format) { additionalFields.response_format = { type: additionalFields.response_format }; } requestBody = { hash, ...additionalFields, }; if (params && Object.keys(params).length > 0) { try { requestBody.params = typeof params === 'string' ? JSON.parse(params) : params; if (typeof requestBody.params !== 'object' || requestBody.params === null) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Params must be a valid JSON object'); } } catch (error) { if (error instanceof Error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid params format: ${error.message}`); } throw error; } } if (messages && messages.length > 0) { requestBody.messages = messages; } if (temperature !== null && temperature !== undefined) { requestBody.temperature = temperature; } const options = { method: 'POST', url: 'https://api-laas.wanted.co.kr/api/preset/v2/chat/completions', headers: { project: `${credentials.project}`, apiKey: `${credentials.apiKey}`, 'Content-Type': 'application/json', }, body: requestBody, json: true, }; if (options.headers) { console.log('Request Data:', { url: options.url, body: JSON.stringify(requestBody, null, 2), headers: { project: options.headers.project, }, }); } let response; try { response = (await this.helpers.request(options)); } catch (error) { let errorMessage = 'Request to Wanted LaaS API failed'; if (error.response) { switch (error.response.status) { case 400: errorMessage = 'Bad request: Please check your input parameters'; break; case 401: errorMessage = 'Authentication failed: Please check your API credentials'; break; case 403: errorMessage = 'Access forbidden: Please check your API permissions'; break; case 429: errorMessage = 'Too many requests: Please try again later'; break; default: errorMessage = `API Error (${error.response.status}): ${((_a = error.response.data) === null || _a === void 0 ? void 0 : _a.message) || error.message}`; } } throw new n8n_workflow_1.NodeOperationError(this.getNode(), errorMessage); } if (!response) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Empty response from API'); } if (!Array.isArray(response.choices)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid response format: missing choices array'); } if (!((_c = (_b = response.choices[0]) === null || _b === void 0 ? void 0 : _b.message) === null || _c === void 0 ? void 0 : _c.content)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid response format: missing message content'); } returnData.push({ json: response, pairedItem: { item: i }, }); } } catch (error) { console.error('Error details:', { message: error.message, response: (_d = error.response) === null || _d === void 0 ? void 0 : _d.data, status: (_e = error.response) === null || _e === void 0 ? void 0 : _e.status, headers: (_f = error.response) === null || _f === void 0 ? void 0 : _f.headers, requestBody: JSON.stringify(requestBody, null, 2), stack: error.stack, }); if (this.continueOnFail()) { returnData.push({ json: { error: error.message, errorDetails: (_g = error.response) === null || _g === void 0 ? void 0 : _g.data, }, pairedItem: { item: i }, }); continue; } throw error; } } return [returnData]; } } exports.WantedLaaS = WantedLaaS; //# sourceMappingURL=WantedLaaS.node.js.map