Appearance
Audio 音频
音频相关接口,包括语音合成、语音转文字、语音翻译。
参考文档: https://platform.openai.com/docs/api-reference/audio
语音合成 (Text-to-Speech)
POST /v1/audio/speech
将文本转换为音频。
请求体 (application/json)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型 ID(如 tts-1) |
| input | string | 是 | 要转换的文本(最大 4096 字符) |
| voice | string | 是 | 语音选择:alloy、echo、fable、onyx、nova、shimmer |
| response_format | string | 否 | mp3、opus、aac、flac、wav、pcm,默认 mp3 |
| speed | number | 否 | 语速 0.25-4,默认 1 |
请求示例
bash
curl -X POST "https://your-api-base/v1/audio/speech" \
-H "Authorization: Bearer sk-xxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1",
"input": "The quick brown fox jumped over the lazy dog.",
"voice": "alloy"
}' \
--output speech.mp3python
from openai import OpenAI
from pathlib import Path
client = OpenAI(
api_key="sk-xxxxxx",
base_url="https://your-api-base/v1"
)
speech_file = Path("speech.mp3")
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input="The quick brown fox jumped over the lazy dog."
)
response.stream_to_file(speech_file)响应
直接返回音频文件二进制内容。
语音转文字 (Transcription)
POST /v1/audio/transcriptions
将音频转换为文本。
请求体 (multipart/form-data)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| file | binary | 是 | 音频文件 |
| model | string | 是 | 模型 ID(如 whisper-1) |
| language | string | 否 | ISO-639-1 语言代码 |
| prompt | string | 否 | 可选提示词 |
| response_format | string | 否 | json、text、srt、verbose_json、vtt,默认 json |
| temperature | number | 否 | 采样温度 |
| timestamp_granularities | array | 否 | word、segment |
请求示例
bash
curl -X POST "https://your-api-base/v1/audio/transcriptions" \
-H "Authorization: Bearer sk-xxxxxx" \
-F file="@audio.mp3" \
-F model="whisper-1" \
-F language="zh"200 成功响应
json
{
"text": "这是一段测试音频的转写结果。"
}语音翻译 (Translation)
POST /v1/audio/translations
将音频翻译为英文文本。
请求体 (multipart/form-data)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| file | binary | 是 | 音频文件 |
| model | string | 是 | 模型 ID |
| prompt | string | 否 | 可选提示词 |
| response_format | string | 否 | 响应格式 |
| temperature | number | 否 | 采样温度 |
请求示例
bash
curl -X POST "https://your-api-base/v1/audio/translations" \
-H "Authorization: Bearer sk-xxxxxx" \
-F file="@audio.mp3" \
-F model="whisper-1"