文本转语音(Text-to-Speech)
使用 OpenAI 原生音频格式,将文本转换为音频。响应通常是音频二进制数据,客户端应保存响应体或按流读取,不能按 JSON 解析。
请求
POST /v1/audio/speech
Content-Type: application/json
Authorization: Bearer sk-your-api-key
调用示例
- cURL
- JavaScript
- Python
curl -X POST "https://www.walmind.cn/v1/audio/speech" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "tts-1",
"input": "欢迎使用华明模型 API。",
"voice": "alloy",
"response_format": "mp3"
}' \
--output speech.mp3
const response = await fetch('https://www.walmind.cn/v1/audio/speech', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.API_KEY}`,
},
body: JSON.stringify({
model: 'tts-1',
input: '欢迎使用华明模型 API。',
voice: 'alloy',
response_format: 'mp3',
}),
});
if (!response.ok) throw new Error(await response.text());
const audio = Buffer.from(await response.arrayBuffer());
await Bun.write('speech.mp3', audio);
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["API_KEY"],
base_url="https://www.walmind.cn/v1",
)
with client.audio.speech.with_streaming_response.create(
model="tts-1",
voice="alloy",
input="欢迎使用华明模型 API。",
response_format="mp3",
) as response:
response.stream_to_file("speech.mp3")
请求参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 语音模型 ID,必填 |
input | string | 要转换为语音的文本,必填 |
voice | string | 音色,必填;可用值以模型支持情况为准 |
response_format | string | 音频格式,例如 mp3、opus、aac 或 flac |
speed | number | 播放速度,是否支持取决于模型 |
响应的 Content-Type 会根据 response_format 返回对应音频类型,例如 audio/mpeg。如果模型或渠道不支持语音生成,会返回接口错误而不是音频内容。