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

This commit is contained in:
2026-03-17 16:22:57 +03:30
commit 3d5eaf9445
15349 changed files with 2847338 additions and 0 deletions
@@ -0,0 +1,26 @@
import type { INodeProperties } from 'n8n-workflow';
export const modelRLC: INodeProperties = {
displayName: 'Model',
name: 'modelId',
type: 'resourceLocator',
default: { mode: 'list', value: '' },
required: true,
modes: [
{
displayName: 'From List',
name: 'list',
type: 'list',
typeOptions: {
searchListMethod: 'modelSearch',
searchable: true,
},
},
{
displayName: 'ID',
name: 'id',
type: 'string',
placeholder: 'e.g. llava, llama3.2-vision',
},
],
};
@@ -0,0 +1,456 @@
import type { IExecuteFunctions, INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { updateDisplayOptions } from 'n8n-workflow';
import type { OllamaChatResponse, OllamaMessage } from '../../helpers';
import { apiRequest } from '../../transport';
import { modelRLC } from '../descriptions';
const properties: INodeProperties[] = [
modelRLC,
{
displayName: 'Text Input',
name: 'text',
type: 'string',
placeholder: "e.g. What's in this image?",
default: "What's in this image?",
typeOptions: {
rows: 2,
},
},
{
displayName: 'Input Type',
name: 'inputType',
type: 'options',
default: 'binary',
options: [
{
name: 'Binary File(s)',
value: 'binary',
},
{
name: 'Image URL(s)',
value: 'url',
},
],
},
{
displayName: 'Input Data Field Name(s)',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
placeholder: 'e.g. data',
hint: 'The name of the input field containing the binary file data to be processed',
description:
'Name of the binary field(s) which contains the image(s), separate multiple field names with commas',
displayOptions: {
show: {
inputType: ['binary'],
},
},
},
{
displayName: 'URL(s)',
name: 'imageUrls',
type: 'string',
placeholder: 'e.g. https://example.com/image.png',
description: 'URL(s) of the image(s) to analyze, multiple URLs can be added separated by comma',
default: '',
displayOptions: {
show: {
inputType: ['url'],
},
},
},
{
displayName: 'Simplify Output',
name: 'simplify',
type: 'boolean',
default: true,
description: 'Whether to simplify the response or not',
},
{
displayName: 'Options',
name: 'options',
placeholder: 'Add Option',
type: 'collection',
default: {},
options: [
{
displayName: 'System Message',
name: 'system',
type: 'string',
default: '',
placeholder: 'e.g. You are a helpful assistant.',
description: 'System message to set the context for the conversation',
typeOptions: {
rows: 2,
},
},
{
displayName: 'Temperature',
name: 'temperature',
type: 'number',
default: 0.8,
typeOptions: {
minValue: 0,
maxValue: 2,
numberPrecision: 2,
},
description: 'Controls randomness in responses. Lower values make output more focused.',
},
{
displayName: 'Output Randomness (Top P)',
name: 'top_p',
default: 0.7,
description: 'The maximum cumulative probability of tokens to consider when sampling',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 1,
numberPrecision: 1,
},
},
{
displayName: 'Top K',
name: 'top_k',
type: 'number',
default: 40,
typeOptions: {
minValue: 1,
},
description: 'Controls diversity by limiting the number of top tokens to consider',
},
{
displayName: 'Max Tokens',
name: 'num_predict',
type: 'number',
default: 1024,
typeOptions: {
minValue: 1,
numberPrecision: 0,
},
description: 'Maximum number of tokens to generate in the completion',
},
{
displayName: 'Frequency Penalty',
name: 'frequency_penalty',
type: 'number',
default: 0.0,
typeOptions: {
minValue: 0,
numberPrecision: 2,
},
description:
'Adjusts the penalty for tokens that have already appeared in the generated text. Higher values discourage repetition.',
},
{
displayName: 'Presence Penalty',
name: 'presence_penalty',
type: 'number',
default: 0.0,
typeOptions: {
numberPrecision: 2,
},
description:
'Adjusts the penalty for tokens based on their presence in the generated text so far. Positive values penalize tokens that have already appeared, encouraging diversity.',
},
{
displayName: 'Repetition Penalty',
name: 'repeat_penalty',
type: 'number',
default: 1.1,
typeOptions: {
minValue: 0,
numberPrecision: 2,
},
description:
'Sets how strongly to penalize repetitions. A higher value (e.g., 1.5) will penalize repetitions more strongly, while a lower value (e.g., 0.9) will be more lenient.',
},
{
displayName: 'Context Length',
name: 'num_ctx',
type: 'number',
default: 4096,
typeOptions: {
minValue: 1,
numberPrecision: 0,
},
description: 'Sets the size of the context window used to generate the next token',
},
{
displayName: 'Repeat Last N',
name: 'repeat_last_n',
type: 'number',
default: 64,
typeOptions: {
minValue: -1,
numberPrecision: 0,
},
description:
'Sets how far back for the model to look back to prevent repetition. (0 = disabled, -1 = num_ctx).',
},
{
displayName: 'Min P',
name: 'min_p',
type: 'number',
default: 0.0,
typeOptions: {
minValue: 0,
maxValue: 1,
numberPrecision: 3,
},
description:
'Alternative to the top_p, and aims to ensure a balance of quality and variety. The parameter p represents the minimum probability for a token to be considered, relative to the probability of the most likely token.',
},
{
displayName: 'Seed',
name: 'seed',
type: 'number',
default: 0,
typeOptions: {
minValue: 0,
numberPrecision: 0,
},
description:
'Sets the random number seed to use for generation. Setting this to a specific number will make the model generate the same text for the same prompt.',
},
{
displayName: 'Stop Sequences',
name: 'stop',
type: 'string',
default: '',
description:
'Sets the stop sequences to use. When this pattern is encountered the LLM will stop generating text and return. Separate multiple patterns with commas',
},
{
displayName: 'Keep Alive',
name: 'keep_alive',
type: 'string',
default: '5m',
description:
'Specifies the duration to keep the loaded model in memory after use. Format: 1h30m (1 hour 30 minutes).',
},
{
displayName: 'Low VRAM Mode',
name: 'low_vram',
type: 'boolean',
default: false,
description:
'Whether to activate low VRAM mode, which reduces memory usage at the cost of slower generation speed. Useful for GPUs with limited memory.',
},
{
displayName: 'Main GPU ID',
name: 'main_gpu',
type: 'number',
default: 0,
typeOptions: {
minValue: 0,
numberPrecision: 0,
},
description:
'Specifies the ID of the GPU to use for the main computation. Only change this if you have multiple GPUs.',
},
{
displayName: 'Context Batch Size',
name: 'num_batch',
type: 'number',
default: 512,
typeOptions: {
minValue: 1,
numberPrecision: 0,
},
description:
'Sets the batch size for prompt processing. Larger batch sizes may improve generation speed but increase memory usage.',
},
{
displayName: 'Number of GPUs',
name: 'num_gpu',
type: 'number',
default: -1,
typeOptions: {
minValue: -1,
numberPrecision: 0,
},
description:
'Specifies the number of GPUs to use for parallel processing. Set to -1 for auto-detection.',
},
{
displayName: 'Number of CPU Threads',
name: 'num_thread',
type: 'number',
default: 0,
typeOptions: {
minValue: 0,
numberPrecision: 0,
},
description:
'Specifies the number of CPU threads to use for processing. Set to 0 for auto-detection.',
},
{
displayName: 'Penalize Newlines',
name: 'penalize_newline',
type: 'boolean',
default: true,
description:
'Whether the model will be less likely to generate newline characters, encouraging longer continuous sequences of text',
},
{
displayName: 'Use Memory Locking',
name: 'use_mlock',
type: 'boolean',
default: false,
description:
'Whether to lock the model in memory to prevent swapping. This can improve performance but requires sufficient available memory.',
},
{
displayName: 'Use Memory Mapping',
name: 'use_mmap',
type: 'boolean',
default: true,
description:
'Whether to use memory mapping for loading the model. This can reduce memory usage but may impact performance.',
},
{
displayName: 'Load Vocabulary Only',
name: 'vocab_only',
type: 'boolean',
default: false,
description:
'Whether to only load the model vocabulary without the weights. Useful for quickly testing tokenization.',
},
{
displayName: 'Output Format',
name: 'format',
type: 'options',
options: [
{ name: 'Default', value: '' },
{ name: 'JSON', value: 'json' },
],
default: '',
description: 'Specifies the format of the API response',
},
],
},
];
interface MessageOptions {
system?: string;
temperature?: number;
top_p?: number;
top_k?: number;
num_predict?: number;
frequency_penalty?: number;
presence_penalty?: number;
repeat_penalty?: number;
num_ctx?: number;
repeat_last_n?: number;
min_p?: number;
seed?: number;
stop?: string | string[];
low_vram?: boolean;
main_gpu?: number;
num_batch?: number;
num_gpu?: number;
num_thread?: number;
penalize_newline?: boolean;
use_mlock?: boolean;
use_mmap?: boolean;
vocab_only?: boolean;
format?: string;
keep_alive?: string;
}
const displayOptions = {
show: {
operation: ['analyze'],
resource: ['image'],
},
};
export const description = updateDisplayOptions(displayOptions, properties);
export async function execute(this: IExecuteFunctions, i: number): Promise<INodeExecutionData[]> {
const model = this.getNodeParameter('modelId', i, '', { extractValue: true }) as string;
const inputType = this.getNodeParameter('inputType', i, 'binary') as string;
const text = this.getNodeParameter('text', i, '') as string;
const simplify = this.getNodeParameter('simplify', i, true) as boolean;
const options = this.getNodeParameter('options', i, {}) as MessageOptions;
let images: string[];
if (inputType === 'url') {
const urls = this.getNodeParameter('imageUrls', i, '') as string;
const urlList = urls
.split(',')
.map((url) => url.trim())
.filter((url) => url);
// For URL inputs, we need to download and convert to base64
const imagePromises = urlList.map(async (url) => {
const response = (await this.helpers.httpRequest({
method: 'GET',
url,
encoding: 'arraybuffer',
})) as Buffer;
return response.toString('base64');
});
images = await Promise.all(imagePromises);
} else {
const binaryPropertyNames = this.getNodeParameter('binaryPropertyName', i, 'data');
const propertyNames = binaryPropertyNames
.split(',')
.map((name: string) => name.trim())
.filter((name: string) => name);
const imagePromises = propertyNames.map(async (binaryPropertyName: string) => {
const buffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
return buffer.toString('base64');
});
images = await Promise.all(imagePromises);
}
const messages: OllamaMessage[] = [
{
role: 'user',
content: text,
images,
},
];
const processedOptions = { ...options };
if (processedOptions.stop && typeof processedOptions.stop === 'string') {
processedOptions.stop = processedOptions.stop
.split(',')
.map((s: string) => s.trim())
.filter(Boolean);
}
const body = {
model,
messages,
stream: false,
options: processedOptions,
};
const response: OllamaChatResponse = await apiRequest.call(this, 'POST', '/api/chat', {
body,
});
if (simplify) {
return [
{
json: { content: response.message.content },
pairedItem: { item: i },
},
];
}
return [
{
json: { ...response },
pairedItem: { item: i },
},
];
}
@@ -0,0 +1,29 @@
import type { INodeProperties } from 'n8n-workflow';
import * as analyze from './analyze.operation';
export { analyze };
export const description: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Analyze Image',
value: 'analyze',
action: 'Analyze image',
description: 'Take in images and answer questions about them',
},
],
default: 'analyze',
displayOptions: {
show: {
resource: ['image'],
},
},
},
...analyze.description,
];
@@ -0,0 +1,8 @@
import type { AllEntities } from 'n8n-workflow';
type NodeMap = {
text: 'message';
image: 'analyze';
};
export type OllamaType = AllEntities<NodeMap>;
@@ -0,0 +1,226 @@
import { mockDeep } from 'jest-mock-extended';
import { NodeOperationError, type IExecuteFunctions, type INode } from 'n8n-workflow';
import * as image from './image';
import * as text from './text';
import { router } from './router';
jest.mock('./image');
jest.mock('./text');
describe('Ollama Router', () => {
const executeFunctionsMock = mockDeep<IExecuteFunctions>();
const mockImageExecute = jest.fn();
const mockTextExecute = jest.fn();
beforeEach(() => {
jest.resetAllMocks();
(image as any).analyze = { execute: mockImageExecute };
(text as any).message = { execute: mockTextExecute };
executeFunctionsMock.getInputData.mockReturnValue([
{ json: { input: 'test1' } },
{ json: { input: 'test2' } },
]);
});
describe('router', () => {
it('should route to text.message operation', async () => {
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
if (parameter === 'resource') return 'text';
if (parameter === 'operation') return 'message';
return undefined;
});
mockTextExecute.mockResolvedValueOnce([
{ json: { result: 'response1' }, pairedItem: { item: 0 } },
]);
mockTextExecute.mockResolvedValueOnce([
{ json: { result: 'response2' }, pairedItem: { item: 1 } },
]);
const result = await router.call(executeFunctionsMock);
expect(result).toEqual([
[
{ json: { result: 'response1' }, pairedItem: { item: 0 } },
{ json: { result: 'response2' }, pairedItem: { item: 1 } },
],
]);
expect(mockTextExecute).toHaveBeenCalledTimes(2);
expect(mockTextExecute).toHaveBeenNthCalledWith(1, 0);
expect(mockTextExecute).toHaveBeenNthCalledWith(2, 1);
});
it('should route to image.analyze operation', async () => {
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
if (parameter === 'resource') return 'image';
if (parameter === 'operation') return 'analyze';
return undefined;
});
mockImageExecute.mockResolvedValueOnce([
{ json: { result: 'image analysis 1' }, pairedItem: { item: 0 } },
]);
mockImageExecute.mockResolvedValueOnce([
{ json: { result: 'image analysis 2' }, pairedItem: { item: 1 } },
]);
const result = await router.call(executeFunctionsMock);
expect(result).toEqual([
[
{ json: { result: 'image analysis 1' }, pairedItem: { item: 0 } },
{ json: { result: 'image analysis 2' }, pairedItem: { item: 1 } },
],
]);
expect(mockImageExecute).toHaveBeenCalledTimes(2);
expect(mockImageExecute).toHaveBeenNthCalledWith(1, 0);
expect(mockImageExecute).toHaveBeenNthCalledWith(2, 1);
});
it('should throw error for unsupported resource', async () => {
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
if (parameter === 'resource') return 'unsupported';
if (parameter === 'operation') return 'test';
return undefined;
});
const mockNode = { name: 'Ollama', type: 'n8n-nodes-langchain.ollama' } as INode;
executeFunctionsMock.getNode.mockReturnValue(mockNode);
await expect(router.call(executeFunctionsMock)).rejects.toThrow(NodeOperationError);
await expect(router.call(executeFunctionsMock)).rejects.toThrow(
'The resource "unsupported" is not supported!',
);
});
it('should handle execution errors with continueOnFail enabled', async () => {
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
if (parameter === 'resource') return 'text';
if (parameter === 'operation') return 'message';
return undefined;
});
executeFunctionsMock.continueOnFail.mockReturnValue(true);
mockTextExecute.mockResolvedValueOnce([
{ json: { result: 'success' }, pairedItem: { item: 0 } },
]);
mockTextExecute.mockRejectedValueOnce(new Error('API Error'));
const result = await router.call(executeFunctionsMock);
expect(result).toEqual([
[
{ json: { result: 'success' }, pairedItem: { item: 0 } },
{ json: { error: 'API Error' }, pairedItem: { item: 1 } },
],
]);
expect(mockTextExecute).toHaveBeenCalledTimes(2);
});
it('should throw NodeOperationError when continueOnFail is disabled', async () => {
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
if (parameter === 'resource') return 'text';
if (parameter === 'operation') return 'message';
return undefined;
});
executeFunctionsMock.continueOnFail.mockReturnValue(false);
const mockNode = { name: 'Ollama', type: 'n8n-nodes-langchain.ollama' } as INode;
executeFunctionsMock.getNode.mockReturnValue(mockNode);
const originalError = new Error('API Connection Failed');
mockTextExecute.mockRejectedValueOnce(originalError);
await expect(router.call(executeFunctionsMock)).rejects.toThrow(NodeOperationError);
});
it('should process multiple items and accumulate results', async () => {
executeFunctionsMock.getInputData.mockReturnValue([
{ json: { input: 'test1' } },
{ json: { input: 'test2' } },
{ json: { input: 'test3' } },
]);
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
if (parameter === 'resource') return 'text';
if (parameter === 'operation') return 'message';
return undefined;
});
mockTextExecute.mockResolvedValueOnce([
{ json: { result: 'response1' }, pairedItem: { item: 0 } },
]);
mockTextExecute.mockResolvedValueOnce([
{ json: { result: 'response2a' }, pairedItem: { item: 1 } },
{ json: { result: 'response2b' }, pairedItem: { item: 1 } },
]);
mockTextExecute.mockResolvedValueOnce([
{ json: { result: 'response3' }, pairedItem: { item: 2 } },
]);
const result = await router.call(executeFunctionsMock);
expect(result).toEqual([
[
{ json: { result: 'response1' }, pairedItem: { item: 0 } },
{ json: { result: 'response2a' }, pairedItem: { item: 1 } },
{ json: { result: 'response2b' }, pairedItem: { item: 1 } },
{ json: { result: 'response3' }, pairedItem: { item: 2 } },
],
]);
expect(mockTextExecute).toHaveBeenCalledTimes(3);
expect(mockTextExecute).toHaveBeenNthCalledWith(1, 0);
expect(mockTextExecute).toHaveBeenNthCalledWith(2, 1);
expect(mockTextExecute).toHaveBeenNthCalledWith(3, 2);
});
it('should handle empty input data', async () => {
executeFunctionsMock.getInputData.mockReturnValue([]);
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
if (parameter === 'resource') return 'text';
if (parameter === 'operation') return 'message';
return undefined;
});
const result = await router.call(executeFunctionsMock);
expect(result).toEqual([[]]);
expect(mockTextExecute).not.toHaveBeenCalled();
});
it('should handle mixed success and failure with continueOnFail', async () => {
executeFunctionsMock.getInputData.mockReturnValue([
{ json: { input: 'test1' } },
{ json: { input: 'test2' } },
{ json: { input: 'test3' } },
]);
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
if (parameter === 'resource') return 'text';
if (parameter === 'operation') return 'message';
return undefined;
});
executeFunctionsMock.continueOnFail.mockReturnValue(true);
mockTextExecute.mockResolvedValueOnce([
{ json: { result: 'success1' }, pairedItem: { item: 0 } },
]);
mockTextExecute.mockRejectedValueOnce(new Error('Error in item 2'));
mockTextExecute.mockResolvedValueOnce([
{ json: { result: 'success3' }, pairedItem: { item: 2 } },
]);
const result = await router.call(executeFunctionsMock);
expect(result).toEqual([
[
{ json: { result: 'success1' }, pairedItem: { item: 0 } },
{ json: { error: 'Error in item 2' }, pairedItem: { item: 1 } },
{ json: { result: 'success3' }, pairedItem: { item: 2 } },
],
]);
});
});
});
@@ -0,0 +1,49 @@
import { NodeOperationError, type IExecuteFunctions, type INodeExecutionData } from 'n8n-workflow';
import * as image from './image';
import type { OllamaType } from './node.type';
import * as text from './text';
export async function router(this: IExecuteFunctions) {
const returnData: INodeExecutionData[] = [];
const items = this.getInputData();
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const ollamaTypeData = {
resource,
operation,
} as OllamaType;
let execute;
switch (ollamaTypeData.resource) {
case 'image':
execute = image[ollamaTypeData.operation].execute;
break;
case 'text':
execute = text[ollamaTypeData.operation].execute;
break;
default:
throw new NodeOperationError(this.getNode(), `The resource "${resource}" is not supported!`);
}
for (let i = 0; i < items.length; i++) {
try {
const responseData = await execute.call(this, i);
returnData.push.apply(returnData, responseData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: i } });
continue;
}
throw new NodeOperationError(this.getNode(), error, {
itemIndex: i,
description: error.description,
});
}
}
return [returnData];
}
@@ -0,0 +1,29 @@
import type { INodeProperties } from 'n8n-workflow';
import * as message from './message.operation';
export { message };
export const description: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Message a Model',
value: 'message',
action: 'Message a model',
description: 'Send a message to Ollama model',
},
],
default: 'message',
displayOptions: {
show: {
resource: ['text'],
},
},
},
...message.description,
];
@@ -0,0 +1,489 @@
import type { Tool } from '@langchain/core/tools';
import type { IExecuteFunctions, INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { updateDisplayOptions } from 'n8n-workflow';
import { zodToJsonSchema } from 'zod-to-json-schema';
import { getConnectedTools } from '@utils/helpers';
import type { OllamaChatResponse, OllamaMessage, OllamaTool } from '../../helpers';
import { apiRequest } from '../../transport';
import { modelRLC } from '../descriptions';
const properties: INodeProperties[] = [
modelRLC,
{
displayName: 'Messages',
name: 'messages',
type: 'fixedCollection',
typeOptions: {
sortable: true,
multipleValues: true,
},
placeholder: 'Add Message',
default: { values: [{ content: '', role: 'user' }] },
options: [
{
displayName: 'Values',
name: 'values',
values: [
{
displayName: 'Content',
name: 'content',
type: 'string',
description: 'The content of the message to be sent',
default: '',
placeholder: 'e.g. Hello, how can you help me?',
typeOptions: {
rows: 2,
},
},
{
displayName: 'Role',
name: 'role',
type: 'options',
description: 'The role of this message in the conversation',
options: [
{
name: 'User',
value: 'user',
description: 'Message from the user',
},
{
name: 'Assistant',
value: 'assistant',
description: 'Response from the assistant (for conversation history)',
},
],
default: 'user',
},
],
},
],
},
{
displayName: 'Simplify Output',
name: 'simplify',
type: 'boolean',
default: true,
description: 'Whether to simplify the response or not',
},
{
displayName: 'Options',
name: 'options',
placeholder: 'Add Option',
type: 'collection',
default: {},
options: [
{
displayName: 'System Message',
name: 'system',
type: 'string',
default: '',
placeholder: 'e.g. You are a helpful assistant.',
description: 'System message to set the context for the conversation',
typeOptions: {
rows: 2,
},
},
{
displayName: 'Temperature',
name: 'temperature',
type: 'number',
default: 0.8,
typeOptions: {
minValue: 0,
maxValue: 2,
numberPrecision: 2,
},
description: 'Controls randomness in responses. Lower values make output more focused.',
},
{
displayName: 'Output Randomness (Top P)',
name: 'top_p',
default: 0.7,
description: 'The maximum cumulative probability of tokens to consider when sampling',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 1,
numberPrecision: 1,
},
},
{
displayName: 'Top K',
name: 'top_k',
type: 'number',
default: 40,
typeOptions: {
minValue: 1,
},
description: 'Controls diversity by limiting the number of top tokens to consider',
},
{
displayName: 'Max Tokens',
name: 'num_predict',
type: 'number',
default: 1024,
typeOptions: {
minValue: 1,
numberPrecision: 0,
},
description: 'Maximum number of tokens to generate in the completion',
},
{
displayName: 'Frequency Penalty',
name: 'frequency_penalty',
type: 'number',
default: 0.0,
typeOptions: {
minValue: 0,
numberPrecision: 2,
},
description:
'Adjusts the penalty for tokens that have already appeared in the generated text. Higher values discourage repetition.',
},
{
displayName: 'Presence Penalty',
name: 'presence_penalty',
type: 'number',
default: 0.0,
typeOptions: {
numberPrecision: 2,
},
description:
'Adjusts the penalty for tokens based on their presence in the generated text so far. Positive values penalize tokens that have already appeared, encouraging diversity.',
},
{
displayName: 'Repetition Penalty',
name: 'repeat_penalty',
type: 'number',
default: 1.1,
typeOptions: {
minValue: 0,
numberPrecision: 2,
},
description:
'Sets how strongly to penalize repetitions. A higher value (e.g., 1.5) will penalize repetitions more strongly, while a lower value (e.g., 0.9) will be more lenient.',
},
{
displayName: 'Context Length',
name: 'num_ctx',
type: 'number',
default: 4096,
typeOptions: {
minValue: 1,
numberPrecision: 0,
},
description: 'Sets the size of the context window used to generate the next token',
},
{
displayName: 'Repeat Last N',
name: 'repeat_last_n',
type: 'number',
default: 64,
typeOptions: {
minValue: -1,
numberPrecision: 0,
},
description:
'Sets how far back for the model to look back to prevent repetition. (0 = disabled, -1 = num_ctx).',
},
{
displayName: 'Min P',
name: 'min_p',
type: 'number',
default: 0.0,
typeOptions: {
minValue: 0,
maxValue: 1,
numberPrecision: 3,
},
description:
'Alternative to the top_p, and aims to ensure a balance of quality and variety. The parameter p represents the minimum probability for a token to be considered, relative to the probability of the most likely token.',
},
{
displayName: 'Seed',
name: 'seed',
type: 'number',
default: 0,
typeOptions: {
minValue: 0,
numberPrecision: 0,
},
description:
'Sets the random number seed to use for generation. Setting this to a specific number will make the model generate the same text for the same prompt.',
},
{
displayName: 'Stop Sequences',
name: 'stop',
type: 'string',
default: '',
description:
'Sets the stop sequences to use. When this pattern is encountered the LLM will stop generating text and return. Separate multiple patterns with commas',
},
{
displayName: 'Keep Alive',
name: 'keep_alive',
type: 'string',
default: '5m',
description:
'Specifies the duration to keep the loaded model in memory after use. Format: 1h30m (1 hour 30 minutes).',
},
{
displayName: 'Low VRAM Mode',
name: 'low_vram',
type: 'boolean',
default: false,
description:
'Whether to activate low VRAM mode, which reduces memory usage at the cost of slower generation speed. Useful for GPUs with limited memory.',
},
{
displayName: 'Main GPU ID',
name: 'main_gpu',
type: 'number',
default: 0,
typeOptions: {
minValue: 0,
numberPrecision: 0,
},
description:
'Specifies the ID of the GPU to use for the main computation. Only change this if you have multiple GPUs.',
},
{
displayName: 'Context Batch Size',
name: 'num_batch',
type: 'number',
default: 512,
typeOptions: {
minValue: 1,
numberPrecision: 0,
},
description:
'Sets the batch size for prompt processing. Larger batch sizes may improve generation speed but increase memory usage.',
},
{
displayName: 'Number of GPUs',
name: 'num_gpu',
type: 'number',
default: -1,
typeOptions: {
minValue: -1,
numberPrecision: 0,
},
description:
'Specifies the number of GPUs to use for parallel processing. Set to -1 for auto-detection.',
},
{
displayName: 'Number of CPU Threads',
name: 'num_thread',
type: 'number',
default: 0,
typeOptions: {
minValue: 0,
numberPrecision: 0,
},
description:
'Specifies the number of CPU threads to use for processing. Set to 0 for auto-detection.',
},
{
displayName: 'Penalize Newlines',
name: 'penalize_newline',
type: 'boolean',
default: true,
description:
'Whether the model will be less likely to generate newline characters, encouraging longer continuous sequences of text',
},
{
displayName: 'Use Memory Locking',
name: 'use_mlock',
type: 'boolean',
default: false,
description:
'Whether to lock the model in memory to prevent swapping. This can improve performance but requires sufficient available memory.',
},
{
displayName: 'Use Memory Mapping',
name: 'use_mmap',
type: 'boolean',
default: true,
description:
'Whether to use memory mapping for loading the model. This can reduce memory usage but may impact performance.',
},
{
displayName: 'Load Vocabulary Only',
name: 'vocab_only',
type: 'boolean',
default: false,
description:
'Whether to only load the model vocabulary without the weights. Useful for quickly testing tokenization.',
},
{
displayName: 'Output Format',
name: 'format',
type: 'options',
options: [
{ name: 'Default', value: '' },
{ name: 'JSON', value: 'json' },
],
default: '',
description: 'Specifies the format of the API response',
},
],
},
];
interface MessageOptions {
system?: string;
temperature?: number;
top_p?: number;
top_k?: number;
num_predict?: number;
frequency_penalty?: number;
presence_penalty?: number;
repeat_penalty?: number;
num_ctx?: number;
repeat_last_n?: number;
min_p?: number;
seed?: number;
stop?: string | string[];
low_vram?: boolean;
main_gpu?: number;
num_batch?: number;
num_gpu?: number;
num_thread?: number;
penalize_newline?: boolean;
use_mlock?: boolean;
use_mmap?: boolean;
vocab_only?: boolean;
format?: string;
keep_alive?: string;
}
const displayOptions = {
show: {
operation: ['message'],
resource: ['text'],
},
};
export const description = updateDisplayOptions(displayOptions, properties);
export async function execute(this: IExecuteFunctions, i: number): Promise<INodeExecutionData[]> {
const model = this.getNodeParameter('modelId', i, '', { extractValue: true }) as string;
const messages = this.getNodeParameter('messages.values', i, []) as OllamaMessage[];
const simplify = this.getNodeParameter('simplify', i, true) as boolean;
const options = this.getNodeParameter('options', i, {}) as MessageOptions;
const { tools, connectedTools } = await getTools.call(this);
if (options.system) {
messages.unshift({
role: 'system',
content: options.system,
});
}
delete options.system;
const processedOptions = { ...options };
if (processedOptions.stop && typeof processedOptions.stop === 'string') {
processedOptions.stop = processedOptions.stop
.split(',')
.map((s: string) => s.trim())
.filter(Boolean);
}
const body = {
model,
messages,
stream: false,
tools,
options: processedOptions,
};
let response: OllamaChatResponse = await apiRequest.call(this, 'POST', '/api/chat', {
body,
});
if (tools.length > 0 && response.message.tool_calls && response.message.tool_calls.length > 0) {
const toolCalls = response.message.tool_calls;
messages.push(response.message);
for (const toolCall of toolCalls) {
let toolResponse = '';
let toolFound = false;
for (const tool of connectedTools) {
if (tool.name === toolCall.function.name) {
toolFound = true;
try {
const result: unknown = await tool.invoke(toolCall.function.arguments);
toolResponse =
typeof result === 'object' && result !== null
? JSON.stringify(result)
: String(result);
} catch (error) {
toolResponse = `Error executing tool: ${error instanceof Error ? error.message : 'Unknown error'}`;
}
break;
}
}
// Add tool response even if tool wasn't found to prevent silent failure
if (!toolFound) {
toolResponse = `Error: Tool '${toolCall.function.name}' not found`;
}
messages.push({
role: 'tool',
content: toolResponse,
tool_name: toolCall.function.name,
});
}
const updatedBody = {
...body,
messages,
};
response = await apiRequest.call(this, 'POST', '/api/chat', {
body: updatedBody,
});
}
if (simplify) {
return [
{
json: { content: response.message.content },
pairedItem: { item: i },
},
];
}
return [
{
json: { ...response },
pairedItem: { item: i },
},
];
}
async function getTools(this: IExecuteFunctions) {
let connectedTools: Tool[] = [];
const nodeInputs = this.getNodeInputs();
if (nodeInputs.some((input) => input.type === 'ai_tool')) {
connectedTools = await getConnectedTools(this, true);
}
const tools: OllamaTool[] = connectedTools.map((tool) => ({
type: 'function',
function: {
name: tool.name,
description: tool.description,
parameters: zodToJsonSchema(tool.schema),
},
}));
return { tools, connectedTools };
}
@@ -0,0 +1,72 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
import { NodeConnectionTypes, type INodeTypeDescription } from 'n8n-workflow';
import * as image from './image';
import * as text from './text';
export const versionDescription: INodeTypeDescription = {
displayName: 'Ollama',
name: 'ollama',
icon: 'file:ollama.svg',
group: ['transform'],
version: 1,
subtitle: '={{ $parameter["operation"] + ": " + $parameter["resource"] }}',
description: 'Interact with Ollama AI models',
defaults: {
name: 'Ollama',
},
usableAsTool: true,
codex: {
alias: ['LangChain', 'image', 'vision', 'AI', 'local'],
categories: ['AI'],
subcategories: {
AI: ['Agents', 'Miscellaneous', 'Root Nodes'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-langchain.ollama/',
},
],
},
},
inputs: `={{
(() => {
const resource = $parameter.resource;
const operation = $parameter.operation;
if (resource === 'text' && operation === 'message') {
return [{ type: 'main' }, { type: 'ai_tool', displayName: 'Tools' }];
}
return ['main'];
})()
}}`,
outputs: [NodeConnectionTypes.Main],
credentials: [
{
name: 'ollamaApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Image',
value: 'image',
},
{
name: 'Text',
value: 'text',
},
],
default: 'text',
},
...image.description,
...text.description,
],
};