mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 17:46:45 +00:00
perf(OpenAI Node): Use streaming for file operations (#18666)
This commit is contained in:
@@ -2,6 +2,7 @@ import FormData from 'form-data';
|
||||
import type { INodeProperties, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||
import { updateDisplayOptions } from 'n8n-workflow';
|
||||
|
||||
import { getBinaryDataFile } from '../../helpers/binary-data';
|
||||
import { apiRequest } from '../../transport';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
@@ -71,19 +72,19 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
|
||||
formData.append('temperature', options.temperature.toString());
|
||||
}
|
||||
|
||||
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
||||
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
||||
|
||||
formData.append('file', dataBuffer, {
|
||||
filename: binaryData.fileName,
|
||||
contentType: binaryData.mimeType,
|
||||
const { filename, contentType, fileContent } = await getBinaryDataFile(
|
||||
this,
|
||||
i,
|
||||
binaryPropertyName,
|
||||
);
|
||||
formData.append('file', fileContent, {
|
||||
filename,
|
||||
contentType,
|
||||
});
|
||||
|
||||
const response = await apiRequest.call(this, 'POST', '/audio/transcriptions', {
|
||||
option: { formData },
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
headers: formData.getHeaders(),
|
||||
});
|
||||
|
||||
return [
|
||||
|
||||
@@ -2,6 +2,7 @@ import FormData from 'form-data';
|
||||
import type { INodeProperties, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||
import { updateDisplayOptions } from 'n8n-workflow';
|
||||
|
||||
import { getBinaryDataFile } from '../../helpers/binary-data';
|
||||
import { apiRequest } from '../../transport';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
@@ -59,19 +60,19 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
|
||||
formData.append('temperature', options.temperature.toString());
|
||||
}
|
||||
|
||||
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
||||
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
||||
|
||||
formData.append('file', dataBuffer, {
|
||||
filename: binaryData.fileName,
|
||||
contentType: binaryData.mimeType,
|
||||
const { filename, contentType, fileContent } = await getBinaryDataFile(
|
||||
this,
|
||||
i,
|
||||
binaryPropertyName,
|
||||
);
|
||||
formData.append('file', fileContent, {
|
||||
filename,
|
||||
contentType,
|
||||
});
|
||||
|
||||
const response = await apiRequest.call(this, 'POST', '/audio/translations', {
|
||||
option: { formData },
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
headers: formData.getHeaders(),
|
||||
});
|
||||
|
||||
return [
|
||||
|
||||
@@ -2,6 +2,7 @@ import FormData from 'form-data';
|
||||
import type { INodeProperties, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||
import { updateDisplayOptions, NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import { getBinaryDataFile } from '../../helpers/binary-data';
|
||||
import { apiRequest } from '../../transport';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
@@ -61,20 +62,20 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
|
||||
|
||||
formData.append('purpose', options.purpose || 'assistants');
|
||||
|
||||
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
||||
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
||||
|
||||
formData.append('file', dataBuffer, {
|
||||
filename: binaryData.fileName,
|
||||
contentType: binaryData.mimeType,
|
||||
const { filename, contentType, fileContent } = await getBinaryDataFile(
|
||||
this,
|
||||
i,
|
||||
binaryPropertyName,
|
||||
);
|
||||
formData.append('file', fileContent, {
|
||||
filename,
|
||||
contentType,
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await apiRequest.call(this, 'POST', '/files', {
|
||||
option: { formData },
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
headers: formData.getHeaders(),
|
||||
});
|
||||
|
||||
return [
|
||||
|
||||
27
packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/helpers/binary-data.ts
vendored
Normal file
27
packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/helpers/binary-data.ts
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { IExecuteFunctions } from 'n8n-workflow';
|
||||
|
||||
/** Chunk size to use for streaming. 256Kb */
|
||||
const CHUNK_SIZE = 256 * 1024;
|
||||
|
||||
/**
|
||||
* Gets the binary data file for the given item index and given property name.
|
||||
* Returns the file name, content type and the file content. Uses streaming
|
||||
* when possible.
|
||||
*/
|
||||
export async function getBinaryDataFile(
|
||||
ctx: IExecuteFunctions,
|
||||
itemIdx: number,
|
||||
binaryPropertyName: string,
|
||||
) {
|
||||
const binaryData = ctx.helpers.assertBinaryData(itemIdx, binaryPropertyName);
|
||||
|
||||
const fileContent = binaryData.id
|
||||
? await ctx.helpers.getBinaryStream(binaryData.id, CHUNK_SIZE)
|
||||
: await ctx.helpers.getBinaryDataBuffer(itemIdx, binaryPropertyName);
|
||||
|
||||
return {
|
||||
filename: binaryData.fileName,
|
||||
contentType: binaryData.mimeType,
|
||||
fileContent,
|
||||
};
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import FormData from 'form-data';
|
||||
import get from 'lodash/get';
|
||||
import type { IDataObject, IExecuteFunctions } from 'n8n-workflow';
|
||||
|
||||
@@ -362,7 +363,12 @@ describe('OpenAi, Audio resource', () => {
|
||||
'POST',
|
||||
'/audio/transcriptions',
|
||||
expect.objectContaining({
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
headers: expect.objectContaining({
|
||||
'content-type': expect.stringMatching(/^multipart\/form-data; boundary=/),
|
||||
}),
|
||||
option: expect.objectContaining({
|
||||
formData: expect.any(FormData),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -386,7 +392,12 @@ describe('OpenAi, Audio resource', () => {
|
||||
'POST',
|
||||
'/audio/translations',
|
||||
expect.objectContaining({
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
headers: expect.objectContaining({
|
||||
'content-type': expect.stringMatching(/^multipart\/form-data; boundary=/),
|
||||
}),
|
||||
option: expect.objectContaining({
|
||||
formData: expect.any(FormData),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -453,7 +464,12 @@ describe('OpenAi, File resource', () => {
|
||||
'POST',
|
||||
'/files',
|
||||
expect.objectContaining({
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
headers: expect.objectContaining({
|
||||
'content-type': expect.stringMatching(/^multipart\/form-data; boundary=/),
|
||||
}),
|
||||
option: expect.objectContaining({
|
||||
formData: expect.any(FormData),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user