如需完整的文件索引,請參閱 llms.txt。在文件頁面的網址末尾加上 .md ,即可取得該頁面的 Markdown 版本。
主要導覽
主題

在 Chat Completions 中使用音訊

為現有的 Chat Completions 應用程式加入音訊輸入與輸出功能。

如果你已經使用 Chat Completions 端點 建立了以文字互動的 LLM 應用程式,也可以為它加入音訊功能。例如,如果你的對話應用程式支援文字輸入,只要將 audio 加入 modalities 陣列,並使用 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
const audio = response.choices[0].message.audio;
if (!audio) throw new Error("The response did not include audio.");
writeFileSync("dog.wav", Buffer.from(audio.data, "base64"), {
  encoding: "utf-8",
});