|
| 1 | +import { BaseCache } from '@langchain/core/caches' |
| 2 | +import { ChatBaiduWenxin } from '@langchain/community/chat_models/baiduwenxin' |
| 3 | +import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' |
| 4 | +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' |
| 5 | + |
| 6 | +class ChatBaiduWenxin_ChatModels 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 = 'ChatBaiduWenxin' |
| 20 | + this.name = 'chatBaiduWenxin' |
| 21 | + this.version = 1.0 |
| 22 | + this.type = 'ChatBaiduWenxin' |
| 23 | + this.icon = 'baiduwenxin.svg' |
| 24 | + this.category = 'Chat Models' |
| 25 | + this.description = 'Wrapper around BaiduWenxin Chat Endpoints' |
| 26 | + this.baseClasses = [this.type, ...getBaseClasses(ChatBaiduWenxin)] |
| 27 | + this.credential = { |
| 28 | + label: 'Connect Credential', |
| 29 | + name: 'credential', |
| 30 | + type: 'credential', |
| 31 | + credentialNames: ['baiduApi'] |
| 32 | + } |
| 33 | + this.inputs = [ |
| 34 | + { |
| 35 | + label: 'Cache', |
| 36 | + name: 'cache', |
| 37 | + type: 'BaseCache', |
| 38 | + optional: true |
| 39 | + }, |
| 40 | + { |
| 41 | + label: 'Model', |
| 42 | + name: 'modelName', |
| 43 | + type: 'string', |
| 44 | + placeholder: 'ERNIE-Bot-turbo' |
| 45 | + }, |
| 46 | + { |
| 47 | + label: 'Temperature', |
| 48 | + name: 'temperature', |
| 49 | + type: 'number', |
| 50 | + step: 0.1, |
| 51 | + default: 0.9, |
| 52 | + optional: true |
| 53 | + } |
| 54 | + ] |
| 55 | + } |
| 56 | + |
| 57 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 58 | + const cache = nodeData.inputs?.cache as BaseCache |
| 59 | + const temperature = nodeData.inputs?.temperature as string |
| 60 | + const modelName = nodeData.inputs?.modelName as string |
| 61 | + |
| 62 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 63 | + const baiduApiKey = getCredentialParam('baiduApiKey', credentialData, nodeData) |
| 64 | + const baiduSecretKey = getCredentialParam('baiduSecretKey', credentialData, nodeData) |
| 65 | + |
| 66 | + const obj: Partial<ChatBaiduWenxin> = { |
| 67 | + streaming: true, |
| 68 | + baiduApiKey, |
| 69 | + baiduSecretKey, |
| 70 | + modelName, |
| 71 | + temperature: temperature ? parseFloat(temperature) : undefined |
| 72 | + } |
| 73 | + if (cache) obj.cache = cache |
| 74 | + |
| 75 | + const model = new ChatBaiduWenxin(obj) |
| 76 | + return model |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +module.exports = { nodeClass: ChatBaiduWenxin_ChatModels } |
0 commit comments