如果您已有完整录音,或要处理范围明确的音频请求,请使用文件转录。上传音频后,您可以接收最终转录文本,也可以在模型处理文件时以流式方式接收文本。
首先使用 gpt-transcribe 。这是按录音原始语言转录语音的推荐模型。只有在需要说话人标签、词级时间戳、字幕格式或翻译成英语时,才使用专用模型。
文件大小上限为 25 MB。支持的输入格式包括 mp3、mp4、mpeg、mpga、m4a、wav 和 webm。
对于仍在从麦克风、通话或媒体流持续传入的音频,请使用
实时转录 。
将音频文件发送到 /v1/audio/transcriptions,并使用 gpt-transcribe:
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("fixtures/audio.wav"),
model: "gpt-transcribe",
});
console.log(transcription.text); 1
2
3
4
5
6
7
8
9
10 from openai import OpenAI
client = OpenAI()
audio_file = open ( "audio.wav" , "rb" )
transcription = client.audio.transcriptions.create(
model = "gpt-transcribe" , file = audio_file
)
print (transcription.text) 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 package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
file, err := os.Open("fixtures/audio.wav")
if err != nil {
panic(err)
}
defer file.Close()
client := openai.NewClient()
transcription, err := client.Audio.Transcriptions.New(context.Background(), openai.AudioTranscriptionNewParams{
File: file,
Model: "gpt-transcribe",
})
if err != nil {
panic(err)
}
fmt.Println(transcription.Text)
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.audio.transcriptions.TranscriptionCreateParams;
import java.nio.file.Path;
var result =
client
.audio()
.transcriptions()
.create(
TranscriptionCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_AUDIO_PATH")))
.model("gpt-transcribe")
.build());
System.out.println(result.asTranscription().text()); 1
2
3
4
5
6
7
8
9
10
11
12
13 using OpenAI.Audio;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-transcribe";
AudioClient client = new(model, key);
await using FileStream audio = File.OpenRead("audio.wav");
AudioTranscription transcription = await client.TranscribeAudioAsync(
audio,
"audio.wav"
);
Console.WriteLine(transcription.Text); 1
2
3
4
5
6
7
8
9
10 require "openai"
require "pathname"
client = OpenAI::Client.new
audio = Pathname("audio.wav")
transcript = client.audio.transcriptions.create(
file: audio,
model: "gpt-transcribe"
)
puts(transcript.text) 1
2
3
4
5 openai audio:transcriptions create \
--model gpt-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-transcribe
模型以 JSON 格式返回转录文本和检测到的语言:
1 2 3 4 {
"text" : "Bonjour, pouvez-vous m'entendre ?" ,
"languages" : [{ "code" : "fr" }]
}
当模型无法可靠地判断语言时,会返回 "languages": []。完整的请求和响应字段请参阅音频 API 参考 。
添加转录上下文
使用 gpt-transcribe 时,配合使用 prompt、keywords 和 languages,以改善领域术语和多语言音频的转录效果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const request = {
model: "gpt-transcribe",
file: fs.createReadStream("fixtures/audio.wav"),
prompt: "A customer support call about a premium plan and account AC-42.",
};
const transcription = await openai.audio.transcriptions.create(request, {
body: {
...request,
keywords: ["premium plan", "AC-42", "billing"],
languages: ["en", "fr"],
},
});
console.log(transcription.text); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 from openai import OpenAI
client = OpenAI()
with open ( "meeting.wav" , "rb" ) as audio_file:
transcription = client.audio.transcriptions.create(
model = "gpt-transcribe" ,
file = audio_file,
prompt = "A customer support call about a premium plan and account AC-42." ,
extra_body = {
"keywords" : [ "premium plan" , "AC-42" , "billing" ],
"languages" : [ "en" , "fr" ],
},
)
print (transcription.text) 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
30
31
32
33 package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
file, err := os.Open("fixtures/audio.wav")
if err != nil {
panic(err)
}
defer file.Close()
parameters := openai.AudioTranscriptionNewParams{
File: file,
Model: "gpt-transcribe",
Prompt: openai.String("A customer support call about a premium plan and account AC-42."),
}
parameters.SetExtraFields(map[string]any{
"keywords": []string{"premium plan", "AC-42", "billing"},
"languages": []string{"en", "fr"},
})
client := openai.NewClient()
transcription, err := client.Audio.Transcriptions.New(context.Background(), parameters)
if err != nil {
panic(err)
}
fmt.Println(transcription.Text)
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.JsonValue;
import com.openai.models.audio.transcriptions.TranscriptionCreateParams;
import java.nio.file.Path;
import java.util.List;
var result =
client
.audio()
.transcriptions()
.create(
TranscriptionCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_AUDIO_PATH")))
.model("gpt-transcribe")
.prompt("A customer support call about a premium plan and account AC-42.")
.putAdditionalBodyProperty(
"keywords", JsonValue.from(List.of("premium plan", "AC-42", "billing")))
.putAdditionalBodyProperty("languages", JsonValue.from(List.of("en", "fr")))
.build());
System.out.println(result.asTranscription().text()); 1
2
3
4
5
6
7
8
9
10
11 require "openai"
require "pathname"
client = OpenAI::Client.new
audio = Pathname("audio.wav")
transcript = client.audio.transcriptions.create(
file: audio,
model: "gpt-transcribe",
keywords: ["OpenAI", "Responses API", "Codex"]
)
puts(transcript.text) 1
2
3
4
5
6
7
8
9
10
11 curl https://api.openai.com/v1/audio/transcriptions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F model="gpt-transcribe" \
-F file="@/path/to/file/meeting.wav" \
-F 'prompt=A customer support call about a premium plan and account AC-42.' \
-F 'keywords[]=premium plan' \
-F 'keywords[]=AC-42' \
-F 'keywords[]=billing' \
-F 'languages[]=en' \
-F 'languages[]=fr'
使用 prompt 提供有关录音的非结构化上下文。
使用 keywords 提供您预期会听到的具体术语。
使用 languages 指定预期的输入语言。
关键词仅作提示,并非必须输出的内容。请仅包含相关术语,并评估它们能否提高准确率,同时避免转录文本中出现录音未提及的术语。
对于 gpt-transcribe,languages 取代了单数形式的 language 字段。请勿同时发送这两个字段。每个关键词必须保持在同一行内,且不得包含 <、>、回车符或换行符。如果遇到其中任一字符,或者 prompt 超出模型的长度限制,API 会拒绝整个请求。
只有在需要识别录音中不同片段的说话人时,才使用 gpt-4o-transcribe-diarize。对于普通文件转录,不推荐使用这一专门用于说话人标注的模型。
请求使用 diarized_json 响应格式,即可接收带有 speaker、start 和 end 元数据的片段。对于超过 30 秒的音频,请将 chunking_strategy 设置为 "auto" 或语音活动检测配置。
您还可以通过 known_speaker_names[] 和 known_speaker_references[] 提供最多四段简短的参考音频,将片段对应到已知说话人。参考片段时长应为 2–10 秒,可使用主音频上传所支持的任意输入格式;使用多部分表单数据时,请将其编码为 data URL 。
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 import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const agentRef = fs.readFileSync("fixtures/agent.wav").toString("base64");
const transcript = await openai.audio.transcriptions.create({
file: fs.createReadStream("fixtures/meeting.wav"),
model: "gpt-4o-transcribe-diarize",
response_format: "diarized_json",
chunking_strategy: "auto",
known_speaker_names: ["agent"],
known_speaker_references: ["data:audio/wav;base64," + agentRef],
});
for (const segment of transcript.segments) {
if (!("speaker" in segment)) continue;
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
24
25 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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/shared/constant"
)
type diarizedTranscript struct {
Segments []struct {
Speaker string `json:"speaker"`
Text string `json:"text"`
Start float64 `json:"start"`
End float64 `json:"end"`
} `json:"segments"`
}
func main() {
agentAudio, err := os.ReadFile("fixtures/agent.wav")
if err != nil {
panic(err)
}
meeting, err := os.Open("fixtures/meeting.wav")
if err != nil {
panic(err)
}
defer meeting.Close()
client := openai.NewClient()
transcription, err := client.Audio.Transcriptions.New(context.Background(), openai.AudioTranscriptionNewParams{
File: meeting,
Model: "gpt-4o-transcribe-diarize",
ResponseFormat: openai.AudioResponseFormatDiarizedJSON,
ChunkingStrategy: openai.AudioTranscriptionNewParamsChunkingStrategyUnion{
OfAuto: constant.ValueOf[constant.Auto](),
},
KnownSpeakerNames: []string{"agent"},
KnownSpeakerReferences: []string{"data:audio/wav;base64," + base64.StdEncoding.EncodeToString(agentAudio)},
})
if err != nil {
panic(err)
}
var result diarizedTranscript
if err := json.Unmarshal([]byte(transcription.RawJSON()), &result); err != nil {
panic(err)
}
for _, segment := range result.Segments {
fmt.Println(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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 import com.fasterxml.jackson.databind.json.JsonMapper;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.audio.AudioResponseFormat;
import com.openai.models.audio.transcriptions.TranscriptionCreateParams;
import com.openai.models.audio.transcriptions.TranscriptionDiarized;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
Path audio = Path.of(System.getenv("OPENAI_EXAMPLE_AUDIO_PATH"));
Path speakerAudio = Path.of(System.getenv("OPENAI_EXAMPLE_SPEAKER_AUDIO_PATH"));
String speakerReference =
"data:audio/wav;base64,"
+ Base64.getEncoder().encodeToString(Files.readAllBytes(speakerAudio));
var result =
client
.audio()
.transcriptions()
.create(
TranscriptionCreateParams.builder()
.file(audio)
.model("gpt-4o-transcribe-diarize")
.responseFormat(AudioResponseFormat.DIARIZED_JSON)
.chunkingStrategyAuto()
.addKnownSpeakerName("agent")
.addKnownSpeakerReference(speakerReference)
.build());
TranscriptionDiarized diarized =
result.isDiarized()
? result.asDiarized()
: new JsonMapper()
.readValue(result.asTranscription().text(), TranscriptionDiarized.class);
for (var segment : diarized.segments()) {
System.out.println(
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
24
25
26
27 require "base64"
require "openai"
require "pathname"
client = OpenAI::Client.new
audio = Pathname("meeting.wav")
speaker_reference = Base64.strict_encode64(File.binread("agent.wav"))
transcript = client.audio.transcriptions.create(
file: audio,
model: "gpt-4o-transcribe-diarize",
response_format: :diarized_json,
chunking_strategy: :auto,
known_speaker_names: ["agent"],
known_speaker_references: ["data:audio/wav;base64,#{speaker_reference}"]
)
segments = Array(
transcript.to_h.fetch(:segments) do
raise "The transcription did not include speaker segments"
end
)
segments.each do |segment|
segment = Hash.try_convert(segment) or raise "Invalid speaker segment"
puts(
"#{segment.fetch(:speaker)}: #{segment.fetch(:text)} " \
"(#{segment.fetch(:start)}-#{segment.fetch(:end_)})"
)
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 字段,但增量内容不包含尚未确定的说话人分配结果。模型仅在片段最终确定时才为其分配说话人。
说话人标注可通过 /v1/audio/transcriptions 使用。
实时转录会话不支持此功能。
要将完整录音翻译成英语,请使用 /v1/audio/translations 并选择 whisper-1。转录会保留录音的原始语言,而此端点返回英语文本。
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("fixtures/german.wav"),
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 ( "german.wav" , "rb" )
translation = client.audio.translations.create(
model = "whisper-1" ,
file = audio_file,
)
print (translation.text) 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 package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
file, err := os.Open("fixtures/german.wav")
if err != nil {
panic(err)
}
defer file.Close()
client := openai.NewClient()
translation, err := client.Audio.Translations.New(context.Background(), openai.AudioTranslationNewParams{
File: file,
Model: openai.AudioModelWhisper1,
})
if err != nil {
panic(err)
}
fmt.Println(translation.Text)
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.audio.translations.TranslationCreateParams;
import java.nio.file.Path;
var result =
client
.audio()
.translations()
.create(
TranslationCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_AUDIO_PATH")))
.model("whisper-1")
.build());
System.out.println(result.asTranslation().text()); 1
2
3
4
5
6
7
8
9
10
11
12 using OpenAI.Audio;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
AudioClient client = new("whisper-1", key);
await using FileStream audio = File.OpenRead("german.wav");
AudioTranslation translation = await client.TranslateAudioAsync(
audio,
"german.wav"
);
Console.WriteLine(translation.Text); 1
2
3
4
5
6
7 require "openai"
require "pathname"
client = OpenAI::Client.new
audio = Pathname("german.wav")
translation = client.audio.translations.create(file: audio, model: "whisper-1")
puts(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?
此端点仅支持翻译成英语。
如果您知道预期的输入语言,请在使用 gpt-transcribe 时配合使用 languages。支持的语言代码格式包括:
ISO 639-1 代码,例如 en、es 和 fr。
部分 ISO 639-3 代码,例如 eng、spa、yue 和 cmn。
带有地区信息的 zh 区域设置代码,例如 zh-cn、zh-tw 和 zh-hk。
API 会拒绝不受支持或格式不正确的语言代码。响应还会列出模型能够可靠检测到的所有语言。
对于 whisper-1,请参阅 Whisper 语言列表 。Whisper 支持 98 种语言,但准确率因语言而异。接受单一语言提示的现有模型使用 language,而非 languages。
如果您需要词级或片段级时间戳,请使用 whisper-1。timestamp_granularities[] 参数 会返回结构化时间戳数据,用于字幕制作和视频编辑。
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("fixtures/audio.wav"),
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 ( "speech.wav" , "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
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
file, err := os.Open("fixtures/audio.wav")
if err != nil {
panic(err)
}
defer file.Close()
client := openai.NewClient()
transcription, err := client.Audio.Transcriptions.New(context.Background(), openai.AudioTranscriptionNewParams{
File: file,
Model: openai.AudioModelWhisper1,
ResponseFormat: openai.AudioResponseFormatVerboseJSON,
TimestampGranularities: []string{"word"},
})
if err != nil {
panic(err)
}
fmt.Println(transcription.Words)
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.audio.AudioResponseFormat;
import com.openai.models.audio.transcriptions.TranscriptionCreateParams;
import java.nio.file.Path;
var result =
client
.audio()
.transcriptions()
.create(
TranscriptionCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_AUDIO_PATH")))
.model("whisper-1")
.responseFormat(AudioResponseFormat.VERBOSE_JSON)
.addTimestampGranularity(TranscriptionCreateParams.TimestampGranularity.WORD)
.build());
result
.asVerbose()
.words()
.orElseThrow()
.forEach(
word -> System.out.println(word.word() + ": " + word.start() + " - " + word.end())); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 using OpenAI.Audio;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "whisper-1";
AudioClient client = new(model, key);
await using FileStream audio = File.OpenRead("speech.wav");
AudioTranscriptionOptions options = new()
{
ResponseFormat = AudioTranscriptionFormat.Verbose,
TimestampGranularities = AudioTimestampGranularities.Word,
};
AudioTranscription transcription = await client.TranscribeAudioAsync(
audio,
"speech.wav",
options
);
foreach (TranscribedWord word in transcription.Words)
{
Console.WriteLine(
$"{word.Word}: {word.StartTime.TotalSeconds:0.00}s - {word.EndTime.TotalSeconds:0.00}s"
);
} 1
2
3
4
5
6
7
8
9
10
11
12
13 require "openai"
require "pathname"
require "pp"
client = OpenAI::Client.new
audio = Pathname("audio.wav")
transcript = client.audio.transcriptions.create(
file: audio,
model: "whisper-1",
response_format: :verbose_json,
timestamp_granularities: [:word]
)
pp(transcript[: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"
仅 whisper-1 支持 timestamp_granularities[] 参数。
转录 API 接受的文件大小上限为 25 MB。对于更大的录音文件,请使用压缩音频格式,或将文件拆分为不超过 25 MB 的片段。避免在句子中间拆分,否则可能丢失上下文并降低准确率。
一种处理方法是使用 PyDub 开源 Python 包 拆分音频:
1
2
3
4
5
6
7
8
9
10 from pydub import AudioSegment
song = AudioSegment.from_wav( "good_morning.wav" )
# PyDub handles time in milliseconds
ten_minutes = 10 * 60 * 1000
first_10_minutes = song[:ten_minutes]
first_10_minutes.export( "good_morning_10.wav" , format = "wav" )
OpenAI 不对 PyDub 等第三方软件的可用性或安全性作任何保证。
使用提示 可以改善对名称、首字母缩略词、格式或录音中特有词汇的识别。使用 gpt-transcribe 时,请将提示与添加转录上下文 中介绍的 keywords 和 languages 配合使用。
现有的 gpt-4o-transcribe 和 gpt-4o-mini-transcribe 集成也支持使用提示。gpt-4o-transcribe-diarize 不支持提示。
适合使用提示的场景包括:
准确转录产品名称、技术术语和首字母缩略词。
沿用较长录音中上一个片段的上下文。
保留标点符号、大小写和语气填充词。
为某种语言选择首选的书写系统。
whisper-1 的提示长度上限为 224 个 Token,且对转录结果的控制能力不及推荐的转录模型。如果您的工作流程需要使用 Whisper,请参阅提高可靠性 。
文件转录可以在模型处理已录制完成的音频时,以流式方式返回部分文本。此过程无需实时会话。
使用 gpt-transcribe 时,设置 stream=true。模型逐段转录录音时,Transcriptions API 会返回转录事件 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14 import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const stream = await openai.audio.transcriptions.create({
file: fs.createReadStream("fixtures/speech.wav"),
model: "gpt-transcribe",
stream: true,
});
for await (const event of stream) {
console.log(event);
} 1
2
3
4
5
6
7
8
9
10
11
12
13 from openai import OpenAI
client = OpenAI()
audio_file = open ( "speech.wav" , "rb" )
stream = client.audio.transcriptions.create(
model = "gpt-transcribe" ,
file = audio_file,
stream = True ,
)
for event in stream:
print (event) 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 package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
file, err := os.Open("fixtures/speech.wav")
if err != nil {
panic(err)
}
defer file.Close()
client := openai.NewClient()
stream := client.Audio.Transcriptions.NewStreaming(context.Background(), openai.AudioTranscriptionNewParams{
File: file,
Model: "gpt-transcribe",
})
for stream.Next() {
fmt.Println(stream.Current().Type)
}
if err := stream.Err(); err != nil {
panic(err)
}
} 1
2
3
4
5
6
7
8
9
10
11 require "openai"
require "pathname"
client = OpenAI::Client.new
audio = Pathname("speech.wav")
stream = client.audio.transcriptions.create_streaming(
file: audio,
model: "gpt-transcribe"
)
stream.each { |event| puts(event.type) } 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=gpt-transcribe \
--form stream=true
模型在转录音频时会发出 transcript.text.delta 事件,然后在最后的 transcript.text.done 事件中返回完整的转录文本。对于使用 response_format="diarized_json" 的带说话人标签的转录,说话人分离模型还会在每个片段定稿时发出 transcript.text.segment 事件。
对于 gpt-transcribe,最后的事件还会包含检测到的语言:
1 2 3 4 5 {
"type" : "transcript.text.done" ,
"text" : "Bonjour, pouvez-vous m'entendre ?" ,
"languages" : [{ "code" : "fr" }]
}
现有的 gpt-4o-transcribe、gpt-4o-mini-transcribe 和
gpt-4o-transcribe-diarize 集成也支持文件流式转录。
whisper-1 不支持。
对于来自麦克风、通话或媒体流的实时音频,请参阅实时转录 指南,而非采用上述面向文件的流式转录方式。该指南介绍了当前的转录会话流程,以及推荐的使用 gpt-live-transcribe 的实时转录方式。
如果您使用 whisper-1 生成时间戳、字幕或进行翻译,以下技巧可以改善对生僻词和首字母缩略词的识别。对于新的通用转录需求,请从 gpt-transcribe 入手,改用转录上下文 。
使用 prompt 参数 第一种方法是使用可选的 prompt 参数传入包含正确拼写的词典。
Whisper 不会像通用文本模型那样遵循指令,且接受的提示最多为 224 个 Token。
1
2
3
4
5
6
7
8
9
10
11
12
13
14 import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream("fixtures/speech.wav"),
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); 1
2
3
4
5
6
7
8
9
10
11
12
13 from openai import OpenAI
client = OpenAI()
audio_file = open ( "speech.wav" , "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
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
file, err := os.Open("fixtures/speech.wav")
if err != nil {
panic(err)
}
defer file.Close()
client := openai.NewClient()
var transcription []byte
err = client.Post(context.Background(), "audio/transcriptions", openai.AudioTranscriptionNewParams{
File: file,
Model: openai.AudioModelWhisper1,
ResponseFormat: openai.AudioResponseFormatText,
Prompt: openai.String("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."),
}, &transcription)
if err != nil {
panic(err)
}
fmt.Println(string(transcription))
} 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 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.HttpResponse;
import com.openai.models.audio.AudioResponseFormat;
import com.openai.models.audio.transcriptions.TranscriptionCreateParams;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
try (HttpResponse result =
client
.audio()
.transcriptions()
.withRawResponse()
.create(
TranscriptionCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_AUDIO_PATH")))
.model("whisper-1")
.responseFormat(AudioResponseFormat.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.")
.build())) {
System.out.println(new String(result.body().readAllBytes(), StandardCharsets.UTF_8));
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 using OpenAI.Audio;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "whisper-1";
AudioClient client = new(model, key);
await using FileStream audio = File.OpenRead("speech.wav");
AudioTranscriptionOptions options = new()
{
ResponseFormat = AudioTranscriptionFormat.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.",
};
AudioTranscription transcription = await client.TranscribeAudioAsync(
audio,
"speech.wav",
options
);
Console.WriteLine(transcription.Text); 1
2
3
4
5
6
7
8
9
10
11 require "openai"
require "pathname"
client = OpenAI::Client.new
audio = Pathname("speech.wav")
transcript = client.audio.transcriptions.create(
file: audio,
model: "whisper-1",
prompt: "The speaker says OpenAI and Responses API"
)
puts(transcript.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 列表相对较短时,它才适合作为可扩展的解决方案。
使用文本模型进行后处理 第二种方法是使用文本模型对转录文本进行后处理。
通过 system_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 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 response.choices[ 0 ].message.content
corrected_text = generate_corrected_transcript( 0 , system_prompt, fake_company_filepath) 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
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.
`
func main() {
file, err := os.Open("fixtures/speech.wav")
if err != nil {
panic(err)
}
defer file.Close()
client := openai.NewClient()
transcription, err := client.Audio.Transcriptions.New(context.Background(), openai.AudioTranscriptionNewParams{
File: file,
Model: openai.AudioModelGPT4oTranscribe,
})
if err != nil {
panic(err)
}
completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "gpt-4.1",
Temperature: openai.Float(0),
Messages: []openai.ChatCompletionMessageParamUnion{
openai.SystemMessage(systemPrompt),
openai.UserMessage(transcription.Text),
},
Store: openai.Bool(true),
})
if err != nil {
panic(err)
}
fmt.Println(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
30
31
32
33
34
35
36
37
38
39
40
41
42 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.audio.transcriptions.TranscriptionCreateParams;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import java.nio.file.Path;
String 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.
""";
var result =
client
.audio()
.transcriptions()
.create(
TranscriptionCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_AUDIO_PATH")))
.model("gpt-4o-transcribe")
.build());
var completion =
client
.chat()
.completions()
.create(
ChatCompletionCreateParams.builder()
.model("gpt-4.1")
.temperature(0.0)
.store(true)
.addSystemMessage(systemPrompt)
.addUserMessage(result.asTranscription().text())
.build());
completion.choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println); 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
30
31
32 using OpenAI.Audio;
using OpenAI.Chat;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-4.1";
ChatClient client = new(model, key);
string transcriptionModel = "gpt-4o-transcribe";
AudioClient audio = new(transcriptionModel, key);
await using FileStream source = File.OpenRead("speech.wav");
AudioTranscription transcription = await audio.TranscribeAudioAsync(source, "speech.wav");
string systemPrompt =
"""
You are a helpful assistant for the company ZyntriQix. Correct any
spelling discrepancies in the transcribed text. Make sure the names
of these 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.
""";
ChatCompletionOptions correctionOptions = new() { Temperature = 0 };
ChatCompletion completion = await client.CompleteChatAsync(
[
new SystemChatMessage(systemPrompt),
new UserChatMessage(transcription.Text),
],
correctionOptions
);
Console.WriteLine(completion.Content[0].Text); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 require "openai"
require "pathname"
client = OpenAI::Client.new
audio = Pathname("speech.wav")
transcript = client.audio.transcriptions.create(
file: audio,
model: "gpt-4o-mini-transcribe"
)
response = client.responses.create(
model: "gpt-4.1",
input: "Add punctuation and paragraph breaks without changing the words:\n#{transcript.text}"
)
puts(response.output_text) 文本模型可以纠正拼写错误,并处理超出 Whisper 224 个 Token 提示窗口容量的较长术语列表。请对照原始音频检查修正结果,以免改变说话人的原意。