Skip to content

Commit

Permalink
Merge pull request #713 from Yongtae723/feature/add_vertexAI
Browse files Browse the repository at this point in the history
Feature/add vertex ai
  • Loading branch information
HenryHengZJ authored Aug 9, 2023
2 parents b4f3d70 + 74abaa3 commit b386743
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/components/credentials/GoogleAuth.credential.ts
Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { ChatGoogleVertexAI, GoogleVertexAIChatInput } from 'langchain/chat_models/googlevertexai'
import { GoogleAuthOptions } from 'google-auth-library'

class GoogleVertexAI_ChatModels implements INode {
label: string
name: string
version: number
type: string
icon: string
category: string
description: string
baseClasses: string[]
credential: INodeParams
inputs: INodeParams[]

constructor() {
this.label = 'ChatGoogleVertexAI'
this.name = 'chatGoogleVertexAI'
this.version = 1.0
this.type = 'ChatGoogleVertexAI'
this.icon = 'vertexai.svg'
this.category = 'Chat Models'
this.description = 'Wrapper around VertexAI large language models that use the Chat endpoint'
this.baseClasses = [this.type, ...getBaseClasses(ChatGoogleVertexAI)]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['googleVertexAuth']
}
this.inputs = [
{
label: 'Model Name',
name: 'modelName',
type: 'options',
options: [
{
label: 'chat-bison',
name: 'chat-bison'
},
{
label: 'codechat-bison',
name: 'codechat-bison'
}
],
default: 'chat-bison',
optional: true
},
{
label: 'Temperature',
name: 'temperature',
type: 'number',
step: 0.1,
default: 0.9,
optional: true
},
{
label: 'Max Output Tokens',
name: 'maxOutputTokens',
type: 'number',
step: 1,
optional: true,
additionalParams: true
},
{
label: 'Top Probability',
name: 'topP',
type: 'number',
step: 0.1,
optional: true,
additionalParams: true
}
]
}

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 modelName = nodeData.inputs?.modelName as string
const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string
const topP = nodeData.inputs?.topP as string

const obj: Partial<GoogleVertexAIChatInput> = {
temperature: parseFloat(temperature),
model: modelName,
authOptions
}

if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
if (topP) obj.topP = parseFloat(topP)

const model = new ChatGoogleVertexAI(obj)
return model
}
}

module.exports = { nodeClass: GoogleVertexAI_ChatModels }
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { GoogleVertexAIEmbeddings, GoogleVertexAIEmbeddingsParams } from 'langchain/embeddings/googlevertexai'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { GoogleAuthOptions } from 'google-auth-library'

class GoogleVertexAIEmbedding_Embeddings implements INode {
label: string
name: string
version: number
type: string
icon: string
category: string
description: string
baseClasses: string[]
credential: INodeParams
inputs: INodeParams[]

constructor() {
this.label = 'GoogleVertexAI Embeddings'
this.name = 'googlevertexaiEmbeddings'
this.version = 1.0
this.type = 'GoogleVertexAIEmbeddings'
this.icon = 'vertexai.svg'
this.category = 'Embeddings'
this.description = 'Google vertexAI API to generate embeddings for a given text'
this.baseClasses = [this.type, ...getBaseClasses(GoogleVertexAIEmbeddings)]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['googleVertexAuth']
}
this.inputs = []
}

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 obj: GoogleVertexAIEmbeddingsParams = {
authOptions
}

const model = new GoogleVertexAIEmbeddings(obj)
return model
}
}

module.exports = { nodeClass: GoogleVertexAIEmbedding_Embeddings }
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
117 changes: 117 additions & 0 deletions packages/components/nodes/llms/GoogleVertexAI/GoogleVertexAI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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
name: string
version: number
type: string
icon: string
category: string
description: string
baseClasses: string[]
credential: INodeParams
inputs: INodeParams[]

constructor() {
this.label = 'GoogleVertexAI'
this.name = 'googlevertexai'
this.version = 1.0
this.type = 'GoogleVertexAI'
this.icon = 'vertexai.svg'
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',
name: 'modelName',
type: 'options',
options: [
{
label: 'text-bison',
name: 'text-bison'
},
{
label: 'code-bison',
name: 'code-bison'
},
{
label: 'code-gecko',
name: 'code-gecko'
}
],
default: 'text-bison'
},
{
label: 'Temperature',
name: 'temperature',
type: 'number',
step: 0.1,
default: 0.7,
optional: true
},
{
label: 'max Output Tokens',
name: 'maxOutputTokens',
type: 'number',
step: 1,
optional: true,
additionalParams: true
},
{
label: 'Top Probability',
name: 'topP',
type: 'number',
step: 0.1,
optional: true,
additionalParams: true
}
]
}

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 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: modelName,
authOptions
}

if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
if (topP) obj.topP = parseFloat(topP)

const model = new GoogleVertexAI(obj)
return model
}
}

module.exports = { nodeClass: GoogleVertexAI_LLMs }
2 changes: 2 additions & 0 deletions packages/components/nodes/llms/GoogleVertexAI/vertexai.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b386743

Please sign in to comment.