mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix(Basic LLM Chain Node): Use correct mimetype for binary data (#12978)
This commit is contained in:
@@ -34,6 +34,7 @@ import { getOptionalOutputParsers } from '@utils/output_parsers/N8nOutputParser'
|
||||
import { getTemplateNoticeField } from '@utils/sharedFields';
|
||||
import { getTracingConfig } from '@utils/tracing';
|
||||
|
||||
import { dataUriFromImageData, UnsupportedMimeTypeError } from './utils';
|
||||
import {
|
||||
getCustomErrorMessage as getCustomOpenAiErrorMessage,
|
||||
isOpenAiError,
|
||||
@@ -88,7 +89,9 @@ async function getImageMessage(
|
||||
NodeConnectionType.AiLanguageModel,
|
||||
0,
|
||||
)) as BaseLanguageModel;
|
||||
const dataURI = `data:image/jpeg;base64,${bufferData.toString('base64')}`;
|
||||
|
||||
try {
|
||||
const dataURI = dataUriFromImageData(binaryData, bufferData);
|
||||
|
||||
const directUriModels = [ChatGoogleGenerativeAI, ChatOllama];
|
||||
const imageUrl = directUriModels.some((i) => model instanceof i)
|
||||
@@ -103,6 +106,11 @@ async function getImageMessage(
|
||||
},
|
||||
],
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof UnsupportedMimeTypeError)
|
||||
throw new NodeOperationError(context.getNode(), error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function getChainPromptTemplate(
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { IBinaryData } from 'n8n-workflow';
|
||||
|
||||
import { dataUriFromImageData, UnsupportedMimeTypeError } from '../utils';
|
||||
|
||||
describe('dataUriFromImageData', () => {
|
||||
it('should not throw an error on images', async () => {
|
||||
const mockBuffer = Buffer.from('Test data');
|
||||
const mockBinaryData = mock<IBinaryData>({ mimeType: 'image/jpeg' });
|
||||
|
||||
const dataUri = dataUriFromImageData(mockBinaryData, mockBuffer);
|
||||
expect(dataUri).toBe('data:image/jpeg;base64,VGVzdCBkYXRh');
|
||||
});
|
||||
|
||||
it('should throw an UnsupportetMimeTypeError on non-images', async () => {
|
||||
const mockBuffer = Buffer.from('Test data');
|
||||
const mockBinaryData = mock<IBinaryData>({ mimeType: 'text/plain' });
|
||||
|
||||
expect(() => {
|
||||
dataUriFromImageData(mockBinaryData, mockBuffer);
|
||||
}).toThrow(UnsupportedMimeTypeError);
|
||||
});
|
||||
});
|
||||
12
packages/@n8n/nodes-langchain/nodes/chains/ChainLLM/utils.ts
Normal file
12
packages/@n8n/nodes-langchain/nodes/chains/ChainLLM/utils.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { IBinaryData } from 'n8n-workflow';
|
||||
import { ApplicationError } from 'n8n-workflow';
|
||||
|
||||
export class UnsupportedMimeTypeError extends ApplicationError {}
|
||||
|
||||
export function dataUriFromImageData(binaryData: IBinaryData, bufferData: Buffer) {
|
||||
if (!binaryData.mimeType?.startsWith('image/'))
|
||||
throw new UnsupportedMimeTypeError(
|
||||
`${binaryData.mimeType} is not a supported type of binary data. Only images are supported.`,
|
||||
);
|
||||
return `data:${binaryData.mimeType};base64,${bufferData.toString('base64')}`;
|
||||
}
|
||||
Reference in New Issue
Block a user