mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(Basic LLM Chain Node): Use correct mimetype for binary data (#12978)
This commit is contained in:
parent
9e4e1ca1f4
commit
2b1eb049f2
|
@ -34,6 +34,7 @@ import { getOptionalOutputParsers } from '@utils/output_parsers/N8nOutputParser'
|
||||||
import { getTemplateNoticeField } from '@utils/sharedFields';
|
import { getTemplateNoticeField } from '@utils/sharedFields';
|
||||||
import { getTracingConfig } from '@utils/tracing';
|
import { getTracingConfig } from '@utils/tracing';
|
||||||
|
|
||||||
|
import { dataUriFromImageData, UnsupportedMimeTypeError } from './utils';
|
||||||
import {
|
import {
|
||||||
getCustomErrorMessage as getCustomOpenAiErrorMessage,
|
getCustomErrorMessage as getCustomOpenAiErrorMessage,
|
||||||
isOpenAiError,
|
isOpenAiError,
|
||||||
|
@ -88,21 +89,28 @@ async function getImageMessage(
|
||||||
NodeConnectionType.AiLanguageModel,
|
NodeConnectionType.AiLanguageModel,
|
||||||
0,
|
0,
|
||||||
)) as BaseLanguageModel;
|
)) as BaseLanguageModel;
|
||||||
const dataURI = `data:image/jpeg;base64,${bufferData.toString('base64')}`;
|
|
||||||
|
|
||||||
const directUriModels = [ChatGoogleGenerativeAI, ChatOllama];
|
try {
|
||||||
const imageUrl = directUriModels.some((i) => model instanceof i)
|
const dataURI = dataUriFromImageData(binaryData, bufferData);
|
||||||
? dataURI
|
|
||||||
: { url: dataURI, detail };
|
|
||||||
|
|
||||||
return new HumanMessage({
|
const directUriModels = [ChatGoogleGenerativeAI, ChatOllama];
|
||||||
content: [
|
const imageUrl = directUriModels.some((i) => model instanceof i)
|
||||||
{
|
? dataURI
|
||||||
type: 'image_url',
|
: { url: dataURI, detail };
|
||||||
image_url: imageUrl,
|
|
||||||
},
|
return new HumanMessage({
|
||||||
],
|
content: [
|
||||||
});
|
{
|
||||||
|
type: 'image_url',
|
||||||
|
image_url: imageUrl,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof UnsupportedMimeTypeError)
|
||||||
|
throw new NodeOperationError(context.getNode(), error.message);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getChainPromptTemplate(
|
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')}`;
|
||||||
|
}
|
Loading…
Reference in a new issue