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:
@@ -0,0 +1,680 @@
|
||||
import { mockDeep } from 'jest-mock-extended';
|
||||
import type { IExecuteFunctions } from 'n8n-workflow';
|
||||
import { z } from 'zod';
|
||||
|
||||
import * as helpers from '@utils/helpers';
|
||||
|
||||
import * as image from './actions/image';
|
||||
import * as text from './actions/text';
|
||||
import * as transport from './transport';
|
||||
import type { OllamaChatResponse, OllamaMessage } from './helpers/interfaces';
|
||||
|
||||
describe('Ollama Node', () => {
|
||||
const executeFunctionsMock = mockDeep<IExecuteFunctions>();
|
||||
const apiRequestMock = jest.spyOn(transport, 'apiRequest');
|
||||
const getConnectedToolsMock = jest.spyOn(helpers, 'getConnectedTools');
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
describe('Text -> Message', () => {
|
||||
it('should call the API with correct parameters for basic message', async () => {
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llama3.2:latest';
|
||||
case 'messages.values':
|
||||
return [{ role: 'user', content: 'Hello, world!' }];
|
||||
case 'simplify':
|
||||
return true;
|
||||
case 'options':
|
||||
return {
|
||||
system: 'You are a helpful assistant.',
|
||||
temperature: 0.7,
|
||||
top_p: 0.9,
|
||||
top_k: 40,
|
||||
num_predict: 1024,
|
||||
};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
executeFunctionsMock.getNodeInputs.mockReturnValue([{ type: 'main' }]);
|
||||
getConnectedToolsMock.mockResolvedValue([]);
|
||||
apiRequestMock.mockResolvedValue({
|
||||
model: 'llama3.2:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: { role: 'assistant', content: 'Hello! How can I help you today?' },
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
const result = await text.message.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: { content: 'Hello! How can I help you today?' },
|
||||
pairedItem: { item: 0 },
|
||||
},
|
||||
]);
|
||||
expect(apiRequestMock).toHaveBeenCalledWith('POST', '/api/chat', {
|
||||
body: {
|
||||
model: 'llama3.2:latest',
|
||||
messages: [
|
||||
{ role: 'system', content: 'You are a helpful assistant.' },
|
||||
{ role: 'user', content: 'Hello, world!' },
|
||||
],
|
||||
stream: false,
|
||||
tools: [],
|
||||
options: {
|
||||
temperature: 0.7,
|
||||
top_p: 0.9,
|
||||
top_k: 40,
|
||||
num_predict: 1024,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should return full response when simplify is false', async () => {
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llama3.2:latest';
|
||||
case 'messages.values':
|
||||
return [{ role: 'user', content: 'Test message' }];
|
||||
case 'simplify':
|
||||
return false;
|
||||
case 'options':
|
||||
return {};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
executeFunctionsMock.getNodeInputs.mockReturnValue([{ type: 'main' }]);
|
||||
getConnectedToolsMock.mockResolvedValue([]);
|
||||
const mockResponse = {
|
||||
model: 'llama3.2:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: { role: 'assistant', content: 'Test response' },
|
||||
done: true,
|
||||
total_duration: 5000000,
|
||||
load_duration: 1000000,
|
||||
eval_count: 10,
|
||||
eval_duration: 2000000,
|
||||
} as OllamaChatResponse;
|
||||
apiRequestMock.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await text.message.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: mockResponse,
|
||||
pairedItem: { item: 0 },
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should handle tool calls correctly', async () => {
|
||||
const mockTool = {
|
||||
name: 'calculator',
|
||||
description: 'Performs calculations',
|
||||
schema: z.object({
|
||||
expression: z.string().describe('Mathematical expression to evaluate'),
|
||||
}),
|
||||
invoke: jest.fn().mockResolvedValue({ result: 42 }),
|
||||
};
|
||||
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llama3.2:latest';
|
||||
case 'messages.values':
|
||||
return [{ role: 'user', content: 'What is 6 * 7?' }];
|
||||
case 'simplify':
|
||||
return true;
|
||||
case 'options':
|
||||
return {};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
executeFunctionsMock.getNodeInputs.mockReturnValue([{ type: 'main' }, { type: 'ai_tool' }]);
|
||||
// @ts-expect-error: Mocking a tool, we do not implement the full interface
|
||||
getConnectedToolsMock.mockResolvedValue([mockTool]);
|
||||
|
||||
apiRequestMock.mockResolvedValueOnce({
|
||||
model: 'llama3.2:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
tool_calls: [
|
||||
{
|
||||
function: {
|
||||
name: 'calculator',
|
||||
arguments: { expression: '6 * 7' },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
apiRequestMock.mockResolvedValueOnce({
|
||||
model: 'llama3.2:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: { role: 'assistant', content: 'The result is 42.' },
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
const result = await text.message.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: { content: 'The result is 42.' },
|
||||
pairedItem: { item: 0 },
|
||||
},
|
||||
]);
|
||||
expect(mockTool.invoke).toHaveBeenCalledWith({ expression: '6 * 7' });
|
||||
expect(apiRequestMock).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should handle tool execution errors gracefully', async () => {
|
||||
const mockTool = {
|
||||
name: 'failing_tool',
|
||||
description: 'A tool that fails',
|
||||
schema: z.object({}),
|
||||
invoke: jest.fn().mockRejectedValue(new Error('Tool execution failed')),
|
||||
};
|
||||
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llama3.2:latest';
|
||||
case 'messages.values':
|
||||
return [{ role: 'user', content: 'Use the failing tool' }];
|
||||
case 'simplify':
|
||||
return true;
|
||||
case 'options':
|
||||
return {};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
executeFunctionsMock.getNodeInputs.mockReturnValue([{ type: 'main' }, { type: 'ai_tool' }]);
|
||||
// @ts-expect-error: Mocking a tool, we do not implement the full interface
|
||||
getConnectedToolsMock.mockResolvedValue([mockTool]);
|
||||
|
||||
apiRequestMock.mockResolvedValueOnce({
|
||||
model: 'llama3.2:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
tool_calls: [
|
||||
{
|
||||
function: {
|
||||
name: 'failing_tool',
|
||||
arguments: {},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
apiRequestMock.mockResolvedValueOnce({
|
||||
model: 'llama3.2:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: { role: 'assistant', content: 'I encountered an error with the tool.' },
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
const result = await text.message.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: { content: 'I encountered an error with the tool.' },
|
||||
pairedItem: { item: 0 },
|
||||
},
|
||||
]);
|
||||
|
||||
const secondCallBody = apiRequestMock.mock.calls[1][2]?.body as any;
|
||||
const toolMessage = secondCallBody.messages.find((msg: OllamaMessage) => msg.role === 'tool');
|
||||
expect(toolMessage.content).toBe('Error executing tool: Tool execution failed');
|
||||
});
|
||||
|
||||
it('should process stop sequences correctly', async () => {
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llama3.2:latest';
|
||||
case 'messages.values':
|
||||
return [{ role: 'user', content: 'Generate text' }];
|
||||
case 'simplify':
|
||||
return true;
|
||||
case 'options':
|
||||
return {
|
||||
stop: '###,END,STOP',
|
||||
};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
executeFunctionsMock.getNodeInputs.mockReturnValue([{ type: 'main' }]);
|
||||
getConnectedToolsMock.mockResolvedValue([]);
|
||||
apiRequestMock.mockResolvedValue({
|
||||
model: 'llama3.2:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: { role: 'assistant', content: 'Generated text' },
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
await text.message.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(apiRequestMock).toHaveBeenCalledWith('POST', '/api/chat', {
|
||||
body: {
|
||||
model: 'llama3.2:latest',
|
||||
messages: [{ role: 'user', content: 'Generate text' }],
|
||||
stream: false,
|
||||
tools: [],
|
||||
options: {
|
||||
stop: ['###', 'END', 'STOP'],
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle various model-specific options', async () => {
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llama3.2:latest';
|
||||
case 'messages.values':
|
||||
return [{ role: 'user', content: 'Test with options' }];
|
||||
case 'simplify':
|
||||
return true;
|
||||
case 'options':
|
||||
return {
|
||||
temperature: 0.5,
|
||||
top_p: 0.8,
|
||||
top_k: 30,
|
||||
num_predict: 512,
|
||||
frequency_penalty: 0.1,
|
||||
presence_penalty: 0.2,
|
||||
repeat_penalty: 1.2,
|
||||
num_ctx: 2048,
|
||||
repeat_last_n: 32,
|
||||
min_p: 0.1,
|
||||
seed: 123,
|
||||
low_vram: true,
|
||||
main_gpu: 1,
|
||||
num_batch: 256,
|
||||
num_gpu: 2,
|
||||
num_thread: 8,
|
||||
penalize_newline: false,
|
||||
use_mlock: true,
|
||||
use_mmap: false,
|
||||
vocab_only: false,
|
||||
keep_alive: '10m',
|
||||
format: 'json',
|
||||
};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
executeFunctionsMock.getNodeInputs.mockReturnValue([{ type: 'main' }]);
|
||||
getConnectedToolsMock.mockResolvedValue([]);
|
||||
apiRequestMock.mockResolvedValue({
|
||||
model: 'llama3.2:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: { role: 'assistant', content: '{"response": "test"}' },
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
await text.message.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(apiRequestMock).toHaveBeenCalledWith('POST', '/api/chat', {
|
||||
body: {
|
||||
model: 'llama3.2:latest',
|
||||
messages: [{ role: 'user', content: 'Test with options' }],
|
||||
stream: false,
|
||||
tools: [],
|
||||
options: {
|
||||
temperature: 0.5,
|
||||
top_p: 0.8,
|
||||
top_k: 30,
|
||||
num_predict: 512,
|
||||
frequency_penalty: 0.1,
|
||||
presence_penalty: 0.2,
|
||||
repeat_penalty: 1.2,
|
||||
num_ctx: 2048,
|
||||
repeat_last_n: 32,
|
||||
min_p: 0.1,
|
||||
seed: 123,
|
||||
low_vram: true,
|
||||
main_gpu: 1,
|
||||
num_batch: 256,
|
||||
num_gpu: 2,
|
||||
num_thread: 8,
|
||||
penalize_newline: false,
|
||||
use_mlock: true,
|
||||
use_mmap: false,
|
||||
vocab_only: false,
|
||||
keep_alive: '10m',
|
||||
format: 'json',
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Image -> Analyze', () => {
|
||||
it('should analyze image from binary data', async () => {
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llava:latest';
|
||||
case 'inputType':
|
||||
return 'binary';
|
||||
case 'binaryPropertyName':
|
||||
return 'data';
|
||||
case 'text':
|
||||
return "What's in this image?";
|
||||
case 'simplify':
|
||||
return true;
|
||||
case 'options':
|
||||
return {
|
||||
temperature: 0.3,
|
||||
num_predict: 512,
|
||||
};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
|
||||
executeFunctionsMock.helpers.getBinaryDataBuffer.mockResolvedValue(
|
||||
Buffer.from('test image data'),
|
||||
);
|
||||
apiRequestMock.mockResolvedValue({
|
||||
model: 'llava:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: 'This image shows a beautiful mountain landscape with snow-capped peaks.',
|
||||
},
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
const result = await image.analyze.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
content: 'This image shows a beautiful mountain landscape with snow-capped peaks.',
|
||||
},
|
||||
pairedItem: { item: 0 },
|
||||
},
|
||||
]);
|
||||
expect(apiRequestMock).toHaveBeenCalledWith('POST', '/api/chat', {
|
||||
body: {
|
||||
model: 'llava:latest',
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: "What's in this image?",
|
||||
images: ['dGVzdCBpbWFnZSBkYXRh'], // base64 encoded 'test image data'
|
||||
},
|
||||
],
|
||||
stream: false,
|
||||
options: {
|
||||
temperature: 0.3,
|
||||
num_predict: 512,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should analyze image from URL', async () => {
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llava:latest';
|
||||
case 'inputType':
|
||||
return 'url';
|
||||
case 'imageUrls':
|
||||
return 'https://example.com/test-image.jpg';
|
||||
case 'text':
|
||||
return 'Describe this image';
|
||||
case 'simplify':
|
||||
return true;
|
||||
case 'options':
|
||||
return {};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
|
||||
executeFunctionsMock.helpers.httpRequest.mockResolvedValue(
|
||||
Buffer.from('downloaded image data'),
|
||||
);
|
||||
apiRequestMock.mockResolvedValue({
|
||||
model: 'llava:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: 'This image contains a sunset over the ocean.',
|
||||
},
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
const result = await image.analyze.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: { content: 'This image contains a sunset over the ocean.' },
|
||||
pairedItem: { item: 0 },
|
||||
},
|
||||
]);
|
||||
expect(executeFunctionsMock.helpers.httpRequest).toHaveBeenCalledWith({
|
||||
method: 'GET',
|
||||
url: 'https://example.com/test-image.jpg',
|
||||
encoding: 'arraybuffer',
|
||||
});
|
||||
expect(apiRequestMock).toHaveBeenCalledWith('POST', '/api/chat', {
|
||||
body: {
|
||||
model: 'llava:latest',
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: 'Describe this image',
|
||||
images: ['ZG93bmxvYWRlZCBpbWFnZSBkYXRh'], // base64 encoded 'downloaded image data'
|
||||
},
|
||||
],
|
||||
stream: false,
|
||||
options: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle multiple images from URLs', async () => {
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llava:latest';
|
||||
case 'inputType':
|
||||
return 'url';
|
||||
case 'imageUrls':
|
||||
return 'https://example.com/image1.jpg, https://example.com/image2.png';
|
||||
case 'text':
|
||||
return 'Compare these images';
|
||||
case 'simplify':
|
||||
return true;
|
||||
case 'options':
|
||||
return {};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
|
||||
executeFunctionsMock.helpers.httpRequest.mockResolvedValueOnce(
|
||||
Buffer.from('first image data'),
|
||||
);
|
||||
executeFunctionsMock.helpers.httpRequest.mockResolvedValueOnce(
|
||||
Buffer.from('second image data'),
|
||||
);
|
||||
apiRequestMock.mockResolvedValue({
|
||||
model: 'llava:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: 'Both images show different landscapes.',
|
||||
},
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
const result = await image.analyze.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: { content: 'Both images show different landscapes.' },
|
||||
pairedItem: { item: 0 },
|
||||
},
|
||||
]);
|
||||
expect(executeFunctionsMock.helpers.httpRequest).toHaveBeenCalledTimes(2);
|
||||
expect(apiRequestMock).toHaveBeenCalledWith('POST', '/api/chat', {
|
||||
body: {
|
||||
model: 'llava:latest',
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: 'Compare these images',
|
||||
images: [
|
||||
'Zmlyc3QgaW1hZ2UgZGF0YQ==', // base64 encoded 'first image data'
|
||||
'c2Vjb25kIGltYWdlIGRhdGE=', // base64 encoded 'second image data'
|
||||
],
|
||||
},
|
||||
],
|
||||
stream: false,
|
||||
options: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle multiple binary images', async () => {
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llava:latest';
|
||||
case 'inputType':
|
||||
return 'binary';
|
||||
case 'binaryPropertyName':
|
||||
return 'image1,image2';
|
||||
case 'text':
|
||||
return 'Analyze these images';
|
||||
case 'simplify':
|
||||
return false;
|
||||
case 'options':
|
||||
return {};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
|
||||
executeFunctionsMock.helpers.getBinaryDataBuffer.mockResolvedValueOnce(
|
||||
Buffer.from('first binary image'),
|
||||
);
|
||||
executeFunctionsMock.helpers.getBinaryDataBuffer.mockResolvedValueOnce(
|
||||
Buffer.from('second binary image'),
|
||||
);
|
||||
const mockResponse = {
|
||||
model: 'llava:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: 'Analysis complete for both images.',
|
||||
},
|
||||
done: true,
|
||||
eval_count: 25,
|
||||
eval_duration: 3000000,
|
||||
} as OllamaChatResponse;
|
||||
apiRequestMock.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await image.analyze.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: mockResponse,
|
||||
pairedItem: { item: 0 },
|
||||
},
|
||||
]);
|
||||
expect(executeFunctionsMock.helpers.getBinaryDataBuffer).toHaveBeenCalledTimes(2);
|
||||
expect(executeFunctionsMock.helpers.getBinaryDataBuffer).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
0,
|
||||
'image1',
|
||||
);
|
||||
expect(executeFunctionsMock.helpers.getBinaryDataBuffer).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
0,
|
||||
'image2',
|
||||
);
|
||||
});
|
||||
|
||||
it('should process stop sequences for image analysis', async () => {
|
||||
executeFunctionsMock.getNodeParameter.mockImplementation((parameter: string) => {
|
||||
switch (parameter) {
|
||||
case 'modelId':
|
||||
return 'llava:latest';
|
||||
case 'inputType':
|
||||
return 'binary';
|
||||
case 'binaryPropertyName':
|
||||
return 'data';
|
||||
case 'text':
|
||||
return 'Describe briefly';
|
||||
case 'simplify':
|
||||
return true;
|
||||
case 'options':
|
||||
return {
|
||||
stop: 'END,DONE',
|
||||
temperature: 0.1,
|
||||
};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
|
||||
executeFunctionsMock.helpers.getBinaryDataBuffer.mockResolvedValue(Buffer.from('test image'));
|
||||
apiRequestMock.mockResolvedValue({
|
||||
model: 'llava:latest',
|
||||
created_at: '2023-10-01T10:00:00Z',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: 'A simple image.',
|
||||
},
|
||||
done: true,
|
||||
} as OllamaChatResponse);
|
||||
|
||||
await image.analyze.execute.call(executeFunctionsMock, 0);
|
||||
|
||||
expect(apiRequestMock).toHaveBeenCalledWith('POST', '/api/chat', {
|
||||
body: {
|
||||
model: 'llava:latest',
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: 'Describe briefly',
|
||||
images: ['dGVzdCBpbWFnZQ=='], // base64 encoded 'test image'
|
||||
},
|
||||
],
|
||||
stream: false,
|
||||
options: {
|
||||
stop: ['END', 'DONE'],
|
||||
temperature: 0.1,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import type { IExecuteFunctions, INodeType } from 'n8n-workflow';
|
||||
|
||||
import { router } from './actions/router';
|
||||
import { versionDescription } from './actions/versionDescription';
|
||||
import { listSearch } from './methods';
|
||||
|
||||
export class Ollama implements INodeType {
|
||||
description = versionDescription;
|
||||
|
||||
methods = {
|
||||
listSearch,
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions) {
|
||||
return await router.call(this);
|
||||
}
|
||||
}
|
||||
@@ -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',
|
||||
},
|
||||
],
|
||||
};
|
||||
+456
@@ -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,
|
||||
];
|
||||
+489
@@ -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 };
|
||||
}
|
||||
+72
@@ -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,
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export type * from './interfaces';
|
||||
@@ -0,0 +1,55 @@
|
||||
export interface OllamaMessage {
|
||||
role: 'system' | 'user' | 'assistant' | 'tool';
|
||||
content: string;
|
||||
images?: string[];
|
||||
tool_calls?: ToolCall[];
|
||||
tool_name?: string;
|
||||
}
|
||||
|
||||
export interface ToolCall {
|
||||
function: {
|
||||
name: string;
|
||||
arguments: Record<string, any>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface OllamaTool {
|
||||
type: 'function';
|
||||
function: {
|
||||
name: string;
|
||||
description: string;
|
||||
parameters: Record<string, unknown>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface OllamaChatResponse {
|
||||
model: string;
|
||||
created_at: string;
|
||||
message: OllamaMessage;
|
||||
done: boolean;
|
||||
done_reason?: string;
|
||||
total_duration?: number;
|
||||
load_duration?: number;
|
||||
prompt_eval_count?: number;
|
||||
prompt_eval_duration?: number;
|
||||
eval_count?: number;
|
||||
eval_duration?: number;
|
||||
}
|
||||
|
||||
export interface OllamaModel {
|
||||
name: string;
|
||||
modified_at: string;
|
||||
size: number;
|
||||
digest: string;
|
||||
details: {
|
||||
format: string;
|
||||
family: string;
|
||||
families: string[] | null;
|
||||
parameter_size: string;
|
||||
quantization_level: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface OllamaTagsResponse {
|
||||
models: OllamaModel[];
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * as listSearch from './listSearch';
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
import { mockDeep } from 'jest-mock-extended';
|
||||
import type { ILoadOptionsFunctions } from 'n8n-workflow';
|
||||
|
||||
import * as transport from '../transport';
|
||||
import { modelSearch } from './listSearch';
|
||||
|
||||
describe('Ollama List Search Methods', () => {
|
||||
const loadOptionsFunctionsMock = mockDeep<ILoadOptionsFunctions>();
|
||||
const apiRequestMock = jest.spyOn(transport, 'apiRequest');
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
describe('modelSearch', () => {
|
||||
it('should return all models when no filter is provided', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [
|
||||
{ name: 'llama3.2:latest' },
|
||||
{ name: 'llama3.2:3b' },
|
||||
{ name: 'codellama:latest' },
|
||||
{ name: 'mistral:7b' },
|
||||
{ name: 'phi3:latest' },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock);
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [
|
||||
{ name: 'llama3.2:latest', value: 'llama3.2:latest' },
|
||||
{ name: 'llama3.2:3b', value: 'llama3.2:3b' },
|
||||
{ name: 'codellama:latest', value: 'codellama:latest' },
|
||||
{ name: 'mistral:7b', value: 'mistral:7b' },
|
||||
{ name: 'phi3:latest', value: 'phi3:latest' },
|
||||
],
|
||||
});
|
||||
expect(apiRequestMock).toHaveBeenCalledWith('GET', '/api/tags');
|
||||
});
|
||||
|
||||
it('should filter models by name (case insensitive)', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [
|
||||
{ name: 'llama3.2:latest' },
|
||||
{ name: 'llama3.2:3b' },
|
||||
{ name: 'codellama:latest' },
|
||||
{ name: 'mistral:7b' },
|
||||
{ name: 'phi3:latest' },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock, 'llama');
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [
|
||||
{ name: 'llama3.2:latest', value: 'llama3.2:latest' },
|
||||
{ name: 'llama3.2:3b', value: 'llama3.2:3b' },
|
||||
{ name: 'codellama:latest', value: 'codellama:latest' },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle case insensitive filtering', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [{ name: 'Llama3.2:latest' }, { name: 'CODELLAMA:latest' }, { name: 'mistral:7b' }],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock, 'LLAMA');
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [
|
||||
{ name: 'Llama3.2:latest', value: 'Llama3.2:latest' },
|
||||
{ name: 'CODELLAMA:latest', value: 'CODELLAMA:latest' },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('should return empty results when filter matches no models', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [{ name: 'llama3.2:latest' }, { name: 'mistral:7b' }],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock, 'gpt');
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle empty model list', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock);
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle partial string matching', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [
|
||||
{ name: 'llama3.2:3b-instruct' },
|
||||
{ name: 'llama3.2:7b' },
|
||||
{ name: 'mistral:3b-instruct' },
|
||||
{ name: 'phi3:3b' },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock, '3b');
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [
|
||||
{ name: 'llama3.2:3b-instruct', value: 'llama3.2:3b-instruct' },
|
||||
{ name: 'mistral:3b-instruct', value: 'mistral:3b-instruct' },
|
||||
{ name: 'phi3:3b', value: 'phi3:3b' },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('should filter by tag', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [
|
||||
{ name: 'llama3.2:latest' },
|
||||
{ name: 'llama3.2:3b' },
|
||||
{ name: 'mistral:latest' },
|
||||
{ name: 'codellama:7b' },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock, 'latest');
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [
|
||||
{ name: 'llama3.2:latest', value: 'llama3.2:latest' },
|
||||
{ name: 'mistral:latest', value: 'mistral:latest' },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle special characters in filter', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [
|
||||
{ name: 'llama3.2:latest' },
|
||||
{ name: 'model-with-dash:1.0' },
|
||||
{ name: 'model_with_underscore:2.0' },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock, 'with-dash');
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [{ name: 'model-with-dash:1.0', value: 'model-with-dash:1.0' }],
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle undefined filter as no filter', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [{ name: 'llama3.2:latest' }, { name: 'mistral:7b' }],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock, undefined);
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [
|
||||
{ name: 'llama3.2:latest', value: 'llama3.2:latest' },
|
||||
{ name: 'mistral:7b', value: 'mistral:7b' },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle empty string filter as no filter', async () => {
|
||||
apiRequestMock.mockResolvedValue({
|
||||
models: [{ name: 'llama3.2:latest' }, { name: 'mistral:7b' }],
|
||||
});
|
||||
|
||||
const result = await modelSearch.call(loadOptionsFunctionsMock, '');
|
||||
|
||||
expect(result).toEqual({
|
||||
results: [
|
||||
{ name: 'llama3.2:latest', value: 'llama3.2:latest' },
|
||||
{ name: 'mistral:7b', value: 'mistral:7b' },
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { ILoadOptionsFunctions, INodeListSearchResult } from 'n8n-workflow';
|
||||
|
||||
import type { OllamaTagsResponse } from '../helpers/interfaces';
|
||||
import { apiRequest } from '../transport';
|
||||
|
||||
export async function modelSearch(
|
||||
this: ILoadOptionsFunctions,
|
||||
filter?: string,
|
||||
): Promise<INodeListSearchResult> {
|
||||
const response: OllamaTagsResponse = await apiRequest.call(this, 'GET', '/api/tags');
|
||||
|
||||
let models = response.models;
|
||||
|
||||
if (filter) {
|
||||
models = models.filter((model) => model.name.toLowerCase().includes(filter.toLowerCase()));
|
||||
}
|
||||
|
||||
return {
|
||||
results: models.map((model) => ({ name: model.name, value: model.name })),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="241.333" height="341.333" version="1.0" viewBox="0 0 181 256"><g fill="#7D7D87"><path d="M37.7 19.5c-5.2 1.8-8.3 4.9-11.7 11.6-4.5 8.9-6.2 19.2-5.8 35.5l.3 14.2-5.8 6.1c-14.8 15.5-18.5 38.7-9.2 57.4l3.4 6.9-2 4.4c-3.4 8.2-5 16.4-5 26.3 0 10.8 1.8 19 5.8 26.2l2.6 4.8-2.1 4.9c-1.2 2.7-2.6 7.1-3.2 9.8-1.4 6.2-1.5 22.1-.1 25.7 1 2.6 1.4 2.7 7.6 2.7 7.3 0 7 .4 5.3-8.6-1.5-8.2.2-18.8 4.2-26.6 3.7-7 3.8-10.4.5-14.8-4.7-6.4-6.8-13.6-6.9-24-.1-10.3 1.4-16 6.6-26.1 3.1-6.1 2.9-8.7-1-12.2-1.1-1-3.1-4.2-4.3-7-1.9-4.2-2.4-6.9-2.3-14.2 0-11.4 2.5-18.3 9.5-26 7-7.6 14.2-11 23.9-11.2 4.1 0 7.8-.2 8.2-.2.4-.1 1.7-2.2 2.9-4.7 3-5.9 9.6-11.9 16.7-15.2 4.9-2.3 7-2.7 14.7-2.7 7.9 0 9.7.4 14.9 2.9 6.8 3.3 13.3 9.4 15.9 14.8 1 2 2.3 4.1 3 4.5.6.4 4.6.8 8.7.8 6.7.1 8.3.5 14 3.6 12.3 6.8 19.3 18.7 19.3 33.4.1 6.7-.4 9-2.7 14.2-1.6 3.5-3.5 6.8-4.3 7.5-3.4 2.8-3.5 5.8-.5 11.7 5.2 10.1 6.7 15.8 6.6 26.1-.1 10.4-2.2 17.6-6.9 24-3.3 4.4-3.2 7.8.5 14.8 4 7.8 5.7 18.4 4.2 26.6-1.7 9-2 8.6 5.3 8.6 6.2 0 6.6-.1 7.6-2.7 1.4-3.6 1.3-19.5-.1-25.7-.6-2.7-2-7.1-3.2-9.8l-2.1-4.9 2.6-4.8c7.6-13.9 7.9-35.9.6-52.8l-2-4.7 2.5-4.6c9.9-18.3 6.4-43.9-8.1-59.1l-5.8-6.1.3-14.2c.4-16.4-1.3-26.6-5.8-35.7-6.4-12.6-17.2-15.9-26.3-7.9-5.4 4.7-9.2 13.8-12.3 29.8-.3 1.4-1 2.2-1.7 1.8-18.2-8-29.7-8.5-44.3-2.1L65 54.9l-.4-2.2C61 34.2 56.1 24.2 49 20.5c-4.3-2.1-7.4-2.4-11.3-1m7.7 16.8c4.2 7.1 8.1 30.1 5.7 33.6-.5.8-3.1 1.6-5.8 1.8-2.6.2-6.2.8-8 1.3l-3.1.8-.7-4.9c-.8-5.9.2-17.2 2.2-24.8C37.1 38.4 40.5 32 42 32c.5 0 2 1.9 3.4 4.3m96.5-1c4 6.5 6.9 23.9 5.6 33.6l-.7 4.9-3.1-.8c-1.8-.5-5.4-1.1-8-1.3-2.7-.2-5.3-1-5.8-1.8-1.2-1.7-.3-14.1 1.7-22.9 1.5-6.4 5.7-15 7.4-15 .4 0 1.8 1.5 2.9 3.3"/><path d="M77.8 119.9c-7.3 2.4-11.6 5.1-16.5 10.4-5.5 6-7.6 12-7.1 20.1.5 7.6 3.5 12.9 10.6 18.3 6.2 4.7 12.7 6.3 25.7 6.3 17.2 0 25.8-3.6 32.9-13.8 4.2-5.9 4.8-15.5 1.6-23-2.9-6.8-11.1-14.3-18.8-17.3-8-3.1-20.7-3.6-28.4-1m25.7 10c16.1 7.1 19.4 23.2 6.6 31.8-4.9 3.3-9.4 4.3-19.6 4.3s-14.7-1-19.6-4.3c-17.8-12-3.2-35.6 21.1-34.3 3.9.2 8.6 1.2 11.5 2.5"/><path d="M83.8 140.1c-2.5 1.4-2.2 4.4.7 6.7 2 1.6 2.4 2.6 1.9 4.9-.7 3.6 1.5 5.8 5.1 4.9 2.1-.5 2.5-1.2 2.5-4.6 0-2.9.5-4.2 2-5 2.7-1.5 2.7-6.6 0-7.5-1-.3-2.8-.1-4 .5-1.4.7-2.6.8-3.9 0-2.3-1.2-2.2-1.2-4.3.1m-44.1-18.9c-.9.7-2.3 3-3.2 5-2.1 5.3-.1 10.3 4.7 11.6 4.3 1.1 6 .6 9.2-2.7 4-4.1 4.3-8.1 1.1-11.9-2.1-2.5-3.4-3.2-6.4-3.2-2 0-4.5.6-5.4 1.2m89.8 2c-3.2 3.8-2.9 7.8 1.1 11.9 3.2 3.3 4.9 3.8 9.2 2.7 4.9-1.3 6.8-6.2 4.6-11.8-1.9-4.7-3.8-6-8.7-6-2.7 0-4.1.7-6.2 3.2"/></g></svg>
|
||||
|
After Width: | Height: | Size: 2.5 KiB |
@@ -0,0 +1,243 @@
|
||||
import { mockDeep } from 'jest-mock-extended';
|
||||
import type { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-workflow';
|
||||
|
||||
import { apiRequest } from './index';
|
||||
|
||||
describe('Ollama Transport', () => {
|
||||
const executeFunctionsMock = mockDeep<IExecuteFunctions>();
|
||||
const loadOptionsFunctionsMock = mockDeep<ILoadOptionsFunctions>();
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
describe('apiRequest', () => {
|
||||
it('should make API request with basic auth', async () => {
|
||||
executeFunctionsMock.getCredentials.mockResolvedValue({
|
||||
baseUrl: 'http://localhost:11434',
|
||||
apiKey: 'test-api-key',
|
||||
});
|
||||
executeFunctionsMock.helpers.httpRequestWithAuthentication.mockResolvedValue({
|
||||
model: 'test-model',
|
||||
response: 'test response',
|
||||
});
|
||||
|
||||
const result = await apiRequest.call(executeFunctionsMock, 'POST', '/api/chat', {
|
||||
body: { model: 'test-model', messages: [] },
|
||||
});
|
||||
|
||||
expect(executeFunctionsMock.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'ollamaApi',
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer test-api-key',
|
||||
},
|
||||
method: 'POST',
|
||||
body: { model: 'test-model', messages: [] },
|
||||
qs: undefined,
|
||||
url: 'http://localhost:11434/api/chat',
|
||||
json: true,
|
||||
},
|
||||
);
|
||||
expect(result).toEqual({ model: 'test-model', response: 'test response' });
|
||||
});
|
||||
|
||||
it('should make API request without auth when no API key provided', async () => {
|
||||
executeFunctionsMock.getCredentials.mockResolvedValue({
|
||||
baseUrl: 'http://localhost:11434',
|
||||
});
|
||||
executeFunctionsMock.helpers.httpRequestWithAuthentication.mockResolvedValue({
|
||||
model: 'test-model',
|
||||
response: 'test response',
|
||||
});
|
||||
|
||||
await apiRequest.call(executeFunctionsMock, 'GET', '/api/tags');
|
||||
|
||||
expect(executeFunctionsMock.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'ollamaApi',
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'GET',
|
||||
body: undefined,
|
||||
qs: undefined,
|
||||
url: 'http://localhost:11434/api/tags',
|
||||
json: true,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle query parameters', async () => {
|
||||
executeFunctionsMock.getCredentials.mockResolvedValue({
|
||||
baseUrl: 'http://localhost:11434',
|
||||
});
|
||||
executeFunctionsMock.helpers.httpRequestWithAuthentication.mockResolvedValue([]);
|
||||
|
||||
await apiRequest.call(executeFunctionsMock, 'GET', '/api/tags', {
|
||||
qs: { limit: 10, offset: 0 },
|
||||
});
|
||||
|
||||
expect(executeFunctionsMock.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'ollamaApi',
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'GET',
|
||||
body: undefined,
|
||||
qs: { limit: 10, offset: 0 },
|
||||
url: 'http://localhost:11434/api/tags',
|
||||
json: true,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle custom headers', async () => {
|
||||
executeFunctionsMock.getCredentials.mockResolvedValue({
|
||||
baseUrl: 'http://localhost:11434',
|
||||
apiKey: 'test-key',
|
||||
});
|
||||
executeFunctionsMock.helpers.httpRequestWithAuthentication.mockResolvedValue({});
|
||||
|
||||
await apiRequest.call(executeFunctionsMock, 'POST', '/api/generate', {
|
||||
headers: { 'X-Custom-Header': 'custom-value' },
|
||||
body: { model: 'test', prompt: 'hello' },
|
||||
});
|
||||
|
||||
expect(executeFunctionsMock.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'ollamaApi',
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer test-key',
|
||||
'X-Custom-Header': 'custom-value',
|
||||
},
|
||||
method: 'POST',
|
||||
body: { model: 'test', prompt: 'hello' },
|
||||
qs: undefined,
|
||||
url: 'http://localhost:11434/api/generate',
|
||||
json: true,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle additional options', async () => {
|
||||
executeFunctionsMock.getCredentials.mockResolvedValue({
|
||||
baseUrl: 'http://localhost:11434',
|
||||
});
|
||||
executeFunctionsMock.helpers.httpRequestWithAuthentication.mockResolvedValue({});
|
||||
|
||||
await apiRequest.call(executeFunctionsMock, 'POST', '/api/chat', {
|
||||
body: { model: 'test' },
|
||||
option: { timeout: 30000, encoding: 'utf8' },
|
||||
});
|
||||
|
||||
expect(executeFunctionsMock.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'ollamaApi',
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'POST',
|
||||
body: { model: 'test' },
|
||||
qs: undefined,
|
||||
url: 'http://localhost:11434/api/chat',
|
||||
json: true,
|
||||
timeout: 30000,
|
||||
encoding: 'utf8',
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should work with ILoadOptionsFunctions', async () => {
|
||||
loadOptionsFunctionsMock.getCredentials.mockResolvedValue({
|
||||
baseUrl: 'http://localhost:11434',
|
||||
});
|
||||
loadOptionsFunctionsMock.helpers.httpRequestWithAuthentication.mockResolvedValue({
|
||||
models: [{ name: 'llama3.2:latest' }],
|
||||
});
|
||||
|
||||
const result = await apiRequest.call(loadOptionsFunctionsMock, 'GET', '/api/tags');
|
||||
|
||||
expect(loadOptionsFunctionsMock.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'ollamaApi',
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'GET',
|
||||
body: undefined,
|
||||
qs: undefined,
|
||||
url: 'http://localhost:11434/api/tags',
|
||||
json: true,
|
||||
},
|
||||
);
|
||||
expect(result).toEqual({ models: [{ name: 'llama3.2:latest' }] });
|
||||
});
|
||||
|
||||
it('should handle baseUrl with trailing slash', async () => {
|
||||
executeFunctionsMock.getCredentials.mockResolvedValue({
|
||||
baseUrl: 'http://localhost:11434/',
|
||||
apiKey: 'test-key',
|
||||
});
|
||||
executeFunctionsMock.helpers.httpRequestWithAuthentication.mockResolvedValue({});
|
||||
|
||||
await apiRequest.call(executeFunctionsMock, 'GET', '/api/tags');
|
||||
|
||||
expect(executeFunctionsMock.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'ollamaApi',
|
||||
expect.objectContaining({
|
||||
url: 'http://localhost:11434/api/tags',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle empty parameters object', async () => {
|
||||
executeFunctionsMock.getCredentials.mockResolvedValue({
|
||||
baseUrl: 'http://localhost:11434',
|
||||
});
|
||||
executeFunctionsMock.helpers.httpRequestWithAuthentication.mockResolvedValue({});
|
||||
|
||||
await apiRequest.call(executeFunctionsMock, 'GET', '/api/tags', {});
|
||||
|
||||
expect(executeFunctionsMock.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'ollamaApi',
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'GET',
|
||||
body: undefined,
|
||||
qs: undefined,
|
||||
url: 'http://localhost:11434/api/tags',
|
||||
json: true,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle undefined parameters', async () => {
|
||||
executeFunctionsMock.getCredentials.mockResolvedValue({
|
||||
baseUrl: 'http://localhost:11434',
|
||||
});
|
||||
executeFunctionsMock.helpers.httpRequestWithAuthentication.mockResolvedValue({});
|
||||
|
||||
await apiRequest.call(executeFunctionsMock, 'GET', '/api/tags');
|
||||
|
||||
expect(executeFunctionsMock.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||
'ollamaApi',
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'GET',
|
||||
body: undefined,
|
||||
qs: undefined,
|
||||
url: 'http://localhost:11434/api/tags',
|
||||
json: true,
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
IHttpRequestMethods,
|
||||
ILoadOptionsFunctions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
type RequestParameters = {
|
||||
headers?: IDataObject;
|
||||
body?: IDataObject | string;
|
||||
qs?: IDataObject;
|
||||
option?: IDataObject;
|
||||
};
|
||||
|
||||
export async function apiRequest(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
endpoint: string,
|
||||
parameters?: RequestParameters,
|
||||
) {
|
||||
const { body, qs, option } = parameters ?? {};
|
||||
|
||||
const credentials = await this.getCredentials<{
|
||||
apiKey?: string;
|
||||
baseUrl: string;
|
||||
}>('ollamaApi');
|
||||
const apiKey = credentials.apiKey;
|
||||
if (apiKey !== undefined && typeof apiKey !== 'string') {
|
||||
throw new Error('API key must be a string');
|
||||
}
|
||||
|
||||
const url = new URL(endpoint, credentials.baseUrl).toString();
|
||||
|
||||
const headers = parameters?.headers ?? {};
|
||||
if (apiKey) {
|
||||
headers.Authorization = `Bearer ${apiKey}`;
|
||||
}
|
||||
|
||||
const options = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...headers,
|
||||
},
|
||||
method,
|
||||
body,
|
||||
qs,
|
||||
url,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (option && Object.keys(option).length !== 0) {
|
||||
Object.assign(options, option);
|
||||
}
|
||||
|
||||
return await this.helpers.httpRequestWithAuthentication.call(this, 'ollamaApi', options);
|
||||
}
|
||||
Reference in New Issue
Block a user