|
| 1 | +/* eslint-disable n8n-nodes-base/node-dirname-against-convention */ |
| 2 | +import { ProjectsClient } from '@google-cloud/resource-manager'; |
| 3 | +import { VertexAIEmbeddings } from '@langchain/google-vertexai'; |
| 4 | +import { formatPrivateKey } from 'n8n-nodes-base/dist/utils/utilities'; |
| 5 | +import { NodeConnectionTypes } from 'n8n-workflow'; |
| 6 | +import type { |
| 7 | + ILoadOptionsFunctions, |
| 8 | + INodeType, |
| 9 | + INodeTypeDescription, |
| 10 | + ISupplyDataFunctions, |
| 11 | + SupplyData, |
| 12 | +} from 'n8n-workflow'; |
| 13 | + |
| 14 | +import { logWrapper } from '@utils/logWrapper'; |
| 15 | +import { getConnectionHintNoticeField } from '@utils/sharedFields'; |
| 16 | + |
| 17 | +export class EmbeddingsGoogleVertex implements INodeType { |
| 18 | + methods = { |
| 19 | + listSearch: { |
| 20 | + async gcpProjectsList(this: ILoadOptionsFunctions) { |
| 21 | + const results: Array<{ name: string; value: string }> = []; |
| 22 | + |
| 23 | + const credentials = await this.getCredentials('googleApi'); |
| 24 | + const privateKey = formatPrivateKey(credentials.privateKey as string); |
| 25 | + const email = (credentials.email as string).trim(); |
| 26 | + |
| 27 | + const client = new ProjectsClient({ |
| 28 | + credentials: { |
| 29 | + client_email: email, |
| 30 | + private_key: privateKey, |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + const [projects] = await client.searchProjects(); |
| 35 | + |
| 36 | + for (const project of projects) { |
| 37 | + if (project.projectId) { |
| 38 | + results.push({ |
| 39 | + name: project.displayName ?? project.projectId, |
| 40 | + value: project.projectId, |
| 41 | + }); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return { results }; |
| 46 | + }, |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + description: INodeTypeDescription = { |
| 51 | + displayName: 'Embeddings Google Vertex', |
| 52 | + name: 'embeddingsGoogleVertex', |
| 53 | + icon: 'file:google.svg', |
| 54 | + group: ['transform'], |
| 55 | + version: 1, |
| 56 | + description: 'Use Google Vertex Embeddings', |
| 57 | + defaults: { |
| 58 | + name: 'Embeddings Google Vertex', |
| 59 | + }, |
| 60 | + requestDefaults: { |
| 61 | + ignoreHttpStatusErrors: true, |
| 62 | + baseURL: '={{ $credentials.host }}', |
| 63 | + }, |
| 64 | + credentials: [ |
| 65 | + { |
| 66 | + name: 'googleApi', |
| 67 | + required: true, |
| 68 | + }, |
| 69 | + ], |
| 70 | + codex: { |
| 71 | + categories: ['AI'], |
| 72 | + subcategories: { |
| 73 | + AI: ['Embeddings'], |
| 74 | + }, |
| 75 | + resources: { |
| 76 | + primaryDocumentation: [ |
| 77 | + { |
| 78 | + url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.embeddingsgooglevertex/', |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + }, |
| 83 | + // eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node |
| 84 | + inputs: [], |
| 85 | + // eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong |
| 86 | + outputs: [NodeConnectionTypes.AiEmbedding], |
| 87 | + outputNames: ['Embeddings'], |
| 88 | + |
| 89 | + properties: [ |
| 90 | + getConnectionHintNoticeField([NodeConnectionTypes.AiVectorStore]), |
| 91 | + { |
| 92 | + displayName: |
| 93 | + 'Each model is using different dimensional density for embeddings. Please make sure to use the same dimensionality for your vector store. The default model is using 768-dimensional embeddings. You can find available models <a href="https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api">here</a>.', |
| 94 | + name: 'notice', |
| 95 | + type: 'notice', |
| 96 | + default: '', |
| 97 | + }, |
| 98 | + { |
| 99 | + displayName: 'Project ID', |
| 100 | + name: 'projectId', |
| 101 | + type: 'resourceLocator', |
| 102 | + default: { mode: 'list', value: '' }, |
| 103 | + required: true, |
| 104 | + description: 'Select or enter your Google Cloud project ID', |
| 105 | + modes: [ |
| 106 | + { |
| 107 | + displayName: 'From List', |
| 108 | + name: 'list', |
| 109 | + type: 'list', |
| 110 | + typeOptions: { |
| 111 | + searchListMethod: 'gcpProjectsList', |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + displayName: 'ID', |
| 116 | + name: 'id', |
| 117 | + type: 'string', |
| 118 | + }, |
| 119 | + ], |
| 120 | + }, |
| 121 | + { |
| 122 | + displayName: 'Model Name', |
| 123 | + name: 'modelName', |
| 124 | + type: 'string', |
| 125 | + description: |
| 126 | + 'The model which will generate the embeddings. <a href="https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api">Learn more</a>.', |
| 127 | + default: 'text-embedding-005', |
| 128 | + }, |
| 129 | + ], |
| 130 | + }; |
| 131 | + |
| 132 | + async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> { |
| 133 | + const credentials = await this.getCredentials('googleApi'); |
| 134 | + const privateKey = formatPrivateKey(credentials.privateKey as string); |
| 135 | + const email = (credentials.email as string).trim(); |
| 136 | + const region = credentials.region as string; |
| 137 | + |
| 138 | + const modelName = this.getNodeParameter('modelName', itemIndex) as string; |
| 139 | + |
| 140 | + const projectId = this.getNodeParameter('projectId', itemIndex, '', { |
| 141 | + extractValue: true, |
| 142 | + }) as string; |
| 143 | + |
| 144 | + const embeddings = new VertexAIEmbeddings({ |
| 145 | + authOptions: { |
| 146 | + projectId, |
| 147 | + credentials: { |
| 148 | + client_email: email, |
| 149 | + private_key: privateKey, |
| 150 | + }, |
| 151 | + }, |
| 152 | + location: region, |
| 153 | + model: modelName, |
| 154 | + }); |
| 155 | + |
| 156 | + return { |
| 157 | + response: logWrapper(embeddings, this), |
| 158 | + }; |
| 159 | + } |
| 160 | +} |
0 commit comments