Add Google Vertex Auth credential and make necessary changes for GoogleVertexAI

This commit is contained in:
Yongtae
2023-08-08 12:46:12 +09:00
parent 701005ecb3
commit 7d6828a361
3 changed files with 87 additions and 8 deletions
@@ -1,6 +1,7 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { GoogleVertexAI, GoogleVertexAITextInput } from 'langchain/llms/googlevertexai'
import { GoogleAuthOptions } from 'google-auth-library'
class GoogleVertexAI_LLMs implements INode {
label: string
@@ -23,6 +24,12 @@ class GoogleVertexAI_LLMs implements INode {
this.category = 'LLMs'
this.description = 'Wrapper around GoogleVertexAI large language models'
this.baseClasses = [this.type, ...getBaseClasses(GoogleVertexAI)]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['googleVertexAuth']
}
this.inputs = [
{
label: 'Model Name',
@@ -71,22 +78,39 @@ class GoogleVertexAI_LLMs 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 model = nodeData.inputs?.modelName as string
const modelName = nodeData.inputs?.modelName as string
const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string
const topP = nodeData.inputs?.topP as string
const obj: Partial<GoogleVertexAITextInput> = {
temperature: parseFloat(temperature),
model
model: modelName,
authOptions
}
if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
if (topP) obj.topP = parseFloat(topP)
const llm_model = new GoogleVertexAI(obj)
return llm_model
const model = new GoogleVertexAI(obj)
return model
}
}