I'm using whisper.js code to get transcription of a wav file. However when running the code below this is the error I'm getting.
Error [ERR_FR_MAX_BODY_LENGTH_EXCEEDED]: Request body larger than maxBodyLength limit.
When I asked chatgpt for the error it said, 'indicates that the size of the audio file you're trying to send to OpenAI's API is exceeding the allowed request body size' so I added the the following part in the code as per said by chatgpt:
const configuration = new Configuration({
apiKey: "YOUR_API_KEY",
maxBodyLength: Infinity,
});
This is my code:
require('dotenv').config();
const { Configuration, OpenAIApi } = require("openai");
const fs = require("fs");
const configuration = new Configuration({
apiKey: "YOUR_API_KEY",
maxBodyLength: Infinity,
});
const openai = new OpenAIApi(configuration);
async function createTranscription(audioFileName) {
const resp = await openai.createTranscription(
fs.createReadStream(audioFileName),
"whisper-1"
);
return resp.data.text;
}
async function main() {
try {
const audioFileName = '/Users/rahulsharma/Desktop/whisper/output_dir/stream.wav';
const transcription = await createTranscription(audioFileName);
console.log(transcription);
} catch (e) {
console.error(e);
}
}
main();
Does anyone how to get around this error?
You can pass some request options when creating the transcription. With the openai v3 package you need to specify all options you can pass according to the OpenAI API reference, then you can pass on an object with maxBodyLength.
await openai.createTranscription(
fs.createReadStream(audioFileName),
model, // "whisper-1"
prompt,
responseFormat,
temperature,
language,
{
maxBodyLength: Infinity,
},
);
If you don't want to specify prompt, responseFormat, temperature, or language, you can just set them to undefined.
Edit: Note that you only need to set maxBodyLength when you use the openai library < version 4. Before version 4 the library uses Axios, starting with v4 the library uses fetch (well, node-fetch). MaxBodyLength is an option specific to axios, fetch doesn't have it and you don't need to do anything to be able to send large files to the Whisper API.