javascript 发送音频文件打开人工智能耳语模型

8mmmxcuj  于 2023-06-20  发布在  Java
关注(0)|答案(2)|浏览(138)

我正在将我录制的音频文件转换为一个blob对象,然后使用文件读取器阅读它,以发出打开ai whisper模型的帖子请求。它需要一个音频文件和模型名称,即whisper-1
我得到的错误是
1 validation error for Request\nbody -> file\n field required (type=value_error.missing)
代码在下面

const handleUpload = () => {
    const audioBlob = new Blob(audioChunks, { type: "audio/webm" });

    const reader = new FileReader();

    reader.readAsDataURL(audioBlob);

    reader.onload = async () => {
      const audioBase64 = await reader.result.split(",")[1];
      const payload = {
        audio: audioBase64,
        model: "whisper-1",
      };

      const payloadString =  JSON.stringify(payload);

      fetch("https://api.openai.com/v1/audio/transcriptions", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${key}`,
        },
        body: payloadString,
      })
        .then((response) => response.json())
        .then((data) => console.log(data))
        .catch((error) => console.error(error));
    };
  };
jogvjijk

jogvjijk1#

一旦你得到audio file,把它传递给下面的函数,你就会得到转录。
它使用openai's transcription api

import FormData from "form-data";
import axios from 'axios'

const transcribe = async (file) => {
  let data = new FormData();

  data.append("file", fs.createReadStream(file));
  data.append("model", "whisper-1");
  data.append("language", "en");

  let config = {
    method: "post",
    maxBodyLength: Infinity,
    url: "https://api.openai.com/v1/audio/transcriptions",
    headers: {
      Authorization:
        `Bearer ${process.env.OPENAI_API_KEY}`,
      "Content-Type": "multipart/form-data",
      ...data.getHeaders(),
    },
    data: data,
  };

  try {
    const response = await axios.request(config);
    const data = response.data;

    return { data };
  } catch (error) {
    return {};
  }
};

我觉得你现在的问题是:

const payload = {
        audio: audioBase64,
        model: "whisper-1",
      };

audio替换为file,作为payload object中的key。像这样:

const payload = {
        file: audioBase64,
        model: "whisper-1",
      };
7gs2gvoe

7gs2gvoe2#

https://api.openai.com/v1/audio/transcriptions- create transcription API采用multipart/form-data。

curl https://api.openai.com/v1/audio/transcriptions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: multipart/form-data" \
  -F file="@/path/to/file/audio.mp3" \
  -F model="whisper-1"

你可以使用fs模块或者使用form-data发送数据作为表单类型

相关问题