mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +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 type { INodeProperties, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||||
import { updateDisplayOptions } from 'n8n-workflow';
|
import { updateDisplayOptions } from 'n8n-workflow';
|
||||||
|
|
||||||
|
import { getBinaryDataFile } from '../../helpers/binary-data';
|
||||||
import { apiRequest } from '../../transport';
|
import { apiRequest } from '../../transport';
|
||||||
|
|
||||||
const properties: INodeProperties[] = [
|
const properties: INodeProperties[] = [
|
||||||
@@ -71,19 +72,19 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
|
|||||||
formData.append('temperature', options.temperature.toString());
|
formData.append('temperature', options.temperature.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
const { filename, contentType, fileContent } = await getBinaryDataFile(
|
||||||
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
this,
|
||||||
|
i,
|
||||||
formData.append('file', dataBuffer, {
|
binaryPropertyName,
|
||||||
filename: binaryData.fileName,
|
);
|
||||||
contentType: binaryData.mimeType,
|
formData.append('file', fileContent, {
|
||||||
|
filename,
|
||||||
|
contentType,
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await apiRequest.call(this, 'POST', '/audio/transcriptions', {
|
const response = await apiRequest.call(this, 'POST', '/audio/transcriptions', {
|
||||||
option: { formData },
|
option: { formData },
|
||||||
headers: {
|
headers: formData.getHeaders(),
|
||||||
'Content-Type': 'multipart/form-data',
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import FormData from 'form-data';
|
|||||||
import type { INodeProperties, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
import type { INodeProperties, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||||
import { updateDisplayOptions } from 'n8n-workflow';
|
import { updateDisplayOptions } from 'n8n-workflow';
|
||||||
|
|
||||||
|
import { getBinaryDataFile } from '../../helpers/binary-data';
|
||||||
import { apiRequest } from '../../transport';
|
import { apiRequest } from '../../transport';
|
||||||
|
|
||||||
const properties: INodeProperties[] = [
|
const properties: INodeProperties[] = [
|
||||||
@@ -59,19 +60,19 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
|
|||||||
formData.append('temperature', options.temperature.toString());
|
formData.append('temperature', options.temperature.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
const { filename, contentType, fileContent } = await getBinaryDataFile(
|
||||||
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
this,
|
||||||
|
i,
|
||||||
formData.append('file', dataBuffer, {
|
binaryPropertyName,
|
||||||
filename: binaryData.fileName,
|
);
|
||||||
contentType: binaryData.mimeType,
|
formData.append('file', fileContent, {
|
||||||
|
filename,
|
||||||
|
contentType,
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await apiRequest.call(this, 'POST', '/audio/translations', {
|
const response = await apiRequest.call(this, 'POST', '/audio/translations', {
|
||||||
option: { formData },
|
option: { formData },
|
||||||
headers: {
|
headers: formData.getHeaders(),
|
||||||
'Content-Type': 'multipart/form-data',
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import FormData from 'form-data';
|
|||||||
import type { INodeProperties, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
import type { INodeProperties, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||||
import { updateDisplayOptions, NodeOperationError } from 'n8n-workflow';
|
import { updateDisplayOptions, NodeOperationError } from 'n8n-workflow';
|
||||||
|
|
||||||
|
import { getBinaryDataFile } from '../../helpers/binary-data';
|
||||||
import { apiRequest } from '../../transport';
|
import { apiRequest } from '../../transport';
|
||||||
|
|
||||||
const properties: INodeProperties[] = [
|
const properties: INodeProperties[] = [
|
||||||
@@ -61,20 +62,20 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
|
|||||||
|
|
||||||
formData.append('purpose', options.purpose || 'assistants');
|
formData.append('purpose', options.purpose || 'assistants');
|
||||||
|
|
||||||
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
const { filename, contentType, fileContent } = await getBinaryDataFile(
|
||||||
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
this,
|
||||||
|
i,
|
||||||
formData.append('file', dataBuffer, {
|
binaryPropertyName,
|
||||||
filename: binaryData.fileName,
|
);
|
||||||
contentType: binaryData.mimeType,
|
formData.append('file', fileContent, {
|
||||||
|
filename,
|
||||||
|
contentType,
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await apiRequest.call(this, 'POST', '/files', {
|
const response = await apiRequest.call(this, 'POST', '/files', {
|
||||||
option: { formData },
|
option: { formData },
|
||||||
headers: {
|
headers: formData.getHeaders(),
|
||||||
'Content-Type': 'multipart/form-data',
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return [
|
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 get from 'lodash/get';
|
||||||
import type { IDataObject, IExecuteFunctions } from 'n8n-workflow';
|
import type { IDataObject, IExecuteFunctions } from 'n8n-workflow';
|
||||||
|
|
||||||
@@ -362,7 +363,12 @@ describe('OpenAi, Audio resource', () => {
|
|||||||
'POST',
|
'POST',
|
||||||
'/audio/transcriptions',
|
'/audio/transcriptions',
|
||||||
expect.objectContaining({
|
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',
|
'POST',
|
||||||
'/audio/translations',
|
'/audio/translations',
|
||||||
expect.objectContaining({
|
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',
|
'POST',
|
||||||
'/files',
|
'/files',
|
||||||
expect.objectContaining({
|
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