I have a frontend that allows users to upload a file which then gets sent to my Node/Express server in a multipart form.
I am using multer
to parse the file from the request but I can’t pass it to OpenAI without error. I have it set to save the file in storage and then I pass the file path into the request and it returns with an error.
The error I get is "Request failed with status code 400"
const express = require("express");
const multer = require("multer");
const { Configuration, OpenAIApi } = require("openai");
const upload = multer({ dest: "uploads/" });
const fs = require("fs");
const whisperConfiguration = new Configuration({
apiKey: "API_KEY",
});
const app = express();
const port = 3000;
app.use(express.static("static"));
app.post("/whisper", upload.single("audio_file"), async (req, res) => {
const whisperTime = process.hrtime();
const openai = new OpenAIApi(whisperConfiguration);
console.log("whisper", req.file);
try {
const resp = await openai.createTranscription(
fs.createReadStream(req.file.path),
"whisper-1"
);
res.send({ apiCall: resp.data, time: process.hrtime(whisperTime) });
} catch (e) {
res.send(e);
}
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));