n8n-nodes-google-vertex-embeddings-extended
Version:
n8n community sub-node for Google Vertex AI Embeddings with output dimensions and configurable batch size support - resolves LangChain compatibility issues
267 lines • 11.8 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbeddingsGoogleVertexExtended = void 0;
const google_vertexai_1 = require("@langchain/google-vertexai");
const logWrapper_1 = require("../../utils/logWrapper");
class EmbeddingsGoogleVertexExtended {
constructor() {
this.description = {
displayName: 'Embeddings Google Vertex Extended',
name: 'embeddingsGoogleVertexExtended',
group: ['transform'],
version: 1,
description: 'Use Google Vertex AI Embeddings with output dimensions support',
defaults: {
name: 'Embeddings Google Vertex Extended',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Embeddings'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.embeddingsgooglevertex/',
},
],
},
},
credentials: [
{
name: 'googleApi',
required: true,
},
],
inputs: [],
outputs: ["ai_embedding"],
outputNames: ['Embeddings'],
properties: [
{
displayName: 'Project ID',
name: 'projectId',
type: 'options',
default: '',
typeOptions: {
loadOptionsMethod: 'getProjects',
},
description: 'The Google Cloud project ID',
required: true,
},
{
displayName: 'Model Name',
name: 'model',
type: 'string',
description: 'The model to use for generating embeddings. <a href="https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api">Learn more</a>.',
default: 'text-embedding-004',
placeholder: 'e.g. text-embedding-004, text-multilingual-embedding-002',
},
{
displayName: 'Output Dimensions',
name: 'outputDimensions',
type: 'number',
default: 0,
description: 'The number of dimensions for the output embeddings. Set to 0 to use the model default. Only supported by certain models like text-embedding-004.',
},
{
displayName: 'Batch Size',
name: 'batchSize',
type: 'number',
default: 1,
description: 'Number of documents to process per API request. Vertex AI currently requires batchSize=1 for embedDocuments.',
typeOptions: {
minValue: 1,
maxValue: 100,
},
},
{
displayName: 'Options',
name: 'options',
placeholder: 'Add Option',
description: 'Additional options',
type: 'collection',
default: {},
options: [
{
displayName: 'Region',
name: 'region',
type: 'string',
default: 'us-central1',
description: 'The region where the model is deployed',
},
{
displayName: 'Task Type',
name: 'taskType',
type: 'options',
default: 'RETRIEVAL_DOCUMENT',
description: 'The type of task for which the embeddings will be used',
options: [
{
name: 'Retrieval Document',
value: 'RETRIEVAL_DOCUMENT',
},
{
name: 'Retrieval Query',
value: 'RETRIEVAL_QUERY',
},
{
name: 'Semantic Similarity',
value: 'SEMANTIC_SIMILARITY',
},
{
name: 'Classification',
value: 'CLASSIFICATION',
},
{
name: 'Clustering',
value: 'CLUSTERING',
},
],
},
],
},
],
};
this.methods = {
loadOptions: {
async getProjects() {
const credentials = await this.getCredentials('googleApi');
const { GoogleAuth } = await Promise.resolve().then(() => __importStar(require('google-auth-library')));
const email = credentials.email;
const privateKey = credentials.privateKey.replace(/\\n/g, '\n');
const auth = new GoogleAuth({
credentials: {
client_email: email,
private_key: privateKey,
},
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
try {
const client = await auth.getClient();
const accessToken = await client.getAccessToken();
const response = await fetch('https://cloudresourcemanager.googleapis.com/v1/projects', {
headers: {
'Authorization': `Bearer ${accessToken.token}`,
},
});
if (!response.ok) {
throw new Error('Failed to fetch projects');
}
const data = await response.json();
const projects = data.projects || [];
return projects.map((project) => ({
name: project.name || project.projectId,
value: project.projectId,
}));
}
catch (error) {
console.error('Error fetching projects:', error);
return [];
}
},
},
};
}
async supplyData() {
console.log('GoogleVertexEmbeddings: supplyData called!');
const credentials = await this.getCredentials('googleApi');
const projectId = this.getNodeParameter('projectId', 0);
const modelName = this.getNodeParameter('model', 0);
const outputDimensions = this.getNodeParameter('outputDimensions', 0, 0);
const batchSize = this.getNodeParameter('batchSize', 0, 1);
const options = this.getNodeParameter('options', 0, {});
const region = options.region || 'us-central1';
const privateKey = credentials.privateKey.replace(/\\n/g, '\n');
const baseEmbeddings = new google_vertexai_1.VertexAIEmbeddings({
authOptions: {
projectId,
credentials: {
client_email: credentials.email,
private_key: privateKey,
},
},
location: region,
model: modelName,
...(outputDimensions > 0 && { outputDimensionality: outputDimensions }),
...(options.taskType && { taskType: options.taskType }),
});
class BatchAwareVertexAIEmbeddings {
constructor(baseEmbeddings, batchSize) {
this.baseEmbeddings = baseEmbeddings;
this.batchSize = batchSize;
}
async embedQuery(document) {
return this.baseEmbeddings.embedQuery(document);
}
async embedDocuments(documents) {
if (this.batchSize === 1) {
const embeddings = [];
for (const doc of documents) {
const embedding = await this.baseEmbeddings.embedQuery(doc);
embeddings.push(embedding);
}
return embeddings;
}
else {
try {
return await this.baseEmbeddings.embedDocuments(documents);
}
catch (error) {
console.warn('Batch processing failed, falling back to single document processing:', error);
const embeddings = [];
for (const doc of documents) {
const embedding = await this.baseEmbeddings.embedQuery(doc);
embeddings.push(embedding);
}
return embeddings;
}
}
}
get modelName() {
return this.baseEmbeddings.modelName || 'google-vertex-ai';
}
}
const embeddings = new BatchAwareVertexAIEmbeddings(baseEmbeddings, batchSize);
console.log('GoogleVertexEmbeddings: About to wrap embeddings with logWrapper');
const wrappedEmbeddings = (0, logWrapper_1.logWrapper)(embeddings, this);
console.log('GoogleVertexEmbeddings: Wrapped embeddings created');
return {
response: wrappedEmbeddings,
};
}
}
exports.EmbeddingsGoogleVertexExtended = EmbeddingsGoogleVertexExtended;
//# sourceMappingURL=EmbeddingsGoogleVertexExtended.node.js.map