非文本端点

语音与向量 API 调用教程

TTS、语音转文字和向量模型都不能调用 /v1/chat/completions,必须使用对应端点和请求格式。

更新于 2026-08-05Bearer 鉴权OpenAI 兼容端点

请使用对应的完整教程

本页只保留快速概览。准确价格、限制、模型参数以及 cURL、Python、Node.js 示例,请分别查看文本转语音教程Whisper 语音转文字教程向量 API 教程

文本转语音(TTS)

支持 gpt-4o-mini-ttstts-1-hd。接口返回二进制音频,必须保存为文件,不能按 JSON 解析。

终端 · 输出 MP3
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.mp3
Python · requests
import 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)

必填参数是 modelinputvoice。常用输出格式包括 mp3opusaacflacwavpcminstructions 只适用于 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"
Python · 文件上传
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"])

可选参数包括 languagepromptresponse_formattemperature。Whisper 还支持 textsrtverbose_jsonvtt

文本向量(Embeddings)

支持 text-embedding-3-largetext-embedding-3-smalltext-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"
  }'
Node.js · fetch
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