n8n-nodes-nemotron
Version:
n8n community node for integrating NVIDIA's Nemotron Ultra 253B AI model
258 lines • 12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LmChatNvidia = void 0;
const openai_1 = require("@langchain/openai");
const nvidia_error_handling_1 = require("./helpers/nvidia-error-handling");
class LmChatNvidia {
constructor() {
this.description = {
displayName: 'NVIDIA Nemotron',
name: 'lmChatNvidia',
icon: 'file:nvidiaLight.svg',
group: ['transform'],
version: 1,
description: 'Interact with NVIDIA Llama 3.1 Nemotron Ultra 253B model',
defaults: {
name: 'NVIDIA Nemotron',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Language Models', 'Root Nodes'],
'Language Models': ['Chat Models (Recommended)'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/community-nodes/n8n-nodes-nvidia/'
}
]
}
},
inputs: [],
outputs: ["ai_languageModel"],
outputNames: ['Model'],
credentials: [
{
name: 'nvidiaApi',
required: true,
},
],
requestDefaults: {
ignoreHttpStatusErrors: true,
baseURL: '={{ $credentials?.url }}',
},
properties: [
{
displayName: 'Model',
name: 'model',
type: 'options',
options: [
{
name: 'Llama 3.1 Nemotron Ultra 253B',
value: 'nvidia/llama-3.1-nemotron-ultra-253b-v1',
},
],
default: 'nvidia/llama-3.1-nemotron-ultra-253b-v1',
description: 'The model which will generate the completion',
},
{
displayName: 'If using JSON response format, you must include word "json" in the prompt in your chain or agent',
name: 'notice',
type: 'notice',
default: '',
displayOptions: {
show: {
'/options.responseFormat': ['json_object'],
},
},
},
{
displayName: 'Options',
name: 'options',
placeholder: 'Add Option',
description: 'Additional options to add',
type: 'collection',
default: {},
options: [
{
displayName: 'Maximum Number of Tokens',
name: 'maxTokens',
default: 4096,
description: 'The maximum number of tokens to generate in the completion',
type: 'number',
typeOptions: {
maxValue: 8192,
},
},
{
displayName: 'Response Format',
name: 'responseFormat',
default: 'text',
type: 'options',
options: [
{
name: 'Text',
value: 'text',
description: 'Regular text response',
},
{
name: 'JSON',
value: 'json_object',
description: 'Enables JSON mode, which should guarantee the message the model generates is valid JSON',
},
],
},
{
displayName: 'Sampling Temperature',
name: 'temperature',
default: 0.6,
typeOptions: { maxValue: 1, minValue: 0, numberPrecision: 2 },
description: 'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.',
type: 'number',
},
{
displayName: 'Top P',
name: 'topP',
default: 0.95,
typeOptions: { maxValue: 1, minValue: 0, numberPrecision: 2 },
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.',
type: 'number',
},
{
displayName: 'Frequency Penalty',
name: 'frequencyPenalty',
default: 0,
typeOptions: { maxValue: 2, minValue: -2, numberPrecision: 2 },
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',
type: 'number',
},
{
displayName: 'Presence Penalty',
name: 'presencePenalty',
default: 0,
typeOptions: { maxValue: 2, minValue: -2, numberPrecision: 2 },
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',
type: 'number',
},
{
displayName: 'Timeout',
name: 'timeout',
default: 360000,
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: 'System Message',
name: 'systemMessage',
default: 'Você é um assistente de IA prestativo. Forneça respostas concisas e precisas.',
description: 'System message to send to the model (leave empty for default)',
type: 'string',
typeOptions: {
rows: 4,
},
},
],
},
],
};
}
async execute() {
const returnData = [];
return [returnData];
}
async supplyData(itemIndex) {
const credentials = await this.getCredentials('nvidiaApi');
const modelName = this.getNodeParameter('model', itemIndex);
const options = this.getNodeParameter('options', itemIndex, {});
if (!credentials.apiKey) {
throw new Error('API Key não encontrada nas credenciais NVIDIA. Verifique sua configuração.');
}
const configuration = {
baseURL: credentials.url,
};
const defaultSystemMessage = 'Você é um assistente de IA prestativo. Forneça respostas concisas e precisas.';
const defaultTemperature = 0.6;
const defaultTopP = 0.95;
const defaultMaxTokens = 4096;
const defaultFrequencyPenalty = 0;
const defaultPresencePenalty = 0;
const modelKwargs = {};
if (options.responseFormat) {
modelKwargs.response_format = { type: options.responseFormat };
}
const systemMessage = options.systemMessage || defaultSystemMessage;
modelKwargs.system_prompt = systemMessage;
console.log('[NVIDIA] Configurando modelo com os seguintes parâmetros:');
console.log(`[NVIDIA] Modelo: ${modelName}`);
console.log(`[NVIDIA] Sistema: ${systemMessage}`);
console.log(`[NVIDIA] Temperatura: ${options.temperature || defaultTemperature}`);
console.log(`[NVIDIA] Top P: ${options.topP || defaultTopP}`);
console.log(`[NVIDIA] Max Tokens: ${options.maxTokens || defaultMaxTokens}`);
const model = new openai_1.ChatOpenAI({
openAIApiKey: credentials.apiKey,
modelName,
temperature: options.temperature || defaultTemperature,
maxTokens: options.maxTokens || defaultMaxTokens,
topP: options.topP || defaultTopP,
frequencyPenalty: options.frequencyPenalty || defaultFrequencyPenalty,
presencePenalty: options.presencePenalty || defaultPresencePenalty,
timeout: options.timeout || 360000,
maxRetries: options.maxRetries || 2,
configuration,
modelKwargs: modelKwargs,
streaming: true,
onFailedAttempt: (error) => {
const failureCategory = (0, nvidia_error_handling_1.nvidiaFailedAttemptHandler)(error);
if (failureCategory) {
console.log(`NVIDIA API failed with category: ${failureCategory}`);
}
throw error;
},
});
const apiKey = credentials.apiKey;
const baseURL = credentials.url;
if (!model.bindTools) {
model.bindTools = function (tools) {
console.log("[NVIDIA] Binding tools to Nemotron model...");
console.log(`[NVIDIA] Number of tools: ${tools.length}`);
const newModel = new openai_1.ChatOpenAI({
modelName: this.modelName,
temperature: this.temperature,
topP: this.topP,
frequencyPenalty: this.frequencyPenalty,
presencePenalty: this.presencePenalty,
maxTokens: this.maxTokens,
openAIApiKey: apiKey,
configuration: { baseURL: baseURL },
timeout: this.timeout,
maxRetries: this.maxRetries,
streaming: this.streaming,
modelKwargs: this.modelKwargs,
});
newModel._functions = tools;
newModel._functionsMode = 'tools';
if (!newModel.lc_namespace) {
newModel.lc_namespace = ['langchain', 'chat_models', 'openai'];
}
console.log("[NVIDIA] Tools bound successfully to Nemotron model");
return newModel;
};
}
if (!model.lc_namespace) {
model.lc_namespace = ['langchain', 'chat_models', 'openai'];
}
return {
response: model,
};
}
}
exports.LmChatNvidia = LmChatNvidia;
//# sourceMappingURL=LmChatNvidia.node.js.map