n8n-nodes-wanted-laas
Version:
Wanted LaaS nodes for n8n
285 lines • 13.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WantedLaaSChatModel = void 0;
const openai_1 = require("@langchain/openai");
const n8n_workflow_1 = require("n8n-workflow");
class WantedLaaSChatModel {
constructor() {
this.description = {
displayName: 'Wanted LaaS Chat Model',
name: 'lmChatWantedLaaS',
icon: { light: 'file:WantedLaaS.svg', dark: 'file:WantedLaaS-dark.svg' },
group: ['transform'],
version: [1],
description: 'For advanced usage with an AI chain',
defaults: {
name: 'Wanted LaaS Chat Model',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Language Models', 'Root Nodes'],
'Language Models': ['Chat Models (Recommended)'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.lmchatopenai/',
},
],
},
},
inputs: [],
outputs: ['ai_languageModel'],
outputNames: ['Model'],
credentials: [
{
name: 'wantedLaaSApi',
required: true,
},
],
requestDefaults: {
ignoreHttpStatusErrors: true,
baseURL: 'https://api-laas.wanted.co.kr/api/preset/v2/',
},
properties: [
{
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: 'Options',
name: 'options',
placeholder: 'Add Option',
description: 'Additional options to add',
type: 'collection',
default: {},
options: [
{
displayName: 'Temperature',
name: 'temperature',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 2,
numberPrecision: 1,
},
default: 0.7,
description: 'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.',
},
{
displayName: 'Maximum Number of Tokens',
name: 'max_tokens',
type: 'number',
default: -1,
description: 'The maximum number of tokens to generate in the completion. Most models have a context length of 2048 tokens (except for the newest models, which support 32,768).',
typeOptions: {
maxValue: 32768,
},
},
{
displayName: 'Top P',
name: 'top_p',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 1,
numberPrecision: 2,
},
default: 1,
description: 'Controls diversity via nucleus sampling: 0.5 means half of all likelihood-weighted options are considered. We generally recommend altering this or temperature but not both.',
},
{
displayName: 'Frequency Penalty',
name: 'frequency_penalty',
type: 'number',
typeOptions: {
minValue: -2,
maxValue: 2,
numberPrecision: 1,
},
default: 0,
description: "Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim",
},
{
displayName: 'Presence Penalty',
name: 'presence_penalty',
type: 'number',
typeOptions: {
minValue: -2,
maxValue: 2,
numberPrecision: 1,
},
default: 0,
description: "Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics",
},
{
displayName: 'Response Format',
name: 'responseFormat',
type: 'options',
options: [
{
name: 'Text',
value: 'text',
description: 'Regular text response',
},
{
name: 'JSON Object',
value: 'json_object',
description: 'Enables JSON mode, which should guarantee the message the model generates is valid JSON',
},
],
default: 'text',
description: 'The format of the response',
},
{
displayName: 'Timeout',
name: 'timeout',
default: 60000,
description: 'Maximum amount of time a request is allowed to take in milliseconds',
type: 'number',
},
{
displayName: 'Max Retries',
name: 'maxRetries',
default: 2,
description: 'Maximum number of retries to attempt',
type: 'number',
},
],
},
{
displayName: 'Additional Parameters',
name: 'params',
type: 'json',
default: '{}',
description: 'Additional parameters to pass to the API',
typeOptions: {
alwaysOpenEditWindow: true,
},
},
],
};
this.methods = {};
}
async supplyData(itemIndex) {
var _a, _b;
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');
}
const hash = this.getNodeParameter('hash', itemIndex);
const options = this.getNodeParameter('options', itemIndex, {});
const additionalParams = this.getNodeParameter('params', itemIndex, {});
if (!hash.trim()) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Preset Hash cannot be empty');
}
const configuration = {
baseURL: 'https://api-laas.wanted.co.kr/api/preset/v2',
defaultHeaders: {
'Content-Type': 'application/json',
project: `${credentials.project}`,
apiKey: `${credentials.apiKey}`,
},
fetch: async (url, init) => {
var _a;
const endpoint = typeof url === 'string' ? url : url.toString();
const isCompletionsEndpoint = endpoint.includes('/chat/completions');
if (!isCompletionsEndpoint) {
return new Response(JSON.stringify({ error: 'Unsupported endpoint' }), {
status: 404,
headers: { 'Content-Type': 'application/json' },
});
}
const body = (init === null || init === void 0 ? void 0 : init.body) ? JSON.parse(init.body) : {};
const requestBody = {
hash,
messages: body.messages || [],
temperature: body.temperature,
max_tokens: body.max_tokens,
top_p: body.top_p,
frequency_penalty: body.frequency_penalty,
presence_penalty: body.presence_penalty,
};
if (body.response_format) {
requestBody.response_format = body.response_format;
}
if (additionalParams && Object.keys(additionalParams).length > 0) {
try {
const parsedParams = typeof additionalParams === 'string'
? JSON.parse(additionalParams)
: additionalParams;
requestBody.params = parsedParams;
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid params format: ${error.message}`);
}
}
const requestOptions = {
method: 'POST',
url: 'https://api-laas.wanted.co.kr/api/preset/v2/chat/completions',
headers: {
'Content-Type': 'application/json',
project: `${credentials.project}`,
apiKey: `${credentials.apiKey}`,
},
body: requestBody,
json: true,
timeout: (_a = options.timeout) !== null && _a !== void 0 ? _a : 60000,
};
try {
const response = (await this.helpers.request(requestOptions));
const openAIResponse = {
id: response.id,
object: response.object,
created: response.created,
model: response.model || hash,
choices: response.choices,
usage: response.usage,
};
return new Response(JSON.stringify(openAIResponse), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
catch (error) {
console.error('Error calling Wanted LaaS API:', error);
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
},
};
const model = new openai_1.ChatOpenAI({
openAIApiKey: credentials.apiKey,
model: hash,
temperature: options.temperature,
maxTokens: options.maxTokens === -1 ? undefined : options.maxTokens,
topP: options.topP,
frequencyPenalty: options.frequencyPenalty,
presencePenalty: options.presencePenalty,
timeout: (_a = options.timeout) !== null && _a !== void 0 ? _a : 60000,
maxRetries: (_b = options.maxRetries) !== null && _b !== void 0 ? _b : 2,
configuration,
});
if (options.responseFormat === 'json_object') {
model.modelKwargs = {
response_format: { type: 'json_object' },
};
}
return {
response: model,
};
}
}
exports.WantedLaaSChatModel = WantedLaaSChatModel;
//# sourceMappingURL=WantedLaaSChatModel.node.js.map