请求帮助获取Whisper API(NextJs)-无法解析多部分表单错误

voase2hg  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(117)

我遇到了一个问题,试图发送一个音频wav文件耳语。我的api里有这段代码
pages> api> whisper. ts
我不明白这个问题。
我在谷歌上搜索了这个问题,并在GitHub、Medium文章、stackoverflow等上查看了其他示例。但我无法纠正这个错误。我可以得到一个回应使用相同的openai键为我的聊天,但不是这个耳语ai。
任何建议都将是有益的。
先谢谢你。
我的准则是

export default withFileUpload(async (req, res) => {
  const file = req.file;
  //console.log('------- req.file', req.file);
  //console.log('------- req.body', req.body);
  console.log('------- req.file.filepath', req.file?.filepath);
  if (!file) {
    res.status(400).send('No file uploaded');
    return;
  }

  // Create form data
  const formData = new FormData();
  //formData.append('file', createReadStream(file.filepath), 'audio.wav');
  formData.append('file', createReadStream(file.filepath), 'audio.wav');
  formData.append('model', 'whisper-1');
  
console.log('whisper formData', formData);
  
  const response = await fetch(
    'https://api.openai.com/v1/audio/transcriptions',
    {
      method: 'POST',
      headers: {
        ...formData.getHeaders(),
        Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
      },
      body: formData,
    }
  );

  console.log("Calling Whisper status", response.status);
  console.log("Calling Whisper json", response.json);
  
  const { text, error } = await response.json();
  console.log("Calling Whisper text", text);
  if (response.ok) {
    res.status(200).json({ text: text });
  } else {
    console.log('1. OPEN AI ERROR:');
    console.log('2.',error.message);
    res.status(400).send(new Error());
  }
});

console.log输出如下:

whisper formData FormData {
  _overheadLength: 255,
  _valueLength: 9,
  _valuesToMeasure: [
    ReadStream {
      fd: null,
      path: '/var/folders/jx/bg6p5m5x30g_ywlsvfr12ndm0000gn/T/beadcd538ea1b3259e7e30805',
      flags: 'r',
      mode: 438,
      start: undefined,
      end: Infinity,
      pos: undefined,
      bytesRead: 0,
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      emit: [Function (anonymous)],
      [Symbol(kFs)]: [Object],
      [Symbol(kIsPerformingIO)]: false,
      [Symbol(kCapture)]: false
    }
  ],
  writable: false,
  readable: true,
  dataSize: 0,
  maxDataSize: 2097152,
  pauseStreams: true,
  _released: false,
  _streams: [
    '----------------------------736652610391458148561295\r\n' +
      'Content-Disposition: form-data; name="file"; filename="audio.wav"\r\n' +
      'Content-Type: audio/wave\r\n' +
      '\r\n',
    DelayedStream {
      source: [ReadStream],
      dataSize: 0,
      maxDataSize: Infinity,
      pauseStream: true,
      _maxDataSizeExceeded: false,
      _released: false,
      _bufferedEvents: [Array],
      _events: [Object: null prototype],
      _eventsCount: 1
    },
    [Function: bound ],
    '----------------------------736652610391458148561295\r\n' +
      'Content-Disposition: form-data; name="model"\r\n' +
      '\r\n',
    'whisper-1',
    [Function: bound ]
  ],
  _currentStream: null,
  _insideLoop: false,
  _pendingNext: false,
  _boundary: '--------------------------736652610391458148561295'
}
Calling Whisper status 400
Calling Whisper json [Function: json]
Calling Whisper text undefined
1. OPEN AI ERROR:
2. Could not parse multipart form
wait  - compiling...
event - compiled successfully in 18 ms (38 modules)

我的笔记本电脑上运行着这个。

5gfr0r5j

5gfr0r5j1#

这可能与您创建的ReadStream有关-它可能没有正确地阅读文件。或者是音频文件类型-有一个已知的问题与某些音频编解码器/音频录制与不同的浏览器没有被正确识别的耳语API。https://community.openai.com/t/whisper-api-cannot-read-files-correctly/93420

相关问题