From e5167f3e20de169c8363777cb2a1106ed040715f Mon Sep 17 00:00:00 2001 From: Martin Andrews Date: Tue, 5 Sep 2023 18:43:14 +0000 Subject: [PATCH] First draft of PaLM API LLM component --- .../GoogleMakerSuite.credential.ts | 23 +++ .../nodes/llms/GooglePaLM/GooglePaLM.ts | 158 ++++++++++++++++++ .../llms/GooglePaLM/Google_PaLM_Logo.svg | 67 ++++++++ packages/components/package.json | 1 + 4 files changed, 249 insertions(+) create mode 100644 packages/components/credentials/GoogleMakerSuite.credential.ts create mode 100644 packages/components/nodes/llms/GooglePaLM/GooglePaLM.ts create mode 100644 packages/components/nodes/llms/GooglePaLM/Google_PaLM_Logo.svg diff --git a/packages/components/credentials/GoogleMakerSuite.credential.ts b/packages/components/credentials/GoogleMakerSuite.credential.ts new file mode 100644 index 000000000..83b850a38 --- /dev/null +++ b/packages/components/credentials/GoogleMakerSuite.credential.ts @@ -0,0 +1,23 @@ +import { INodeParams, INodeCredential } from '../src/Interface' + +class GoogleMakerSuite implements INodeCredential { + label: string + name: string + version: number + inputs: INodeParams[] + + constructor() { + this.label = 'Google MakerSuite' + this.name = 'googleMakerSuite' + this.version = 1.0 + this.inputs = [ + { + label: 'MakerSuite API Key', + name: 'googleMakerSuiteKey', + type: 'password' + } + ] + } +} + +module.exports = { credClass: GoogleMakerSuite } diff --git a/packages/components/nodes/llms/GooglePaLM/GooglePaLM.ts b/packages/components/nodes/llms/GooglePaLM/GooglePaLM.ts new file mode 100644 index 000000000..c286531c1 --- /dev/null +++ b/packages/components/nodes/llms/GooglePaLM/GooglePaLM.ts @@ -0,0 +1,158 @@ +import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' +import { GooglePaLM, GooglePaLMTextInput } from 'langchain/llms/googlepalm' + +class GooglePaLM_LLMs implements INode { + label: string + name: string + version: number + type: string + icon: string + category: string + description: string + baseClasses: string[] + credential: INodeParams + inputs: INodeParams[] + + constructor() { + this.label = 'GooglePaLM' + this.name = 'GooglePaLM' + this.version = 1.0 + this.type = 'GooglePaLM' + this.icon = 'Google_PaLM_Logo.svg' + this.category = 'LLMs' + this.description = 'Wrapper around Google MakerSuite PaLM large language models' + this.baseClasses = [this.type, ...getBaseClasses(GooglePaLM)] + this.credential = { + label: 'Connect Credential', + name: 'credential', + type: 'credential', + credentialNames: ['googleMakerSuite'], + description: + 'Google MakerSuite API credential. Get this from https://makersuite.google.com/app/apikey' + + } + this.inputs = [ + { + label: 'Model Name', + name: 'modelName', + type: 'options', + options: [ + { + label: 'models/text-bison-001', + name: 'models/text-bison-001' + } + ], + default: 'models/text-bison-001', + optional: true + }, + { + label: 'Temperature', + name: 'temperature', + type: 'number', + step: 0.1, + default: 0.7, + optional: true, + description: "Controls the randomness of the output.\n"+ + "Values can range from [0.0,1.0], inclusive. A value closer to 1.0 "+ + "will produce responses that are more varied and creative, while"+ + "a value closer to 0.0 will typically result in more straightforward"+ + "responses from the model." + }, + { + label: 'Max Output Tokens', + name: 'maxOutputTokens', + type: 'number', + step: 1, + optional: true, + additionalParams: true, + description: "Maximum number of tokens to generate in the completion." + }, + { + label: 'Top Probability', + name: 'topP', + type: 'number', + step: 0.1, + optional: true, + additionalParams: true, + description: "Top-p changes how the model selects tokens for output.\n"+ + "Tokens are selected from most probable to least until "+ + "the sum of their probabilities equals the top-p value.\n"+ + "For example, if tokens A, B, and C have a probability of .3, .2, and .1 "+ + "and the top-p value is .5, then the model will select either A or B "+ + "as the next token (using temperature)." + }, + { + label: 'Top-k', + name: 'topK', + type: 'number', + step: 1, + optional: true, + additionalParams: true, + description: "Top-k changes how the model selects tokens for output.\n"+ + "A top-k of 1 means the selected token is the most probable among "+ + "all tokens in the model’s vocabulary (also called greedy decoding), "+ + "while a top-k of 3 means that the next token is selected from "+ + "among the 3 most probable tokens (using temperature)." + }, + { + label: 'Stop Sequences', + name: 'stopSequencesObj', + type: 'json', + optional: true, + additionalParams: true + //default: { list:[] }, + //description: + // "The 'list' field should contain a list of character strings (up to 5) that will stop output generation.\n"+ + // " * If specified, the API will stop at the first appearance of a stop sequence.\n"+ + // "Note: The stop sequence will not be included as part of the response." + } + /* + { + label: 'Safety Settings', + name: 'safetySettings', + type: 'json', + optional: true, + additionalParams: true + } + */ + ] + } + + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { + const modelName = nodeData.inputs?.modelName as string + const temperature = nodeData.inputs?.temperature as string + const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string + const topP = nodeData.inputs?.topP as string + const topK = nodeData.inputs?.topK as string + const stopSequencesObj = nodeData.inputs?.stopSequencesObj + + const credentialData = await getCredentialData(nodeData.credential ?? '', options) + const googleMakerSuiteKey = getCredentialParam('googleMakerSuiteKey', credentialData, nodeData) + + const obj: Partial = { + modelName: modelName, + temperature: parseFloat(temperature), + apiKey: googleMakerSuiteKey + } + + if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10) + if (topP) obj.topP = parseFloat(topP) + if (topK) obj.topK = parseFloat(topK) + + let parsedStopSequences: any | undefined = undefined + if (stopSequencesObj) { + try { + parsedStopSequences = typeof stopSequencesObj === 'object' ? stopSequencesObj : JSON.parse(stopSequencesObj) + obj.stopSequences = parsedStopSequences.list || [] + } catch (exception) { + throw new Error("Invalid JSON in the GooglePaLM's stopSequences: " + exception) + } + } + + const model = new GooglePaLM(obj) + return model + } +} + +module.exports = { nodeClass: GooglePaLM_LLMs } diff --git a/packages/components/nodes/llms/GooglePaLM/Google_PaLM_Logo.svg b/packages/components/nodes/llms/GooglePaLM/Google_PaLM_Logo.svg new file mode 100644 index 000000000..5c345fe1c --- /dev/null +++ b/packages/components/nodes/llms/GooglePaLM/Google_PaLM_Logo.svg @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/components/package.json b/packages/components/package.json index 1837609e1..d3dd4e532 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -19,6 +19,7 @@ "@aws-sdk/client-dynamodb": "^3.360.0", "@dqbd/tiktoken": "^1.0.7", "@getzep/zep-js": "^0.6.3", + "@google-ai/generativelanguage": "^0.2.1", "@huggingface/inference": "^2.6.1", "@notionhq/client": "^2.2.8", "@opensearch-project/opensearch": "^1.2.0",