请使用对应的完整教程
本页只保留快速概览。准确价格、限制、模型参数以及 cURL、Python、Node.js 示例,请分别查看文本转语音教程、Whisper 语音转文字教程和向量 API 教程。
文本转语音(TTS)
支持 gpt-4o-mini-tts 和 tts-1-hd。接口返回二进制音频,必须保存为文件,不能按 JSON 解析。
curl https://api-models.com/v1/audio/speech \
-H "Authorization: Bearer $API_MODELS_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini-tts",
"input": "你好,欢迎使用 API Models。",
"voice": "alloy",
"response_format": "mp3"
}' \
--output speech.mp3import os
import requests
response = requests.post(
"https://api-models.com/v1/audio/speech",
headers={"Authorization": f"Bearer {os.environ['API_MODELS_KEY']}"},
json={
"model": "gpt-4o-mini-tts",
"input": "你好,欢迎使用 API Models。",
"voice": "alloy",
"response_format": "mp3",
},
timeout=120,
)
response.raise_for_status()
open("speech.mp3", "wb").write(response.content)必填参数是 model、input 和 voice。常用输出格式包括 mp3、opus、aac、flac、wav、pcm。instructions 只适用于 gpt-4o-mini-tts,不适用于 tts-1-hd。
Whisper 语音转文字
whisper-1 使用 multipart/form-data 上传音频文件并返回文本,不能发送聊天 JSON 请求体。
curl https://api-models.com/v1/audio/transcriptions \
-H "Authorization: Bearer $API_MODELS_KEY" \
-F "file=@./audio.mp3" \
-F "model=whisper-1" \
-F "response_format=json"import os
import requests
with open("audio.mp3", "rb") as audio:
response = requests.post(
"https://api-models.com/v1/audio/transcriptions",
headers={"Authorization": f"Bearer {os.environ['API_MODELS_KEY']}"},
files={"file": ("audio.mp3", audio, "audio/mpeg")},
data={"model": "whisper-1", "response_format": "json"},
timeout=180,
)
response.raise_for_status()
print(response.json()["text"])可选参数包括 language、prompt、response_format 和 temperature。Whisper 还支持 text、srt、verbose_json、vtt。
文本向量(Embeddings)
支持 text-embedding-3-large、text-embedding-3-small 和 text-embedding-ada-002。向量位于响应的 data[].embedding。
curl https://api-models.com/v1/embeddings \
-H "Authorization: Bearer $API_MODELS_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-small",
"input": "把这句话转换成向量。",
"encoding_format": "float"
}'const response = await fetch("https://api-models.com/v1/embeddings", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.API_MODELS_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "text-embedding-3-small",
input: "把这句话转换成向量。",
encoding_format: "float",
}),
});
if (!response.ok) throw new Error(await response.text());
const data = await response.json();
console.log(data.data[0].embedding.slice(0, 8));dimensions 只适用于 text-embedding-3 系列,不适用于 text-embedding-ada-002。
常见错误
| 现象 | 原因 | 处理 |
|---|---|---|
| 出现聊天接口错误 | 端点用错 | 改用上方对应能力端点 |
| 415 / 请求体无效 | Content-Type 错误 | TTS 和向量用 JSON;转写用 multipart |
| 音频为空或损坏 | 把二进制响应当 JSON | 直接把响应体写入文件 |
| 模型不存在 | 模型 ID 不一致 | 从模型列表复制完整 ID |