first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
+228
@@ -0,0 +1,228 @@
|
||||
import { ProjectsClient } from '@google-cloud/resource-manager';
|
||||
import type { GoogleAISafetySetting } from '@langchain/google-common';
|
||||
import { ChatVertexAI, type ChatVertexAIInput } from '@langchain/google-vertexai';
|
||||
import { formatPrivateKey } from 'n8n-nodes-base/dist/utils/utilities';
|
||||
import {
|
||||
NodeConnectionTypes,
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
type ISupplyDataFunctions,
|
||||
type SupplyData,
|
||||
type ILoadOptionsFunctions,
|
||||
type JsonObject,
|
||||
NodeOperationError,
|
||||
validateNodeParameters,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { makeErrorFromStatus } from './error-handling';
|
||||
import { getAdditionalOptions } from '../gemini-common/additional-options';
|
||||
import {
|
||||
makeN8nLlmFailedAttemptHandler,
|
||||
N8nLlmTracing,
|
||||
getConnectionHintNoticeField,
|
||||
} from '@n8n/ai-utilities';
|
||||
|
||||
export class LmChatGoogleVertex implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Google Vertex Chat Model',
|
||||
|
||||
name: 'lmChatGoogleVertex',
|
||||
icon: 'file:google.svg',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Chat Model Google Vertex',
|
||||
defaults: {
|
||||
name: 'Google Vertex Chat Model',
|
||||
},
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Language Models', 'Root Nodes'],
|
||||
'Language Models': ['Chat Models (Recommended)'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.lmchatgooglevertex/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
inputs: [],
|
||||
|
||||
outputs: [NodeConnectionTypes.AiLanguageModel],
|
||||
outputNames: ['Model'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'googleApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
getConnectionHintNoticeField([NodeConnectionTypes.AiChain, NodeConnectionTypes.AiAgent]),
|
||||
{
|
||||
displayName: 'Project ID',
|
||||
name: 'projectId',
|
||||
type: 'resourceLocator',
|
||||
default: { mode: 'list', value: '' },
|
||||
required: true,
|
||||
description: 'Select or enter your Google Cloud project ID',
|
||||
modes: [
|
||||
{
|
||||
displayName: 'From List',
|
||||
name: 'list',
|
||||
type: 'list',
|
||||
typeOptions: {
|
||||
searchListMethod: 'gcpProjectsList',
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'ID',
|
||||
name: 'id',
|
||||
type: 'string',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Model Name',
|
||||
name: 'modelName',
|
||||
type: 'string',
|
||||
description:
|
||||
'The model which will generate the completion. <a href="https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models">Learn more</a>.',
|
||||
default: 'gemini-2.5-flash',
|
||||
},
|
||||
getAdditionalOptions({ supportsThinkingBudget: true }),
|
||||
],
|
||||
};
|
||||
|
||||
methods = {
|
||||
listSearch: {
|
||||
async gcpProjectsList(this: ILoadOptionsFunctions) {
|
||||
const results: Array<{ name: string; value: string }> = [];
|
||||
|
||||
const credentials = await this.getCredentials('googleApi');
|
||||
const privateKey = formatPrivateKey(credentials.privateKey as string);
|
||||
const email = (credentials.email as string).trim();
|
||||
|
||||
const client = new ProjectsClient({
|
||||
credentials: {
|
||||
client_email: email,
|
||||
private_key: privateKey,
|
||||
},
|
||||
});
|
||||
|
||||
const [projects] = await client.searchProjects();
|
||||
|
||||
for (const project of projects) {
|
||||
if (project.projectId) {
|
||||
results.push({
|
||||
name: project.displayName ?? project.projectId,
|
||||
value: project.projectId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { results };
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
const credentials = await this.getCredentials('googleApi');
|
||||
const privateKey = formatPrivateKey(credentials.privateKey as string);
|
||||
const email = (credentials.email as string).trim();
|
||||
const region = credentials.region as string;
|
||||
|
||||
const modelName = this.getNodeParameter('modelName', itemIndex) as string;
|
||||
|
||||
const projectId = this.getNodeParameter('projectId', itemIndex, '', {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
|
||||
const options = this.getNodeParameter('options', itemIndex, {
|
||||
maxOutputTokens: 2048,
|
||||
temperature: 0.4,
|
||||
topK: 40,
|
||||
topP: 0.9,
|
||||
});
|
||||
|
||||
// Validate options parameter
|
||||
validateNodeParameters(
|
||||
options,
|
||||
{
|
||||
maxOutputTokens: { type: 'number', required: false },
|
||||
temperature: { type: 'number', required: false },
|
||||
topK: { type: 'number', required: false },
|
||||
topP: { type: 'number', required: false },
|
||||
thinkingBudget: { type: 'number', required: false },
|
||||
},
|
||||
this.getNode(),
|
||||
);
|
||||
|
||||
const safetySettings = this.getNodeParameter(
|
||||
'options.safetySettings.values',
|
||||
itemIndex,
|
||||
null,
|
||||
) as GoogleAISafetySetting[];
|
||||
|
||||
try {
|
||||
const modelConfig: ChatVertexAIInput = {
|
||||
authOptions: {
|
||||
projectId,
|
||||
credentials: {
|
||||
client_email: email,
|
||||
private_key: privateKey,
|
||||
},
|
||||
},
|
||||
location: region,
|
||||
model: modelName,
|
||||
topK: options.topK,
|
||||
topP: options.topP,
|
||||
temperature: options.temperature,
|
||||
maxOutputTokens: options.maxOutputTokens,
|
||||
safetySettings,
|
||||
callbacks: [new N8nLlmTracing(this)],
|
||||
// Handle ChatVertexAI invocation errors to provide better error messages
|
||||
onFailedAttempt: makeN8nLlmFailedAttemptHandler(this, (error: any) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
const customError = makeErrorFromStatus(Number(error?.response?.status), {
|
||||
modelName,
|
||||
});
|
||||
|
||||
if (customError) {
|
||||
throw new NodeOperationError(this.getNode(), error as JsonObject, customError);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}),
|
||||
};
|
||||
|
||||
// Add thinkingBudget if specified
|
||||
if (options.thinkingBudget !== undefined) {
|
||||
modelConfig.thinkingBudget = options.thinkingBudget;
|
||||
}
|
||||
|
||||
const model = new ChatVertexAI(modelConfig);
|
||||
|
||||
return {
|
||||
response: model,
|
||||
};
|
||||
} catch (e) {
|
||||
// Catch model name validation error from LangChain (https://github.com/langchain-ai/langchainjs/blob/ef201d0ee85ee4049078270a0cfd7a1767e624f8/libs/langchain-google-common/src/utils/common.ts#L124)
|
||||
// to show more helpful error message
|
||||
if (e?.message?.startsWith('Unable to verify model params')) {
|
||||
throw new NodeOperationError(this.getNode(), e as JsonObject, {
|
||||
message: 'Unsupported model',
|
||||
description: "Only models starting with 'gemini' are supported.",
|
||||
});
|
||||
}
|
||||
|
||||
// Assume all other exceptions while creating a new ChatVertexAI instance are parameter validation errors
|
||||
throw new NodeOperationError(this.getNode(), e as JsonObject, {
|
||||
message: 'Invalid options',
|
||||
description: e.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
export interface ErrorLike {
|
||||
message?: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export interface ErrorContext {
|
||||
modelName?: string;
|
||||
}
|
||||
|
||||
export function makeErrorFromStatus(statusCode: number, context?: ErrorContext): ErrorLike {
|
||||
const errorMessages: Record<number, ErrorLike> = {
|
||||
403: {
|
||||
message: 'Unauthorized for this project',
|
||||
description:
|
||||
'Check your Google Cloud project ID, that your credential has access to that project and that billing is enabled',
|
||||
},
|
||||
404: {
|
||||
message: context?.modelName
|
||||
? `No model found called '${context.modelName}'`
|
||||
: 'No model found',
|
||||
},
|
||||
};
|
||||
|
||||
return errorMessages[statusCode];
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 48 48"><defs><path id="a" d="M44.5 20H24v8.5h11.8C34.7 33.9 30.1 37 24 37c-7.2 0-13-5.8-13-13s5.8-13 13-13c3.1 0 5.9 1.1 8.1 2.9l6.4-6.4C34.6 4.1 29.6 2 24 2 11.8 2 2 11.8 2 24s9.8 22 22 22c11 0 21-8 21-22 0-1.3-.2-2.7-.5-4"/></defs><clipPath id="b"><use xlink:href="#a" overflow="visible"/></clipPath><path fill="#FBBC05" d="M0 37V11l17 13z" clip-path="url(#b)"/><path fill="#EA4335" d="m0 11 17 13 7-6.1L48 14V0H0z" clip-path="url(#b)"/><path fill="#34A853" d="m0 37 30-23 7.9 1L48 0v48H0z" clip-path="url(#b)"/><path fill="#4285F4" d="M48 48 17 24l-4-3 35-10z" clip-path="url(#b)"/></svg>
|
||||
|
After Width: | Height: | Size: 687 B |
+147
@@ -0,0 +1,147 @@
|
||||
import { ChatVertexAI } from '@langchain/google-vertexai';
|
||||
import { makeN8nLlmFailedAttemptHandler, N8nLlmTracing } from '@n8n/ai-utilities';
|
||||
import { createMockExecuteFunction } from 'n8n-nodes-base/test/nodes/Helpers';
|
||||
import type { INode, ISupplyDataFunctions } from 'n8n-workflow';
|
||||
|
||||
import { LmChatGoogleVertex } from '../LmChatGoogleVertex.node';
|
||||
|
||||
jest.mock('@langchain/google-vertexai');
|
||||
jest.mock('@n8n/ai-utilities');
|
||||
jest.mock('n8n-nodes-base/dist/utils/utilities', () => ({
|
||||
formatPrivateKey: jest.fn().mockImplementation((key: string) => key),
|
||||
}));
|
||||
|
||||
const MockedChatVertexAI = jest.mocked(ChatVertexAI);
|
||||
const MockedN8nLlmTracing = jest.mocked(N8nLlmTracing);
|
||||
const mockedMakeN8nLlmFailedAttemptHandler = jest.mocked(makeN8nLlmFailedAttemptHandler);
|
||||
|
||||
describe('LmChatGoogleVertex - Thinking Budget', () => {
|
||||
let lmChatGoogleVertex: LmChatGoogleVertex;
|
||||
let mockContext: jest.Mocked<ISupplyDataFunctions>;
|
||||
|
||||
const mockNode: INode = {
|
||||
id: '1',
|
||||
name: 'Google Vertex Chat Model',
|
||||
typeVersion: 1,
|
||||
type: 'n8n-nodes-langchain.lmChatGoogleVertex',
|
||||
position: [0, 0],
|
||||
parameters: {},
|
||||
};
|
||||
|
||||
const setupMockContext = () => {
|
||||
mockContext = createMockExecuteFunction<ISupplyDataFunctions>(
|
||||
{},
|
||||
mockNode,
|
||||
) as jest.Mocked<ISupplyDataFunctions>;
|
||||
|
||||
mockContext.getCredentials = jest.fn().mockResolvedValue({
|
||||
privateKey: 'test-private-key',
|
||||
email: 'test@n8n.io',
|
||||
region: 'us-central1',
|
||||
});
|
||||
mockContext.getNode = jest.fn().mockReturnValue(mockNode);
|
||||
mockContext.getNodeParameter = jest.fn();
|
||||
|
||||
MockedN8nLlmTracing.mockImplementation(() => ({}) as unknown as N8nLlmTracing);
|
||||
mockedMakeN8nLlmFailedAttemptHandler.mockReturnValue(jest.fn());
|
||||
|
||||
return mockContext;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
lmChatGoogleVertex = new LmChatGoogleVertex();
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('supplyData - thinking budget parameter passing', () => {
|
||||
it('should not include thinkingBudget in model config when not specified', async () => {
|
||||
const mockContext = setupMockContext();
|
||||
|
||||
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
|
||||
if (paramName === 'modelName') return 'gemini-2.5-flash';
|
||||
if (paramName === 'projectId') return 'test-project';
|
||||
if (paramName === 'options') {
|
||||
// Return options without thinkingBudget
|
||||
return {
|
||||
maxOutputTokens: 2048,
|
||||
temperature: 0.4,
|
||||
topK: 40,
|
||||
topP: 0.9,
|
||||
};
|
||||
}
|
||||
if (paramName === 'options.safetySettings.values') return null;
|
||||
return undefined;
|
||||
});
|
||||
|
||||
await lmChatGoogleVertex.supplyData.call(mockContext, 0);
|
||||
expect(MockedChatVertexAI).toHaveBeenCalledTimes(1);
|
||||
const callArgs = MockedChatVertexAI.mock.calls[0][0];
|
||||
expect(callArgs).not.toHaveProperty('thinkingBudget');
|
||||
expect(callArgs).toMatchObject({
|
||||
authOptions: {
|
||||
projectId: 'test-project',
|
||||
credentials: {
|
||||
client_email: 'test@n8n.io',
|
||||
private_key: 'test-private-key',
|
||||
},
|
||||
},
|
||||
location: 'us-central1',
|
||||
model: 'gemini-2.5-flash',
|
||||
topK: 40,
|
||||
topP: 0.9,
|
||||
temperature: 0.4,
|
||||
maxOutputTokens: 2048,
|
||||
});
|
||||
});
|
||||
|
||||
it('should include thinkingBudget in model config when specified', async () => {
|
||||
const mockContext = setupMockContext();
|
||||
const expectedThinkingBudget = 1024;
|
||||
|
||||
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
|
||||
if (paramName === 'modelName') return 'gemini-2.5-flash';
|
||||
if (paramName === 'projectId') return 'test-project';
|
||||
if (paramName === 'options') {
|
||||
// Return options with thinkingBudget
|
||||
return {
|
||||
maxOutputTokens: 2048,
|
||||
temperature: 0.4,
|
||||
topK: 40,
|
||||
topP: 0.9,
|
||||
thinkingBudget: expectedThinkingBudget,
|
||||
};
|
||||
}
|
||||
if (paramName === 'options.safetySettings.values') return null;
|
||||
return undefined;
|
||||
});
|
||||
|
||||
await lmChatGoogleVertex.supplyData.call(mockContext, 0);
|
||||
expect(MockedChatVertexAI).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
authOptions: {
|
||||
projectId: 'test-project',
|
||||
credentials: {
|
||||
client_email: 'test@n8n.io',
|
||||
private_key: 'test-private-key',
|
||||
},
|
||||
},
|
||||
location: 'us-central1',
|
||||
model: 'gemini-2.5-flash',
|
||||
topK: 40,
|
||||
topP: 0.9,
|
||||
temperature: 0.4,
|
||||
maxOutputTokens: 2048,
|
||||
thinkingBudget: expectedThinkingBudget,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
callbacks: expect.arrayContaining([expect.any(Object)]),
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
onFailedAttempt: expect.any(Function),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user