|
| 1 | +import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' |
| 2 | +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' |
| 3 | +import { GooglePaLM, GooglePaLMTextInput } from 'langchain/llms/googlepalm' |
| 4 | + |
| 5 | +class GooglePaLM_LLMs implements INode { |
| 6 | + label: string |
| 7 | + name: string |
| 8 | + version: number |
| 9 | + type: string |
| 10 | + icon: string |
| 11 | + category: string |
| 12 | + description: string |
| 13 | + baseClasses: string[] |
| 14 | + credential: INodeParams |
| 15 | + inputs: INodeParams[] |
| 16 | + |
| 17 | + constructor() { |
| 18 | + this.label = 'GooglePaLM' |
| 19 | + this.name = 'GooglePaLM' |
| 20 | + this.version = 1.0 |
| 21 | + this.type = 'GooglePaLM' |
| 22 | + this.icon = 'Google_PaLM_Logo.svg' |
| 23 | + this.category = 'LLMs' |
| 24 | + this.description = 'Wrapper around Google MakerSuite PaLM large language models' |
| 25 | + this.baseClasses = [this.type, ...getBaseClasses(GooglePaLM)] |
| 26 | + this.credential = { |
| 27 | + label: 'Connect Credential', |
| 28 | + name: 'credential', |
| 29 | + type: 'credential', |
| 30 | + credentialNames: ['googleMakerSuite'] |
| 31 | + } |
| 32 | + this.inputs = [ |
| 33 | + { |
| 34 | + label: 'Model Name', |
| 35 | + name: 'modelName', |
| 36 | + type: 'options', |
| 37 | + options: [ |
| 38 | + { |
| 39 | + label: 'models/text-bison-001', |
| 40 | + name: 'models/text-bison-001' |
| 41 | + } |
| 42 | + ], |
| 43 | + default: 'models/text-bison-001', |
| 44 | + optional: true |
| 45 | + }, |
| 46 | + { |
| 47 | + label: 'Temperature', |
| 48 | + name: 'temperature', |
| 49 | + type: 'number', |
| 50 | + step: 0.1, |
| 51 | + default: 0.7, |
| 52 | + optional: true, |
| 53 | + description: |
| 54 | + 'Controls the randomness of the output.\n' + |
| 55 | + 'Values can range from [0.0,1.0], inclusive. A value closer to 1.0 ' + |
| 56 | + 'will produce responses that are more varied and creative, while ' + |
| 57 | + 'a value closer to 0.0 will typically result in more straightforward ' + |
| 58 | + 'responses from the model.' |
| 59 | + }, |
| 60 | + { |
| 61 | + label: 'Max Output Tokens', |
| 62 | + name: 'maxOutputTokens', |
| 63 | + type: 'number', |
| 64 | + step: 1, |
| 65 | + optional: true, |
| 66 | + additionalParams: true, |
| 67 | + description: 'Maximum number of tokens to generate in the completion.' |
| 68 | + }, |
| 69 | + { |
| 70 | + label: 'Top Probability', |
| 71 | + name: 'topP', |
| 72 | + type: 'number', |
| 73 | + step: 0.1, |
| 74 | + optional: true, |
| 75 | + additionalParams: true, |
| 76 | + description: |
| 77 | + 'Top-p changes how the model selects tokens for output.\n' + |
| 78 | + 'Tokens are selected from most probable to least until ' + |
| 79 | + 'the sum of their probabilities equals the top-p value.\n' + |
| 80 | + 'For example, if tokens A, B, and C have a probability of .3, .2, and .1 ' + |
| 81 | + 'and the top-p value is .5, then the model will select either A or B ' + |
| 82 | + 'as the next token (using temperature).' |
| 83 | + }, |
| 84 | + { |
| 85 | + label: 'Top-k', |
| 86 | + name: 'topK', |
| 87 | + type: 'number', |
| 88 | + step: 1, |
| 89 | + optional: true, |
| 90 | + additionalParams: true, |
| 91 | + description: |
| 92 | + 'Top-k changes how the model selects tokens for output.\n' + |
| 93 | + 'A top-k of 1 means the selected token is the most probable among ' + |
| 94 | + 'all tokens in the model vocabulary (also called greedy decoding), ' + |
| 95 | + 'while a top-k of 3 means that the next token is selected from ' + |
| 96 | + 'among the 3 most probable tokens (using temperature).' |
| 97 | + }, |
| 98 | + { |
| 99 | + label: 'Stop Sequences', |
| 100 | + name: 'stopSequencesObj', |
| 101 | + type: 'json', |
| 102 | + optional: true, |
| 103 | + additionalParams: true |
| 104 | + //default: { list:[] }, |
| 105 | + //description: |
| 106 | + // 'The "list" field should contain a list of character strings (up to 5) that will stop output generation.\n' + |
| 107 | + // ' * If specified, the API will stop at the first appearance of a stop sequence.\n' + |
| 108 | + // 'Note: The stop sequence will not be included as part of the response.' |
| 109 | + } |
| 110 | + /* |
| 111 | + { |
| 112 | + label: 'Safety Settings', |
| 113 | + name: 'safetySettings', |
| 114 | + type: 'json', |
| 115 | + optional: true, |
| 116 | + additionalParams: true |
| 117 | + } |
| 118 | + */ |
| 119 | + ] |
| 120 | + } |
| 121 | + |
| 122 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 123 | + const modelName = nodeData.inputs?.modelName as string |
| 124 | + const temperature = nodeData.inputs?.temperature as string |
| 125 | + const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string |
| 126 | + const topP = nodeData.inputs?.topP as string |
| 127 | + const topK = nodeData.inputs?.topK as string |
| 128 | + const stopSequencesObj = nodeData.inputs?.stopSequencesObj |
| 129 | + |
| 130 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 131 | + const googleMakerSuiteKey = getCredentialParam('googleMakerSuiteKey', credentialData, nodeData) |
| 132 | + |
| 133 | + const obj: Partial<GooglePaLMTextInput> = { |
| 134 | + modelName: modelName, |
| 135 | + temperature: parseFloat(temperature), |
| 136 | + apiKey: googleMakerSuiteKey |
| 137 | + } |
| 138 | + |
| 139 | + if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10) |
| 140 | + if (topP) obj.topP = parseFloat(topP) |
| 141 | + if (topK) obj.topK = parseFloat(topK) |
| 142 | + |
| 143 | + let parsedStopSequences: any | undefined = undefined |
| 144 | + if (stopSequencesObj) { |
| 145 | + try { |
| 146 | + parsedStopSequences = typeof stopSequencesObj === 'object' ? stopSequencesObj : JSON.parse(stopSequencesObj) |
| 147 | + obj.stopSequences = parsedStopSequences.list || [] |
| 148 | + } catch (exception) { |
| 149 | + throw new Error("Invalid JSON in the GooglePaLM's stopSequences: " + exception) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + const model = new GooglePaLM(obj) |
| 154 | + return model |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +module.exports = { nodeClass: GooglePaLM_LLMs } |
0 commit comments