当前位置: 首页 > article >正文

OpenAI transcription API bad request

题意:“OpenAI 转录 API 错误请求”

问题背景:

const FormData = require('form-data');

    const data = new FormData();

    console.log('buffer: ', buffer);
    console.log('typeof buffer: ', typeof buffer);

    const filename = new Date().getTime().toString() + '.webm';
    data.append('model', 'whisper-1');
    data.append('file', bufferToStream(buffer));
    
    console.log('data: ', data);

    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,
    };

    await axios.request(config)

I am trying to post a file to openAI to get transcription. But I constantly get 400 bad request error.

“我正在尝试向 OpenAI 发送一个文件以获取转录。但是我不断收到 400 错误请求。”

I am using JavaScript (not TypeScript) so I can't use the FormData package, at least I have tried it and it didn't help me.

“我正在使用 JavaScript(而不是 TypeScript),所以我不能使用 FormData 包。至少我尝试过了,它没有帮助我。”

Error: Data after transformation must be a string, an Array Buffer, a Buffer, or a Stream

I tried Postman and it works, which provides me with the following code snippet:

“我尝试了 Postman,它有效,这里是它给我的代码片段:”

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
let data = new FormData();
data.append('model', 'whisper-1');
data.append('file', fs.createReadStream('/Users/UserName/Downloads/session-1708252969167.webm'));

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.openai.com/v1/audio/transcriptions',
  headers: { 
    'Authorization': {{OPENAI_API_KEY}}, 
    'Cookie': '__cf_bm=5L5OJyh1Yd28yUT9Uv0TT9F5POhL1fzeraSZF1C.9OA-1708337727-1.0-AV+XT0hftIn7mtJxICJduMQBpFcB9UQvbil6GmgI2BX7rw0BZRvuseeN0QgeuIt7KyNTxGJs/xcWIKMpkQdvzC4=; _cfuvid=3uLOzYX3DlLxSjGMLDqD0qcGIj.4zkEXt2czm9OiEfU-1708251423683-0.0-604800000', 
    ...data.getHeaders()
  },
  data : data
};

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});

I tried this code snippet as well, but it throw me a 400 error.

“我也尝试了这个代码片段,但它仍然返回了 400 错误。”

What did I do wrong?        

“我哪里做错了?”

问题解决:

This code will work        

“这段代码会有效。”

Save as trans.js

const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');

// Function to make the API call
async function callOpenAI() {
  try {
    const fileStream = fs.createReadStream('audio.webm');

    const formData = new FormData();
    formData.append('file', fileStream);
    formData.append('model', 'whisper-1');

    const response = await axios.post('https://api.openai.com/v1/audio/transcriptions', formData, {
      headers: {
        'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
        ...formData.getHeaders() // Get headers manually
      }
    });

    console.log(response.data);
  } catch (error) {
    console.error('Error:', error.response.data);
  }
}

// Call the function
callOpenAI();

Check API by Postman

Set environment variable for API KEY

On Git Bash

OPENAI_API_KEY=[your API KEY]

Install dependencies        安装依赖
npm install axios fs form-data
Run it        运行
node trans.js
Result        结果


http://www.kler.cn/a/291165.html

相关文章:

  • 行政管理痛点解决方案:OA系统助力企业提效减负
  • PHY6239:具有高精确度AFE的无线MCU芯片,常用在智能穿戴上
  • Spring MVC 中,处理异常的 6种方式
  • 自然语言处理学什么
  • 基于微信小程序的短视频系统(SpringBoot)+文档
  • 【Linux】结构化命令:for命令
  • ubuntu20.04 colmap安装
  • 假期学习-- iOS runloop了解和使用
  • 【STM32】寄存器点灯
  • 比特币客户端和API
  • 日程安排组件DHTMLX Scheduler v7.1 - 支持RFC-5545格式
  • VSCode中多行文本的快速前后缩进
  • kitti深度估计数据集介绍
  • Python知识点:Python开发中,如何使用Git进行版本控制
  • WEB渗透Linux提权篇-可写文件提权
  • 集成电路学习:什么是MOSFET(MOS管)
  • 【kubernetes】污点配置、cm、secret练习
  • 数学基础 -- 线性代数之共轭转置矩阵
  • 体会循环---冒泡排序
  • 2024.9.4
  • 在js渲染的dom中的事件中传递对象
  • 米家商城主题 html 页面源码分享,可用于网页设计作业
  • 室内导航定位系统在医院的应用与部署
  • Kubernetes学习指南:保姆级实操手册05——配置集群HA负载均衡
  • 智能巡检机器人创新设计的关键技术
  • Xml 映射文件中常见的标签