|
| 1 | +import { WatsonxEmbeddings, WatsonxInputEmbeddings } from '@langchain/community/embeddings/ibm' |
| 2 | +import { WatsonxAuth } from '@langchain/community/dist/types/ibm' |
| 3 | +import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' |
| 4 | +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' |
| 5 | + |
| 6 | +class IBMWatsonx_Embeddings implements INode { |
| 7 | + label: string |
| 8 | + name: string |
| 9 | + version: number |
| 10 | + type: string |
| 11 | + icon: string |
| 12 | + category: string |
| 13 | + description: string |
| 14 | + baseClasses: string[] |
| 15 | + credential: INodeParams |
| 16 | + inputs: INodeParams[] |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.label = 'IBM Watsonx Embeddings' |
| 20 | + this.name = 'ibmEmbedding' |
| 21 | + this.version = 1.0 |
| 22 | + this.type = 'WatsonxEmbeddings' |
| 23 | + this.icon = 'ibm.png' |
| 24 | + this.category = 'Embeddings' |
| 25 | + this.description = 'Generate embeddings for a given text using open source model on IBM Watsonx' |
| 26 | + this.baseClasses = [this.type, ...getBaseClasses(WatsonxEmbeddings)] |
| 27 | + this.credential = { |
| 28 | + label: 'Connect Credential', |
| 29 | + name: 'credential', |
| 30 | + type: 'credential', |
| 31 | + credentialNames: ['ibmWatsonx'] |
| 32 | + } |
| 33 | + this.inputs = [ |
| 34 | + { |
| 35 | + label: 'Model Name', |
| 36 | + name: 'modelName', |
| 37 | + type: 'string', |
| 38 | + default: 'ibm/slate-30m-english-rtrvr' |
| 39 | + }, |
| 40 | + { |
| 41 | + label: 'Truncate Input Tokens', |
| 42 | + name: 'truncateInputTokens', |
| 43 | + type: 'number', |
| 44 | + description: 'Truncate the input tokens.', |
| 45 | + step: 1, |
| 46 | + optional: true, |
| 47 | + additionalParams: true |
| 48 | + }, |
| 49 | + { |
| 50 | + label: 'Max Retries', |
| 51 | + name: 'maxRetries', |
| 52 | + type: 'number', |
| 53 | + description: 'The maximum number of retries.', |
| 54 | + step: 1, |
| 55 | + optional: true, |
| 56 | + additionalParams: true |
| 57 | + }, |
| 58 | + { |
| 59 | + label: 'Max Concurrency', |
| 60 | + name: 'maxConcurrency', |
| 61 | + type: 'number', |
| 62 | + description: 'The maximum number of concurrencies.', |
| 63 | + step: 1, |
| 64 | + optional: true, |
| 65 | + additionalParams: true |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
| 69 | + |
| 70 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 71 | + const modelName = nodeData.inputs?.modelName as string |
| 72 | + const truncateInputTokens = nodeData.inputs?.truncateInputTokens as string |
| 73 | + const maxRetries = nodeData.inputs?.maxRetries as string |
| 74 | + const maxConcurrency = nodeData.inputs?.maxConcurrency as string |
| 75 | + |
| 76 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 77 | + const version = getCredentialParam('version', credentialData, nodeData) |
| 78 | + const serviceUrl = getCredentialParam('serviceUrl', credentialData, nodeData) |
| 79 | + const projectId = getCredentialParam('projectId', credentialData, nodeData) |
| 80 | + const watsonxAIAuthType = getCredentialParam('watsonxAIAuthType', credentialData, nodeData) |
| 81 | + const watsonxAIApikey = getCredentialParam('watsonxAIApikey', credentialData, nodeData) |
| 82 | + const watsonxAIBearerToken = getCredentialParam('watsonxAIBearerToken', credentialData, nodeData) |
| 83 | + |
| 84 | + const auth = { |
| 85 | + version, |
| 86 | + serviceUrl, |
| 87 | + projectId, |
| 88 | + watsonxAIAuthType, |
| 89 | + watsonxAIApikey, |
| 90 | + watsonxAIBearerToken |
| 91 | + } |
| 92 | + |
| 93 | + const obj: WatsonxInputEmbeddings & WatsonxAuth = { |
| 94 | + ...auth, |
| 95 | + model: modelName |
| 96 | + } |
| 97 | + |
| 98 | + if (truncateInputTokens) obj.truncateInputTokens = parseInt(truncateInputTokens, 10) |
| 99 | + if (maxRetries) obj.maxRetries = parseInt(maxRetries, 10) |
| 100 | + if (maxConcurrency) obj.maxConcurrency = parseInt(maxConcurrency, 10) |
| 101 | + |
| 102 | + const model = new WatsonxEmbeddings(obj) |
| 103 | + return model |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +module.exports = { nodeClass: IBMWatsonx_Embeddings } |
0 commit comments