Audio API 提供两个语音转文本端点:
transcriptions
translations
过去,这两个端点均由我们开源的 Whisper 模型 (whisper-1) 提供支持。该 transcriptions 端点现在还支持更高质量的模型快照,但对参数的支持有限:
gpt-4o-mini-transcribe
gpt-4o-transcribe
gpt-4o-transcribe-diarize
所有端点均可用于:
- 将音频转录为音频所使用的任何语言。
- 将音频翻译并转录为英文。
目前,文件上传的大小限制为 25 MB,支持以下输入文件类型: mp3, mp4, mpeg, mpga, m4a, wav,且 webm。当以数据 URL 形式提供时,用于说话人分离的已知说话人参考音频片段也接受相同的格式。
请使用本指南进行文件上传和有界音频请求。如果您的应用程序需要从麦克风、通话或媒体流中获取实时转录增量,请使用 实时转录
instead.
转录 API 接收您要转录的音频文件以及音频转录所需的输出文件格式作为输入。所有模型都支持相同的输入格式集。在输出方面:
whisper-1 支持 json, text, srt, verbose_json,且 vtt.
gpt-4o-transcribe and gpt-4o-mini-transcribe 支持 json or plain text.
gpt-4o-transcribe-diarize 支持 json, text,且 diarized_json (这会向响应中添加说话者片段)。
1
2
3
4
5
6
7
8
9
10
11
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream("/path/to/file/audio.mp3"),
model: "gpt-4o-transcribe",
});
console.log(transcription.text);
1
2
3
4
5
6
7
8
9
10
11
from openai import OpenAI
client = OpenAI()
audio_file= open("/path/to/file/audio.mp3", "rb")
transcription = client.audio.transcriptions.create(
model="gpt-4o-transcribe",
file=audio_file
)
print(transcription.text)
1
2
3
4
5
openai audio:transcriptions create \
--model gpt-4o-transcribe \
--file /path/to/file/audio.mp3 \
--raw-output \
--transform text
1
2
3
4
5
6
curl --request POST \
--url https://api.openai.com/v1/audio/transcriptions \
--header "Authorization: Bearer $OPENAI_API_KEY" \
--header 'Content-Type: multipart/form-data' \
--form file=@/path/to/file/audio.mp3 \
--form model=gpt-4o-transcribe
默认情况下,响应类型将为包含原始文本的 json。
{
"text": "Imagine the wildest idea that you've ever had, and you're curious about how it might scale to something that's a 100, a 1,000 times bigger.
....
}
Audio API 还允许您在请求中设置额外的参数。例如,如果您想设置 response_format as text,你的请求如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream("/path/to/file/speech.mp3"),
model: "gpt-4o-transcribe",
response_format: "text",
});
console.log(transcription.text);
1
2
3
4
5
6
7
8
9
10
11
12
from openai import OpenAI
client = OpenAI()
audio_file = open("/path/to/file/speech.mp3", "rb")
transcription = client.audio.transcriptions.create(
model="gpt-4o-transcribe",
file=audio_file,
response_format="text"
)
print(transcription.text)
1
2
3
4
5
6
7
curl --request POST \
--url https://api.openai.com/v1/audio/transcriptions \
--header "Authorization: Bearer $OPENAI_API_KEY" \
--header 'Content-Type: multipart/form-data' \
--form file=@/path/to/file/speech.mp3 \
--form model=gpt-4o-transcribe \
--form response_format=text
The API 参考 包含了所有可用参数的完整列表。
gpt-4o-transcribe and gpt-4o-mini-transcribe 支持 json or text
响应并允许提示和 logprobs。 gpt-4o-transcribe-diarize 会添加说话者标签,但在您的音频超过 30 秒时需要 chunking_strategy (建议使用"auto" ),并且不支持提示、logprobs 或 timestamp_granularities[].
gpt-4o-transcribe-diarize 可生成具备说话者意识的转录内容。请求 diarized_json 响应格式,以接收包含 speaker, start,且 end 元数据的片段数组。设置 chunking_strategy (无论是 "auto" 还是语音活动检测配置),以便服务可以将音频分割成多个片段;当输入超过 30 秒时,这是必需的。
您可以选择性地使用以下方式提供最多四个简短的音频参考 known_speaker_names[] and known_speaker_references[] 以将片段映射到已知说话人。提供 2 到 10 秒的参考片段,格式须为主音频上传支持的任何输入格式;当使用多部分表单数据时,请将其编码为 数据 URL 使用 multipart 表单数据时。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const agentRef = fs.readFileSync("agent.wav").toString("base64");
const transcript = await openai.audio.transcriptions.create({
file: fs.createReadStream("meeting.wav"),
model: "gpt-4o-transcribe-diarize",
response_format: "diarized_json",
chunking_strategy: "auto",
extra_body: {
known_speaker_names: ["agent"],
known_speaker_references: ["data:audio/wav;base64," + agentRef],
},
});
for (const segment of transcript.segments) {
console.log(`${segment.speaker}: ${segment.text}`, segment.start, segment.end);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import base64
from openai import OpenAI
client = OpenAI()
def to_data_url(path: str) -> str:
with open(path, "rb") as fh:
return "data:audio/wav;base64," + base64.b64encode(fh.read()).decode("utf-8")
with open("meeting.wav", "rb") as audio_file:
transcript = client.audio.transcriptions.create(
model="gpt-4o-transcribe-diarize",
file=audio_file,
response_format="diarized_json",
chunking_strategy="auto",
extra_body={
"known_speaker_names": ["agent"],
"known_speaker_references": [to_data_url("agent.wav")],
},
)
for segment in transcript.segments:
print(segment.speaker, segment.text, segment.start, segment.end)
1
2
3
4
5
6
7
8
9
10
curl --request POST \
--url https://api.openai.com/v1/audio/transcriptions \
--header "Authorization: Bearer $OPENAI_API_KEY" \
--header 'Content-Type: multipart/form-data' \
--form file=@/path/to/file/meeting.wav \
--form model=gpt-4o-transcribe-diarize \
--form response_format=diarized_json \
--form chunking_strategy=auto \
--form 'known_speaker_names[]=agent' \
--form 'known_speaker_references[]=data:audio/wav;base64,AAA...'
当 stream=true,带说话人分离的响应会输出 transcript.text.segment 每当片段完成时触发事件。 transcript.text.delta 事件包含一个 segment_id 字段,但分离的增量数据不会流式传输部分说话人分配,直到每个片段最终确定。
gpt-4o-transcribe-diarize 目前仅通过
/v1/audio/transcriptions 可用,且尚不支持 Realtime API。
翻译 API 接受任何受支持语言的音频文件作为输入,并在必要时将音频转录为英语。这与我们的 /Transcripts 端点不同,因为输出不再是原始输入语言,而是被翻译为英文文本。此端点仅支持 whisper-1 model.
1
2
3
4
5
6
7
8
9
10
11
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const translation = await openai.audio.translations.create({
file: fs.createReadStream("/path/to/file/german.mp3"),
model: "whisper-1",
});
console.log(translation.text);
1
2
3
4
5
6
7
8
9
10
11
from openai import OpenAI
client = OpenAI()
audio_file = open("/path/to/file/german.mp3", "rb")
translation = client.audio.translations.create(
model="whisper-1",
file=audio_file,
)
print(translation.text)
1
2
3
4
5
6
curl --request POST \
--url https://api.openai.com/v1/audio/translations \
--header "Authorization: Bearer $OPENAI_API_KEY" \
--header 'Content-Type: multipart/form-data' \
--form file=@/path/to/file/german.mp3 \
--form model=whisper-1 \
在这种情况下,输入的音频是德语,输出的文本如下所示:
Hello, my name is Wolfgang and I come from Germany. Where are you heading today?
目前我们仅支持翻译为英语。
我们目前 支持以下语言 通过 transcriptions and translations endpoint:
南非语、阿拉伯语、亚美尼亚语、阿塞拜疆语、白俄罗斯语、波斯尼亚语、保加利亚语、加泰罗尼亚语、中文、克罗地亚语、捷克语、丹麦语、荷兰语、英语、爱沙尼亚语、芬兰语、法语、加利西亚语、德语、希腊语、希伯来语、印地语、匈牙利语、冰岛语、印度尼西亚语、意大利语、日语、卡纳达语、哈萨克语、韩语、拉脱维亚语、立陶宛语、马其顿语、马来语、马拉地语、毛利语、尼泊尔语、挪威语、波斯语、波兰语、葡萄牙语、罗马尼亚语、俄语、塞尔维亚语、斯洛伐克语、斯洛文尼亚语、西班牙语、斯瓦希里语、瑞典语、他加禄语、泰米尔语、泰语、土耳其语、乌克兰语、乌尔都语、越南语和威尔士语。
虽然底层模型在 98 种语言上进行了训练,但我们仅列出了词错率低于 50% 的语言 词错率 (WER) 是语音转文本模型准确性的行业标准基准。模型也会返回上述未列出语言的结果,但质量会较低。
对于基于 GPT-4o 的模型,我们支持部分 ISO 639-1 和 639-3 语言代码。对于我们没有涵盖的语言代码,请尝试通过提示指定特定语言(例如“Output in English”)。
默认情况下,Transcriptions API 将输出所提供音频的文本转录结果。 timestamp_granularities[] 参数 支持更具结构化且带时间戳的 JSON 输出格式,时间戳可精确到片段、单词级别或两者兼有。这为转录文本和视频编辑实现了词级别的精度,从而允许删除与特定单词相关联的特定帧。
1
2
3
4
5
6
7
8
9
10
11
12
13
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream("audio.mp3"),
model: "whisper-1",
response_format: "verbose_json",
timestamp_granularities: ["word"]
});
console.log(transcription.words);
1
2
3
4
5
6
7
8
9
10
11
12
13
from openai import OpenAI
client = OpenAI()
audio_file = open("/path/to/file/speech.mp3", "rb")
transcription = client.audio.transcriptions.create(
file=audio_file,
model="whisper-1",
response_format="verbose_json",
timestamp_granularities=["word"]
)
print(transcription.words)
1
2
3
4
5
6
7
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 "timestamp_granularities[]=word" \
-F model="whisper-1" \
-F response_format="verbose_json"
The timestamp_granularities[] 参数仅支持 whisper-1.
默认情况下,Transcriptions API 仅支持小于 25 MB 的文件。如果你的音频文件超过此大小,则需要将其切分为 25 MB 或更小的块,或者使用压缩的音频格式。为了获得最佳性能,我们建议你避免在句子中间切分音频,因为这可能会导致部分上下文丢失。
处理此问题的一种方法是使用 PyDub 开源 Python 包 to split the audio:
1
2
3
4
5
6
7
8
9
10
from pydub import AudioSegment
song = AudioSegment.from_mp3("good_morning.mp3")
ten_minutes = 10 * 60 * 1000
first_10_minutes = song[:ten_minutes]
first_10_minutes.export("good_morning_10.mp3", format="mp3")
OpenAI 不对 PyDub 等第三方软件的可用性或安全性做任何保证。
你可以使用 提示词 来提高 Transcriptions API 生成的转录质量。
1
2
3
4
5
6
7
8
9
10
11
12
13
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream("/path/to/file/speech.mp3"),
model: "gpt-4o-transcribe",
response_format: "text",
prompt:"The following conversation is a lecture about the recent developments around OpenAI, GPT-4.5 and the future of AI.",
});
console.log(transcription.text);
1
2
3
4
5
6
7
8
9
10
11
12
13
from openai import OpenAI
client = OpenAI()
audio_file = open("/path/to/file/speech.mp3", "rb")
transcription = client.audio.transcriptions.create(
model="gpt-4o-transcribe",
file=audio_file,
response_format="text",
prompt="The following conversation is a lecture about the recent developments around OpenAI, GPT-4.5 and the future of AI."
)
print(transcription.text)
1
2
3
4
5
6
7
curl --request POST \
--url https://api.openai.com/v1/audio/transcriptions \
--header "Authorization: Bearer $OPENAI_API_KEY" \
--header 'Content-Type: multipart/form-data' \
--form file=@/path/to/file/speech.mp3 \
--form model=gpt-4o-transcribe \
--form prompt="The following conversation is a lecture about the recent developments around OpenAI, GPT-4.5 and the future of AI."
For gpt-4o-transcribe and gpt-4o-mini-transcribe,你可以使用 prompt 参数,通过向模型提供额外上下文来提高转录质量,这类似于你提示其他 GPT-4o 模型的方式。目前,提示功能不支持 gpt-4o-transcribe-diarize.
以下是一些提示在不同场景中如何提供帮助的示例:
- 提示可以帮助纠正模型在音频中误识别的特定单词或首字母缩略词。例如,以下提示改善了 DALL·E 和 GPT-3 这两个词的转录效果,它们之前被错误地写成了“GDP 3”和“DALI”:“这份转录是关于 OpenAI 的,它创造了像 DALL·E、GPT-3 和 ChatGPT 这样的技术,希望有一天能构建出一个造福全人类的 AGI 系统。"
- 为了保持被切分为多个片段的文件的上下文,可以使用前一片段的转录内容来提示模型。模型会利用先前音频中的相关信息,从而提高转录准确性。
whisper-1 模型仅考虑提示的最后 224 个 token,并会忽略之前的内容。对于多语言输入,Whisper 使用自定义的分词器。对于纯英文输入,它使用标准的 GPT-2 分词器。你可以在开源的 Whisper Python 包.
- 有时模型会在转录中省略标点符号。为了防止这种情况,请使用包含标点符号的简单提示:“Hello, welcome to my lecture.”
- 模型可能也会省略音频中常见的语气词。如果你希望在转录中保留这些语气词,请使用包含它们的提示:“Umm, let me think like, hmm… Okay, here’s what I’m, like, thinking.”
- 某些语言可以有不同的书写方式,例如简体中文或繁体中文。默认情况下,模型可能不会总是采用你期望的书写风格来生成转录内容。你可以通过使用符合你偏好的书写风格的提示来改善这一点。
For whisper-1,模型会尝试匹配提示词的风格,因此如果提示词使用了大写字母和标点符号,生成的文本也更有可能使用它们。但是,当前的提示系统比我们的其他语言模型更受限,对生成文本的控制能力有限。
你可以在以下链接找到更多关于改进 whisper-1 转录的示例,请参见 提高可靠性 section.
根据你的使用场景,以及你是要转录已完成的录音,还是要处理持续进行的音频流并使用 OpenAI 进行轮次检测,有两种流式转录的方法。
如果你已经有一段完整的录音(无论它是一个音频文件,还是你正在使用自己的轮次检测机制(如按键说话)),你可以结合使用我们的 Transcription API 与 stream=True 来接收以下数据流: 转录事件 一旦模型完成该部分音频的转录。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const stream = await openai.audio.transcriptions.create({
file: fs.createReadStream("/path/to/file/speech.mp3"),
model: "gpt-4o-mini-transcribe",
response_format: "text",
stream: true,
});
for await (const event of stream) {
console.log(event);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from openai import OpenAI
client = OpenAI()
audio_file = open("/path/to/file/speech.mp3", "rb")
stream = client.audio.transcriptions.create(
model="gpt-4o-mini-transcribe",
file=audio_file,
response_format="text",
stream=True
)
for event in stream:
print(event)
1
2
3
4
5
6
7
curl --request POST \
--url https://api.openai.com/v1/audio/transcriptions \
--header "Authorization: Bearer $OPENAI_API_KEY" \
--header 'Content-Type: multipart/form-data' \
--form file=@example.wav \
--form model=whisper-1 \
--form stream=True
您将收到一连串 transcript.text.delta 事件,一旦模型完成该部分音频的转录,随后会收到一个 transcript.text.done 事件(在转录完成时),其中包含完整的转录文本。当使用 response_format="diarized_json",该流还会发出 transcript.text.segment 带有说话人标签的事件,每次一个片段被最终确认时。
此外,您可以使用 include[] 参数,在响应中包含 logprobs 以获取转录中各 Token 的对数概率。这有助于判断模型对特定部分转录结果的置信度。
对于来自麦克风、通话或媒体流的实时音频,请使用 实时转录 指南,而不是上面面向文件的流式路径。它涵盖了当前的转录会话流程以及推荐的实时路径,包含 gpt-realtime-whisper.
使用 Whisper 时面临的最常见挑战之一是,模型通常无法识别不常见的单词或首字母缩略词。以下是一些可在此类情况下提高 Whisper 可靠性的不同技巧:
第一种方法是使用可选的 prompt 参数来传递包含正确拼写的字典。
因为 Whisper 没有经过指令跟随技术的训练,所以它的运行方式更像是一个基础的 GPT 模型。请记住,Whisper 仅会考虑 prompt 的前 224 个 token。
1
2
3
4
5
6
7
8
9
10
11
12
13
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream("/path/to/file/speech.mp3"),
model: "whisper-1",
response_format: "text",
prompt:"ZyntriQix, Digique Plus, CynapseFive, VortiQore V8, EchoNix Array, OrbitalLink Seven, DigiFractal Matrix, PULSE, RAPT, B.R.I.C.K., Q.U.A.R.T.Z., F.L.I.N.T.",
});
console.log(transcription.text);
1
2
3
4
5
6
7
8
9
10
11
12
13
from openai import OpenAI
client = OpenAI()
audio_file = open("/path/to/file/speech.mp3", "rb")
transcription = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text",
prompt="ZyntriQix, Digique Plus, CynapseFive, VortiQore V8, EchoNix Array, OrbitalLink Seven, DigiFractal Matrix, PULSE, RAPT, B.R.I.C.K., Q.U.A.R.T.Z., F.L.I.N.T."
)
print(transcription.text)
1
2
3
4
5
6
7
curl --request POST \
--url https://api.openai.com/v1/audio/transcriptions \
--header "Authorization: Bearer $OPENAI_API_KEY" \
--header 'Content-Type: multipart/form-data' \
--form file=@/path/to/file/speech.mp3 \
--form model=whisper-1 \
--form prompt="ZyntriQix, Digique Plus, CynapseFive, VortiQore V8, EchoNix Array, OrbitalLink Seven, DigiFractal Matrix, PULSE, RAPT, B.R.I.C.K., Q.U.A.R.T.Z., F.L.I.N.T."
虽然这种方法提高了可靠性,但它被限制在 224 个 token 内,因此你的 SKU 列表必须相对较短,才能使其成为一种可扩展的解决方案。
第二种方法涉及使用 GPT-4 或 GPT-3.5-Turbo 进行后处理步骤。
我们首先通过 system_prompt 变量为 GPT-4 提供指令。类似于我们之前处理 prompt 参数的做法,我们可以定义自己的公司和产品名称。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const systemPrompt = `
You are a helpful assistant for the company ZyntriQix. Your task is
to correct any spelling discrepancies in the transcribed text. Make
sure that the names of the following products are spelled correctly:
ZyntriQix, Digique Plus, CynapseFive, VortiQore V8, EchoNix Array,
OrbitalLink Seven, DigiFractal Matrix, PULSE, RAPT, B.R.I.C.K.,
Q.U.A.R.T.Z., F.L.I.N.T. Only add necessary punctuation such as
periods, commas, and capitalization, and use only the context provided.
`;
const transcript = await transcribe(audioFile);
const completion = await openai.chat.completions.create({
model: "gpt-4.1",
temperature: temperature,
messages: [
{
role: "system",
content: systemPrompt
},
{
role: "user",
content: transcript
}
],
store: true,
});
console.log(completion.choices[0].message.content);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
system_prompt = """
You are a helpful assistant for the company ZyntriQix. Your task is to correct
any spelling discrepancies in the transcribed text. Make sure that the names of
the following products are spelled correctly: ZyntriQix, Digique Plus,
CynapseFive, VortiQore V8, EchoNix Array, OrbitalLink Seven, DigiFractal
Matrix, PULSE, RAPT, B.R.I.C.K., Q.U.A.R.T.Z., F.L.I.N.T. Only add necessary
punctuation such as periods, commas, and capitalization, and use only the
context provided.
"""
def generate_corrected_transcript(temperature, system_prompt, audio_file):
response = client.chat.completions.create(
model="gpt-4.1",
temperature=temperature,
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": transcribe(audio_file, "")
}
]
)
return completion.choices[0].message.content
corrected_text = generate_corrected_transcript(
0, system_prompt, fake_company_filepath
)
如果你在自己的音频文件上尝试此操作,你会发现 GPT-4 修正了转录文本中的许多拼写错误。得益于更大的上下文窗口,这种方法可能比使用 Whisper 的 prompt 参数更具可扩展性。它也更可靠,因为 GPT-4 可以通过指令进行引导,而 Whisper 由于缺乏指令跟随能力,无法做到这一点。