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" }
);