Open
Description
Confirm this is a Node library issue and not an underlying OpenAI API issue
- This is an issue with the Node library
Describe the bug
Whenever a web ReadableStream
is passed to the toFile
helper function, the full contents of the file are buffered before forwarding the request to the Whisper OpenAI API endpoint.
However, it is possible to avoid having to buffer the whole file into the server memory, and instead just use the server as a middleware that streamlines the data from the source of the file to the API endpoint.
The problem has been discussed on issue #414 as well.
A workaround using axios
and FormData
that seems to work:
import FormData = require("form-data");
import axios from "axios";
async function foo() {
const bucket = storage.bucket(bucketName);
const file = bucket.file(fileName);
const readStream = file.createReadStream();
const form = new FormData();
// Make sure that the file has the proper extension as well
// (In this example, we just add the `webm` extension for brevity
if (!fileName.includes(".")) {
fileName = `${fileName}.webm`;
}
form.append("file", readStream, fileName);
form.append("model", "whisper-1");
const apiKey = OPENAI_API_KEY
const response = await axios.post(
"https://api.openai.com/v1/audio/transcriptions",
form,
{
headers: {
...form.getHeaders(),
Authorization: `Bearer ${apiKey}`,
},
}
);
const transcription = response.data.text:
return transcription;
}
To Reproduce
Example uses Cloud / Firebase Storage.
Code snippets
import {OpenAI, toFile} from 'openai'
const bucket = storage.bucket(bucketName);
const file = bucket.file(fileName);
const openai = new OpenAI({
apiKey: apiKey,
});
const completion = await openai.audio.transcriptions.create({
file: await toFile(file, "myfile.mp3"),
model: "whisper-1",
});
OS
Linux (Google Cloud Functions)
Node version
18
Library version
4.11.1