For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主导航

Chat Completions 中的音频

为现有的 Chat Completions 应用添加音频输入和输出功能。

如果您已有一个使用 Chat Completions 端点的文本 LLM 应用,可能希望为其添加音频功能。例如,如果您的聊天应用支持文本输入,您可以添加音频输入和输出功能:在 modalities 数组中加入 audio,并使用音频模型,例如 gpt-audio-1.5

Responses API 文档目前介绍的是 文本和图像输入、文本输出。对于这种音频聊天模式,请使用 Chat Completions 并搭配支持音频的模型。

根据提示生成接近真人语音的音频回复
import { writeFileSync } from "node:fs";
import OpenAI from "openai";

const openai = new OpenAI();

// Generate an audio response to the given prompt
const response = await openai.chat.completions.create({
  model: "gpt-audio-1.5",
  modalities: ["text", "audio"],
  audio: { voice: "alloy", format: "wav" },
  messages: [
    {
      role: "user",
      content: "Is a golden retriever a good family dog?",
    },
  ],
  store: true,
});

// Inspect returned data
console.log(response.choices[0]);

// Write audio data to a file
writeFileSync(
  "dog.wav",
  Buffer.from(response.choices[0].message.audio.data, "base64"),
  { encoding: "utf-8" }
);