Skip to content

Refactor google credentials into a shared function #4893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseCache } from '@langchain/core/caches'
import { ChatVertexAI as LcChatVertexAI, ChatVertexAIInput } from '@langchain/google-vertexai'
import { ChatVertexAIInput, ChatVertexAI as LcChatVertexAI } from '@langchain/google-vertexai'
import { buildGoogleCredentials } from '../../../src/google-utils'
import {
ICommonObject,
IMultiModalOption,
Expand All @@ -9,8 +10,8 @@ import {
INodeParams,
IVisionChatModal
} from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { getModels, MODEL_TYPE } from '../../../src/modelLoader'
import { getBaseClasses } from '../../../src/utils'

const DEFAULT_IMAGE_MAX_TOKEN = 8192
const DEFAULT_IMAGE_MODEL = 'gemini-1.5-flash-latest'
Expand Down Expand Up @@ -173,27 +174,6 @@ class GoogleVertexAI_ChatModels implements INode {
}

async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
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
Expand All @@ -217,7 +197,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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { VertexAIEmbeddings, GoogleVertexAIEmbeddingsInput } from '@langchain/google-vertexai'
import { GoogleVertexAIEmbeddingsInput, VertexAIEmbeddings } from '@langchain/google-vertexai'
import { buildGoogleCredentials } from '../../../src/google-utils'
import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { MODEL_TYPE, getModels } from '../../../src/modelLoader'
import { getBaseClasses } from '../../../src/utils'

class GoogleVertexAIEmbedding_Embeddings implements INode {
label: string
Expand Down Expand Up @@ -52,32 +53,17 @@ class GoogleVertexAIEmbedding_Embeddings implements INode {
}

async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const modelName = nodeData.inputs?.modelName as string
const googleApplicationCredentialFilePath = getCredentialParam('googleApplicationCredentialFilePath', credentialData, nodeData)
const googleApplicationCredential = getCredentialParam('googleApplicationCredential', credentialData, nodeData)
const projectID = getCredentialParam('projectID', credentialData, nodeData)
const region = nodeData.inputs?.region as string

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)
return model
Expand Down
29 changes: 5 additions & 24 deletions packages/components/nodes/llms/GoogleVertexAI/GoogleVertexAI.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -83,28 +84,6 @@ class GoogleVertexAI_LLMs implements INode {
}

async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
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
Expand All @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions packages/components/src/google-utils.ts
Original file line number Diff line number Diff line change
@@ -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<SupportedAuthOptions | null> => {
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
}