Refactor google credentials into a shared function (#4893)
This commit is contained in:
parent
a3f47af027
commit
00342bde88
|
|
@ -1,5 +1,6 @@
|
||||||
import { BaseCache } from '@langchain/core/caches'
|
import { BaseCache } from '@langchain/core/caches'
|
||||||
import { ChatVertexAIInput, ChatVertexAI as LcChatVertexAI } from '@langchain/google-vertexai'
|
import { ChatVertexAIInput, ChatVertexAI as LcChatVertexAI } from '@langchain/google-vertexai'
|
||||||
|
import { buildGoogleCredentials } from '../../../src/google-utils'
|
||||||
import {
|
import {
|
||||||
ICommonObject,
|
ICommonObject,
|
||||||
IMultiModalOption,
|
IMultiModalOption,
|
||||||
|
|
@ -10,7 +11,7 @@ import {
|
||||||
IVisionChatModal
|
IVisionChatModal
|
||||||
} from '../../../src/Interface'
|
} from '../../../src/Interface'
|
||||||
import { getModels, getRegions, MODEL_TYPE } from '../../../src/modelLoader'
|
import { getModels, getRegions, MODEL_TYPE } from '../../../src/modelLoader'
|
||||||
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
import { getBaseClasses } from '../../../src/utils'
|
||||||
|
|
||||||
const DEFAULT_IMAGE_MAX_TOKEN = 8192
|
const DEFAULT_IMAGE_MAX_TOKEN = 8192
|
||||||
const DEFAULT_IMAGE_MODEL = 'gemini-1.5-flash-latest'
|
const DEFAULT_IMAGE_MODEL = 'gemini-1.5-flash-latest'
|
||||||
|
|
@ -184,27 +185,6 @@ class GoogleVertexAI_ChatModels implements INode {
|
||||||
}
|
}
|
||||||
|
|
||||||
async init(nodeData: INodeData, _: string, options: ICommonObject): 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)
|
|
||||||
|
|
||||||
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 temperature = nodeData.inputs?.temperature as string
|
||||||
const modelName = nodeData.inputs?.modelName as string
|
const modelName = nodeData.inputs?.modelName as string
|
||||||
const customModelName = nodeData.inputs?.customModelName as string
|
const customModelName = nodeData.inputs?.customModelName as string
|
||||||
|
|
@ -229,7 +209,10 @@ class GoogleVertexAI_ChatModels implements INode {
|
||||||
modelName: customModelName || modelName,
|
modelName: customModelName || modelName,
|
||||||
streaming: streaming ?? true
|
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 (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
|
||||||
if (topP) obj.topP = parseFloat(topP)
|
if (topP) obj.topP = parseFloat(topP)
|
||||||
if (cache) obj.cache = cache
|
if (cache) obj.cache = cache
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import { GoogleVertexAIEmbeddingsInput, VertexAIEmbeddings } 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 { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
|
||||||
import { MODEL_TYPE, getModels, getRegions } from '../../../src/modelLoader'
|
import { MODEL_TYPE, getModels, getRegions } from '../../../src/modelLoader'
|
||||||
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
import { getBaseClasses } from '../../../src/utils'
|
||||||
|
|
||||||
class GoogleVertexAIEmbedding_Embeddings implements INode {
|
class GoogleVertexAIEmbedding_Embeddings implements INode {
|
||||||
label: string
|
label: string
|
||||||
|
|
@ -63,33 +64,16 @@ class GoogleVertexAIEmbedding_Embeddings implements INode {
|
||||||
}
|
}
|
||||||
|
|
||||||
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
||||||
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
|
||||||
const modelName = nodeData.inputs?.modelName as string
|
const modelName = nodeData.inputs?.modelName as string
|
||||||
const region = nodeData.inputs?.region as string
|
const region = nodeData.inputs?.region as string
|
||||||
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 obj: GoogleVertexAIEmbeddingsInput = {
|
const obj: GoogleVertexAIEmbeddingsInput = {
|
||||||
model: modelName
|
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
|
if (region) obj.location = region
|
||||||
|
|
||||||
const model = new VertexAIEmbeddings(obj)
|
const model = new VertexAIEmbeddings(obj)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
import { BaseCache } from '@langchain/core/caches'
|
import { BaseCache } from '@langchain/core/caches'
|
||||||
import { VertexAI, VertexAIInput } from '@langchain/google-vertexai'
|
import { VertexAI, VertexAIInput } from '@langchain/google-vertexai'
|
||||||
import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
|
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 { getModels, MODEL_TYPE } from '../../../src/modelLoader'
|
||||||
|
import { buildGoogleCredentials } from '../../../src/google-utils'
|
||||||
|
|
||||||
class GoogleVertexAI_LLMs implements INode {
|
class GoogleVertexAI_LLMs implements INode {
|
||||||
label: string
|
label: string
|
||||||
|
|
@ -83,28 +84,6 @@ class GoogleVertexAI_LLMs implements INode {
|
||||||
}
|
}
|
||||||
|
|
||||||
async init(nodeData: INodeData, _: string, options: ICommonObject): 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)
|
|
||||||
|
|
||||||
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 temperature = nodeData.inputs?.temperature as string
|
||||||
const modelName = 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
|
||||||
|
|
@ -115,7 +94,9 @@ class GoogleVertexAI_LLMs implements INode {
|
||||||
temperature: parseFloat(temperature),
|
temperature: parseFloat(temperature),
|
||||||
model: modelName
|
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 (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
|
||||||
if (topP) obj.topP = parseFloat(topP)
|
if (topP) obj.topP = parseFloat(topP)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue