|
| 1 | +import { ChatOpenAI, OpenAIChatInput } from '@langchain/openai' |
| 2 | +import { BaseCache } from '@langchain/core/caches' |
| 3 | +import { BaseLLMParams } from '@langchain/core/language_models/llms' |
| 4 | +import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' |
| 5 | +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' |
| 6 | + |
| 7 | +class ChatNvdiaNIM_ChatModels implements INode { |
| 8 | + label: string |
| 9 | + name: string |
| 10 | + version: number |
| 11 | + type: string |
| 12 | + icon: string |
| 13 | + category: string |
| 14 | + description: string |
| 15 | + baseClasses: string[] |
| 16 | + credential: INodeParams |
| 17 | + inputs: INodeParams[] |
| 18 | + |
| 19 | + constructor() { |
| 20 | + this.label = 'ChatNvdiaNIM' |
| 21 | + this.name = 'chatNvdiaNIM' |
| 22 | + this.version = 1.0 |
| 23 | + this.type = 'ChatNvdiaNIM' |
| 24 | + this.icon = 'nvdia.svg' |
| 25 | + this.category = 'Chat Models' |
| 26 | + this.description = 'Wrapper around Nvdia NIM Inference API' |
| 27 | + this.baseClasses = [this.type, ...getBaseClasses(ChatOpenAI)] |
| 28 | + this.credential = { |
| 29 | + label: 'Connect Credential', |
| 30 | + name: 'credential', |
| 31 | + type: 'credential', |
| 32 | + credentialNames: ['nvdiaNIMApi'], |
| 33 | + optional: true |
| 34 | + } |
| 35 | + this.inputs = [ |
| 36 | + { |
| 37 | + label: 'Cache', |
| 38 | + name: 'cache', |
| 39 | + type: 'BaseCache', |
| 40 | + optional: true |
| 41 | + }, |
| 42 | + { |
| 43 | + label: 'Model Name', |
| 44 | + name: 'modelName', |
| 45 | + type: 'string', |
| 46 | + placeholder: 'microsoft/phi-3-mini-4k-instruct' |
| 47 | + }, |
| 48 | + { |
| 49 | + label: 'Temperature', |
| 50 | + name: 'temperature', |
| 51 | + type: 'number', |
| 52 | + step: 0.1, |
| 53 | + default: 0.9, |
| 54 | + optional: true |
| 55 | + }, |
| 56 | + { |
| 57 | + label: 'Base Path', |
| 58 | + name: 'basePath', |
| 59 | + type: 'string', |
| 60 | + description: 'Specify the URL of the deployed NIM Inference API', |
| 61 | + placeholder: 'https://integrate.api.nvidia.com/v1' |
| 62 | + }, |
| 63 | + { |
| 64 | + label: 'Streaming', |
| 65 | + name: 'streaming', |
| 66 | + type: 'boolean', |
| 67 | + default: true, |
| 68 | + optional: true, |
| 69 | + additionalParams: true |
| 70 | + }, |
| 71 | + { |
| 72 | + label: 'Max Tokens', |
| 73 | + name: 'maxTokens', |
| 74 | + type: 'number', |
| 75 | + step: 1, |
| 76 | + optional: true, |
| 77 | + additionalParams: true |
| 78 | + }, |
| 79 | + { |
| 80 | + label: 'Top Probability', |
| 81 | + name: 'topP', |
| 82 | + type: 'number', |
| 83 | + step: 0.1, |
| 84 | + optional: true, |
| 85 | + additionalParams: true |
| 86 | + }, |
| 87 | + { |
| 88 | + label: 'Frequency Penalty', |
| 89 | + name: 'frequencyPenalty', |
| 90 | + type: 'number', |
| 91 | + step: 0.1, |
| 92 | + optional: true, |
| 93 | + additionalParams: true |
| 94 | + }, |
| 95 | + { |
| 96 | + label: 'Presence Penalty', |
| 97 | + name: 'presencePenalty', |
| 98 | + type: 'number', |
| 99 | + step: 0.1, |
| 100 | + optional: true, |
| 101 | + additionalParams: true |
| 102 | + }, |
| 103 | + { |
| 104 | + label: 'Timeout', |
| 105 | + name: 'timeout', |
| 106 | + type: 'number', |
| 107 | + step: 1, |
| 108 | + optional: true, |
| 109 | + additionalParams: true |
| 110 | + }, |
| 111 | + { |
| 112 | + label: 'Base Options', |
| 113 | + name: 'baseOptions', |
| 114 | + type: 'json', |
| 115 | + optional: true, |
| 116 | + additionalParams: true |
| 117 | + } |
| 118 | + ] |
| 119 | + } |
| 120 | + |
| 121 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 122 | + const temperature = nodeData.inputs?.temperature as string |
| 123 | + const modelName = nodeData.inputs?.modelName as string |
| 124 | + const maxTokens = nodeData.inputs?.maxTokens as string |
| 125 | + const topP = nodeData.inputs?.topP as string |
| 126 | + const frequencyPenalty = nodeData.inputs?.frequencyPenalty as string |
| 127 | + const presencePenalty = nodeData.inputs?.presencePenalty as string |
| 128 | + const timeout = nodeData.inputs?.timeout as string |
| 129 | + const streaming = nodeData.inputs?.streaming as boolean |
| 130 | + const basePath = nodeData.inputs?.basePath as string |
| 131 | + const baseOptions = nodeData.inputs?.baseOptions |
| 132 | + const cache = nodeData.inputs?.cache as BaseCache |
| 133 | + |
| 134 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 135 | + const nvdiaNIMApiKey = getCredentialParam('nvdiaNIMApiKey', credentialData, nodeData) |
| 136 | + |
| 137 | + const obj: Partial<OpenAIChatInput> & BaseLLMParams & { nvdiaNIMApiKey?: string } = { |
| 138 | + temperature: parseFloat(temperature), |
| 139 | + modelName, |
| 140 | + openAIApiKey: nvdiaNIMApiKey, |
| 141 | + streaming: streaming ?? true |
| 142 | + } |
| 143 | + |
| 144 | + if (maxTokens) obj.maxTokens = parseInt(maxTokens, 10) |
| 145 | + if (topP) obj.topP = parseFloat(topP) |
| 146 | + if (frequencyPenalty) obj.frequencyPenalty = parseFloat(frequencyPenalty) |
| 147 | + if (presencePenalty) obj.presencePenalty = parseFloat(presencePenalty) |
| 148 | + if (timeout) obj.timeout = parseInt(timeout, 10) |
| 149 | + if (cache) obj.cache = cache |
| 150 | + |
| 151 | + let parsedBaseOptions: any | undefined = undefined |
| 152 | + |
| 153 | + if (baseOptions) { |
| 154 | + try { |
| 155 | + parsedBaseOptions = typeof baseOptions === 'object' ? baseOptions : JSON.parse(baseOptions) |
| 156 | + } catch (exception) { |
| 157 | + throw new Error("Invalid JSON in the ChatOpenAI's BaseOptions: " + exception) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + const model = new ChatOpenAI(obj, { |
| 162 | + basePath, |
| 163 | + baseOptions: parsedBaseOptions |
| 164 | + }) |
| 165 | + return model |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +module.exports = { nodeClass: ChatNvdiaNIM_ChatModels } |
0 commit comments