diff --git a/packages/components/nodes/chatmodels/ChatGoogleVertexAI/ChatGoogleVertexAI.ts b/packages/components/nodes/chatmodels/ChatGoogleVertexAI/ChatGoogleVertexAI.ts index e27c3609350..00641da1dfa 100644 --- a/packages/components/nodes/chatmodels/ChatGoogleVertexAI/ChatGoogleVertexAI.ts +++ b/packages/components/nodes/chatmodels/ChatGoogleVertexAI/ChatGoogleVertexAI.ts @@ -1,5 +1,6 @@ import { BaseCache } from '@langchain/core/caches' import { ChatVertexAIInput, ChatVertexAI as LcChatVertexAI } from '@langchain/google-vertexai' +import { buildGoogleCredentials } from '../../../src/google-utils' import { ICommonObject, IMultiModalOption, @@ -10,7 +11,7 @@ import { IVisionChatModal } from '../../../src/Interface' import { getModels, getRegions, MODEL_TYPE } from '../../../src/modelLoader' -import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' +import { getBaseClasses } from '../../../src/utils' const DEFAULT_IMAGE_MAX_TOKEN = 8192 const DEFAULT_IMAGE_MODEL = 'gemini-1.5-flash-latest' @@ -184,27 +185,6 @@ class GoogleVertexAI_ChatModels implements INode { } async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { - const credentialData = await getCredentialData(nodeData.credential ?? '', options) - const googleApplicationCredentialFilePath = getCredentialParam('googleApplicationCredentialFilePath', credentialData, nodeData) - const googleApplicationCredential = getCredentialParam('googleApplicationCredential', credentialData, nodeData) - const projectID = getCredentialParam('projectID', credentialData, nodeData) - - const authOptions: ICommonObject = {} - if (Object.keys(credentialData).length !== 0) { - if (!googleApplicationCredentialFilePath && !googleApplicationCredential) - throw new Error('Please specify your Google Application Credential') - if (!googleApplicationCredentialFilePath && !googleApplicationCredential) - throw new Error( - 'Error: More than one component has been inputted. Please use only one of the following: Google Application Credential File Path or Google Credential JSON Object' - ) - if (googleApplicationCredentialFilePath && !googleApplicationCredential) - authOptions.keyFile = googleApplicationCredentialFilePath - else if (!googleApplicationCredentialFilePath && googleApplicationCredential) - authOptions.credentials = JSON.parse(googleApplicationCredential) - - if (projectID) authOptions.projectId = projectID - } - const temperature = nodeData.inputs?.temperature as string const modelName = nodeData.inputs?.modelName as string const customModelName = nodeData.inputs?.customModelName as string @@ -229,7 +209,10 @@ class GoogleVertexAI_ChatModels implements INode { modelName: customModelName || modelName, streaming: streaming ?? true } - if (Object.keys(authOptions).length !== 0) obj.authOptions = authOptions + + const authOptions = await buildGoogleCredentials(nodeData, options) + if (authOptions && Object.keys(authOptions).length !== 0) obj.authOptions = authOptions + if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10) if (topP) obj.topP = parseFloat(topP) if (cache) obj.cache = cache diff --git a/packages/components/nodes/embeddings/GoogleVertexAIEmbedding/GoogleVertexAIEmbedding.ts b/packages/components/nodes/embeddings/GoogleVertexAIEmbedding/GoogleVertexAIEmbedding.ts index 20e40c1ffa6..e73ed50eeb1 100644 --- a/packages/components/nodes/embeddings/GoogleVertexAIEmbedding/GoogleVertexAIEmbedding.ts +++ b/packages/components/nodes/embeddings/GoogleVertexAIEmbedding/GoogleVertexAIEmbedding.ts @@ -1,7 +1,8 @@ import { GoogleVertexAIEmbeddingsInput, VertexAIEmbeddings } from '@langchain/google-vertexai' +import { buildGoogleCredentials } from '../../../src/google-utils' import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface' import { MODEL_TYPE, getModels, getRegions } from '../../../src/modelLoader' -import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' +import { getBaseClasses } from '../../../src/utils' class GoogleVertexAIEmbedding_Embeddings implements INode { label: string @@ -63,33 +64,16 @@ class GoogleVertexAIEmbedding_Embeddings implements INode { } async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { - const credentialData = await getCredentialData(nodeData.credential ?? '', options) const modelName = nodeData.inputs?.modelName as string const region = nodeData.inputs?.region as string - const googleApplicationCredentialFilePath = getCredentialParam('googleApplicationCredentialFilePath', credentialData, nodeData) - const googleApplicationCredential = getCredentialParam('googleApplicationCredential', credentialData, nodeData) - const projectID = getCredentialParam('projectID', credentialData, nodeData) - const authOptions: any = {} - if (Object.keys(credentialData).length !== 0) { - if (!googleApplicationCredentialFilePath && !googleApplicationCredential) - throw new Error('Please specify your Google Application Credential') - if (!googleApplicationCredentialFilePath && !googleApplicationCredential) - throw new Error( - 'Error: More than one component has been inputted. Please use only one of the following: Google Application Credential File Path or Google Credential JSON Object' - ) - - if (googleApplicationCredentialFilePath && !googleApplicationCredential) - authOptions.keyFile = googleApplicationCredentialFilePath - else if (!googleApplicationCredentialFilePath && googleApplicationCredential) - authOptions.credentials = JSON.parse(googleApplicationCredential) - - if (projectID) authOptions.projectId = projectID - } const obj: GoogleVertexAIEmbeddingsInput = { model: modelName } - if (Object.keys(authOptions).length !== 0) obj.authOptions = authOptions + + const authOptions = await buildGoogleCredentials(nodeData, options) + if (authOptions && Object.keys(authOptions).length !== 0) obj.authOptions = authOptions + if (region) obj.location = region const model = new VertexAIEmbeddings(obj) diff --git a/packages/components/nodes/llms/GoogleVertexAI/GoogleVertexAI.ts b/packages/components/nodes/llms/GoogleVertexAI/GoogleVertexAI.ts index fa6ec0000c0..273ce81b9eb 100644 --- a/packages/components/nodes/llms/GoogleVertexAI/GoogleVertexAI.ts +++ b/packages/components/nodes/llms/GoogleVertexAI/GoogleVertexAI.ts @@ -1,8 +1,9 @@ import { BaseCache } from '@langchain/core/caches' import { VertexAI, VertexAIInput } from '@langchain/google-vertexai' import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface' -import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' +import { getBaseClasses } from '../../../src/utils' import { getModels, MODEL_TYPE } from '../../../src/modelLoader' +import { buildGoogleCredentials } from '../../../src/google-utils' class GoogleVertexAI_LLMs implements INode { label: string @@ -83,28 +84,6 @@ class GoogleVertexAI_LLMs implements INode { } async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { - const credentialData = await getCredentialData(nodeData.credential ?? '', options) - const googleApplicationCredentialFilePath = getCredentialParam('googleApplicationCredentialFilePath', credentialData, nodeData) - const googleApplicationCredential = getCredentialParam('googleApplicationCredential', credentialData, nodeData) - const projectID = getCredentialParam('projectID', credentialData, nodeData) - - const authOptions: any = {} - if (Object.keys(credentialData).length !== 0) { - if (!googleApplicationCredentialFilePath && !googleApplicationCredential) - throw new Error('Please specify your Google Application Credential') - if (!googleApplicationCredentialFilePath && !googleApplicationCredential) - throw new Error( - 'Error: More than one component has been inputted. Please use only one of the following: Google Application Credential File Path or Google Credential JSON Object' - ) - - if (googleApplicationCredentialFilePath && !googleApplicationCredential) - authOptions.keyFile = googleApplicationCredentialFilePath - else if (!googleApplicationCredentialFilePath && googleApplicationCredential) - authOptions.credentials = JSON.parse(googleApplicationCredential) - - if (projectID) authOptions.projectId = projectID - } - const temperature = nodeData.inputs?.temperature as string const modelName = nodeData.inputs?.modelName as string const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string @@ -115,7 +94,9 @@ class GoogleVertexAI_LLMs implements INode { temperature: parseFloat(temperature), model: modelName } - if (Object.keys(authOptions).length !== 0) obj.authOptions = authOptions + + const authOptions = await buildGoogleCredentials(nodeData, options) + if (authOptions && Object.keys(authOptions).length !== 0) obj.authOptions = authOptions if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10) if (topP) obj.topP = parseFloat(topP) diff --git a/packages/components/src/google-utils.ts b/packages/components/src/google-utils.ts new file mode 100644 index 00000000000..f868e669998 --- /dev/null +++ b/packages/components/src/google-utils.ts @@ -0,0 +1,29 @@ +import { getCredentialData, getCredentialParam, type ICommonObject, type INodeData } from '.' +import type { ChatVertexAIInput, VertexAIInput } from '@langchain/google-vertexai' + +type SupportedAuthOptions = ChatVertexAIInput['authOptions'] | VertexAIInput['authOptions'] + +export const buildGoogleCredentials = async (nodeData: INodeData, options: ICommonObject): Promise => { + const credentialData = await getCredentialData(nodeData.credential ?? '', options) + const googleApplicationCredentialFilePath = getCredentialParam('googleApplicationCredentialFilePath', credentialData, nodeData) + const googleApplicationCredential = getCredentialParam('googleApplicationCredential', credentialData, nodeData) + const projectID = getCredentialParam('projectID', credentialData, nodeData) + + const authOptions: any = {} + if (Object.keys(credentialData).length !== 0) { + if (!googleApplicationCredentialFilePath && !googleApplicationCredential) + throw new Error('Please specify your Google Application Credential') + if (!googleApplicationCredentialFilePath && !googleApplicationCredential) + throw new Error( + 'Error: More than one component has been inputted. Please use only one of the following: Google Application Credential File Path or Google Credential JSON Object' + ) + + if (googleApplicationCredentialFilePath && !googleApplicationCredential) authOptions.keyFile = googleApplicationCredentialFilePath + else if (!googleApplicationCredentialFilePath && googleApplicationCredential) + authOptions.credentials = JSON.parse(googleApplicationCredential) + + if (projectID) authOptions.projectId = projectID + } + + return authOptions +}