Skip to content

Commit 00342bd

Browse files
authored
Refactor google credentials into a shared function (#4893)
1 parent a3f47af commit 00342bd

File tree

4 files changed

+46
-69
lines changed

4 files changed

+46
-69
lines changed

packages/components/nodes/chatmodels/ChatGoogleVertexAI/ChatGoogleVertexAI.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BaseCache } from '@langchain/core/caches'
22
import { ChatVertexAIInput, ChatVertexAI as LcChatVertexAI } from '@langchain/google-vertexai'
3+
import { buildGoogleCredentials } from '../../../src/google-utils'
34
import {
45
ICommonObject,
56
IMultiModalOption,
@@ -10,7 +11,7 @@ import {
1011
IVisionChatModal
1112
} from '../../../src/Interface'
1213
import { getModels, getRegions, MODEL_TYPE } from '../../../src/modelLoader'
13-
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
14+
import { getBaseClasses } from '../../../src/utils'
1415

1516
const DEFAULT_IMAGE_MAX_TOKEN = 8192
1617
const DEFAULT_IMAGE_MODEL = 'gemini-1.5-flash-latest'
@@ -184,27 +185,6 @@ class GoogleVertexAI_ChatModels implements INode {
184185
}
185186

186187
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
187-
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
188-
const googleApplicationCredentialFilePath = getCredentialParam('googleApplicationCredentialFilePath', credentialData, nodeData)
189-
const googleApplicationCredential = getCredentialParam('googleApplicationCredential', credentialData, nodeData)
190-
const projectID = getCredentialParam('projectID', credentialData, nodeData)
191-
192-
const authOptions: ICommonObject = {}
193-
if (Object.keys(credentialData).length !== 0) {
194-
if (!googleApplicationCredentialFilePath && !googleApplicationCredential)
195-
throw new Error('Please specify your Google Application Credential')
196-
if (!googleApplicationCredentialFilePath && !googleApplicationCredential)
197-
throw new Error(
198-
'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'
199-
)
200-
if (googleApplicationCredentialFilePath && !googleApplicationCredential)
201-
authOptions.keyFile = googleApplicationCredentialFilePath
202-
else if (!googleApplicationCredentialFilePath && googleApplicationCredential)
203-
authOptions.credentials = JSON.parse(googleApplicationCredential)
204-
205-
if (projectID) authOptions.projectId = projectID
206-
}
207-
208188
const temperature = nodeData.inputs?.temperature as string
209189
const modelName = nodeData.inputs?.modelName as string
210190
const customModelName = nodeData.inputs?.customModelName as string
@@ -229,7 +209,10 @@ class GoogleVertexAI_ChatModels implements INode {
229209
modelName: customModelName || modelName,
230210
streaming: streaming ?? true
231211
}
232-
if (Object.keys(authOptions).length !== 0) obj.authOptions = authOptions
212+
213+
const authOptions = await buildGoogleCredentials(nodeData, options)
214+
if (authOptions && Object.keys(authOptions).length !== 0) obj.authOptions = authOptions
215+
233216
if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
234217
if (topP) obj.topP = parseFloat(topP)
235218
if (cache) obj.cache = cache

packages/components/nodes/embeddings/GoogleVertexAIEmbedding/GoogleVertexAIEmbedding.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { GoogleVertexAIEmbeddingsInput, VertexAIEmbeddings } from '@langchain/google-vertexai'
2+
import { buildGoogleCredentials } from '../../../src/google-utils'
23
import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
34
import { MODEL_TYPE, getModels, getRegions } from '../../../src/modelLoader'
4-
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
5+
import { getBaseClasses } from '../../../src/utils'
56

67
class GoogleVertexAIEmbedding_Embeddings implements INode {
78
label: string
@@ -63,33 +64,16 @@ class GoogleVertexAIEmbedding_Embeddings implements INode {
6364
}
6465

6566
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
66-
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
6767
const modelName = nodeData.inputs?.modelName as string
6868
const region = nodeData.inputs?.region as string
69-
const googleApplicationCredentialFilePath = getCredentialParam('googleApplicationCredentialFilePath', credentialData, nodeData)
70-
const googleApplicationCredential = getCredentialParam('googleApplicationCredential', credentialData, nodeData)
71-
const projectID = getCredentialParam('projectID', credentialData, nodeData)
7269

73-
const authOptions: any = {}
74-
if (Object.keys(credentialData).length !== 0) {
75-
if (!googleApplicationCredentialFilePath && !googleApplicationCredential)
76-
throw new Error('Please specify your Google Application Credential')
77-
if (!googleApplicationCredentialFilePath && !googleApplicationCredential)
78-
throw new Error(
79-
'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'
80-
)
81-
82-
if (googleApplicationCredentialFilePath && !googleApplicationCredential)
83-
authOptions.keyFile = googleApplicationCredentialFilePath
84-
else if (!googleApplicationCredentialFilePath && googleApplicationCredential)
85-
authOptions.credentials = JSON.parse(googleApplicationCredential)
86-
87-
if (projectID) authOptions.projectId = projectID
88-
}
8970
const obj: GoogleVertexAIEmbeddingsInput = {
9071
model: modelName
9172
}
92-
if (Object.keys(authOptions).length !== 0) obj.authOptions = authOptions
73+
74+
const authOptions = await buildGoogleCredentials(nodeData, options)
75+
if (authOptions && Object.keys(authOptions).length !== 0) obj.authOptions = authOptions
76+
9377
if (region) obj.location = region
9478

9579
const model = new VertexAIEmbeddings(obj)

packages/components/nodes/llms/GoogleVertexAI/GoogleVertexAI.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { BaseCache } from '@langchain/core/caches'
22
import { VertexAI, VertexAIInput } from '@langchain/google-vertexai'
33
import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
4-
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
4+
import { getBaseClasses } from '../../../src/utils'
55
import { getModels, MODEL_TYPE } from '../../../src/modelLoader'
6+
import { buildGoogleCredentials } from '../../../src/google-utils'
67

78
class GoogleVertexAI_LLMs implements INode {
89
label: string
@@ -83,28 +84,6 @@ class GoogleVertexAI_LLMs implements INode {
8384
}
8485

8586
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
86-
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
87-
const googleApplicationCredentialFilePath = getCredentialParam('googleApplicationCredentialFilePath', credentialData, nodeData)
88-
const googleApplicationCredential = getCredentialParam('googleApplicationCredential', credentialData, nodeData)
89-
const projectID = getCredentialParam('projectID', credentialData, nodeData)
90-
91-
const authOptions: any = {}
92-
if (Object.keys(credentialData).length !== 0) {
93-
if (!googleApplicationCredentialFilePath && !googleApplicationCredential)
94-
throw new Error('Please specify your Google Application Credential')
95-
if (!googleApplicationCredentialFilePath && !googleApplicationCredential)
96-
throw new Error(
97-
'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'
98-
)
99-
100-
if (googleApplicationCredentialFilePath && !googleApplicationCredential)
101-
authOptions.keyFile = googleApplicationCredentialFilePath
102-
else if (!googleApplicationCredentialFilePath && googleApplicationCredential)
103-
authOptions.credentials = JSON.parse(googleApplicationCredential)
104-
105-
if (projectID) authOptions.projectId = projectID
106-
}
107-
10887
const temperature = nodeData.inputs?.temperature as string
10988
const modelName = nodeData.inputs?.modelName as string
11089
const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string
@@ -115,7 +94,9 @@ class GoogleVertexAI_LLMs implements INode {
11594
temperature: parseFloat(temperature),
11695
model: modelName
11796
}
118-
if (Object.keys(authOptions).length !== 0) obj.authOptions = authOptions
97+
98+
const authOptions = await buildGoogleCredentials(nodeData, options)
99+
if (authOptions && Object.keys(authOptions).length !== 0) obj.authOptions = authOptions
119100

120101
if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
121102
if (topP) obj.topP = parseFloat(topP)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { getCredentialData, getCredentialParam, type ICommonObject, type INodeData } from '.'
2+
import type { ChatVertexAIInput, VertexAIInput } from '@langchain/google-vertexai'
3+
4+
type SupportedAuthOptions = ChatVertexAIInput['authOptions'] | VertexAIInput['authOptions']
5+
6+
export const buildGoogleCredentials = async (nodeData: INodeData, options: ICommonObject): Promise<SupportedAuthOptions | null> => {
7+
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
8+
const googleApplicationCredentialFilePath = getCredentialParam('googleApplicationCredentialFilePath', credentialData, nodeData)
9+
const googleApplicationCredential = getCredentialParam('googleApplicationCredential', credentialData, nodeData)
10+
const projectID = getCredentialParam('projectID', credentialData, nodeData)
11+
12+
const authOptions: any = {}
13+
if (Object.keys(credentialData).length !== 0) {
14+
if (!googleApplicationCredentialFilePath && !googleApplicationCredential)
15+
throw new Error('Please specify your Google Application Credential')
16+
if (!googleApplicationCredentialFilePath && !googleApplicationCredential)
17+
throw new Error(
18+
'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'
19+
)
20+
21+
if (googleApplicationCredentialFilePath && !googleApplicationCredential) authOptions.keyFile = googleApplicationCredentialFilePath
22+
else if (!googleApplicationCredentialFilePath && googleApplicationCredential)
23+
authOptions.credentials = JSON.parse(googleApplicationCredential)
24+
25+
if (projectID) authOptions.projectId = projectID
26+
}
27+
28+
return authOptions
29+
}

0 commit comments

Comments
 (0)