Add Google Vertex AI credential support to GoogleVertexAI node

This commit is contained in:
Yongtae 2023-08-08 13:06:38 +09:00
parent 812077c2aa
commit f7894ed4bf
3 changed files with 88 additions and 8 deletions

View File

@ -0,0 +1,55 @@
import { INodeParams, INodeCredential } from '../src/Interface'
class GoogleVertexAuth implements INodeCredential {
label: string
name: string
version: number
inputs: INodeParams[]
constructor() {
this.label = 'Google Vertex Auth'
this.name = 'googleVertexAuth'
this.version = 1.0
this.inputs = [
{
label: 'Google Application Credential File Path',
name: 'googleApplicationCredentialFilePath',
description:
'Path to your google application credential json file. You can also use the credential JSON object (either one)',
placeholder: 'your-path/application_default_credentials.json',
type: 'string',
optional: true
},
{
label: 'Google Credential JSON Object',
name: 'googleApplicationCredential',
description: 'JSON object of your google application credential. You can also use the file path (either one)',
placeholder: `{
"type": ...,
"project_id": ...,
"private_key_id": ...,
"private_key": ...,
"client_email": ...,
"client_id": ...,
"auth_uri": ...,
"token_uri": ...,
"auth_provider_x509_cert_url": ...,
"client_x509_cert_url": ...
}`,
type: 'string',
rows: 4,
optional: true
},
{
label: 'Project ID',
name: 'projectID',
description: 'Project ID of GCP. If not provided, it will be read from the credential file',
type: 'string',
optional: true,
additionalParams: true
}
]
}
}
module.exports = { credClass: GoogleVertexAuth }

View File

@ -1,6 +1,7 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface' import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils' import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { ChatGoogleVertexAI, GoogleVertexAIChatInput } from 'langchain/chat_models/googlevertexai' import { ChatGoogleVertexAI, GoogleVertexAIChatInput } from 'langchain/chat_models/googlevertexai'
import { GoogleAuthOptions } from 'google-auth-library'
class GoogleVertexAI_ChatModels implements INode { class GoogleVertexAI_ChatModels implements INode {
label: string label: string
@ -23,6 +24,12 @@ class GoogleVertexAI_ChatModels implements INode {
this.category = 'Chat Models' this.category = 'Chat Models'
this.description = 'Wrapper around VertexAI large language models that use the Chat endpoint' this.description = 'Wrapper around VertexAI large language models that use the Chat endpoint'
this.baseClasses = [this.type, ...getBaseClasses(ChatGoogleVertexAI)] this.baseClasses = [this.type, ...getBaseClasses(ChatGoogleVertexAI)]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['googleVertexAuth']
}
this.inputs = [ this.inputs = [
{ {
label: 'Model Name', label: 'Model Name',
@ -68,22 +75,40 @@ class GoogleVertexAI_ChatModels implements INode {
] ]
} }
async init(nodeData: INodeData, _: string): Promise<any> { 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)
if (!googleApplicationCredentialFilePath && !googleApplicationCredential)
throw new Error('Please specify your Google Application Credential')
if (googleApplicationCredentialFilePath && googleApplicationCredential)
throw new Error('Please use either Google Application Credential File Path or Google Credential JSON Object')
const authOptions: GoogleAuthOptions = {}
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 temperature = nodeData.inputs?.temperature as string
const model = nodeData.inputs?.modelName as string const modelName = nodeData.inputs?.modelName as string
const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string
const topP = nodeData.inputs?.topP as string const topP = nodeData.inputs?.topP as string
const obj: Partial<GoogleVertexAIChatInput> = { const obj: Partial<GoogleVertexAIChatInput> = {
temperature: parseFloat(temperature), temperature: parseFloat(temperature),
model model: modelName,
authOptions
} }
if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10) if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
if (topP) obj.topP = parseFloat(topP) if (topP) obj.topP = parseFloat(topP)
const chat_model = new ChatGoogleVertexAI(obj) const model = new ChatGoogleVertexAI(obj)
return chat_model return model
} }
} }

View File

@ -39,7 +39,7 @@
"google-auth-library": "^9.0.0", "google-auth-library": "^9.0.0",
"graphql": "^16.6.0", "graphql": "^16.6.0",
"html-to-text": "^9.0.5", "html-to-text": "^9.0.5",
"langchain": "^0.0.117", "langchain": "^0.0.122",
"linkifyjs": "^4.1.1", "linkifyjs": "^4.1.1",
"mammoth": "^1.5.1", "mammoth": "^1.5.1",
"moment": "^2.29.3", "moment": "^2.29.3",