语音合成

最后更新:2026-11-06

POSThttps://www.sagelib.com/audio/speech

接收用户输入的文本,返回AI生成的、自然流畅的语音音频文件(如MP3格式),适用于内容播报、辅助工具、智能交互等场景。

接口鉴权方式请参考:API接入与鉴权

请求参数

请求头

参数名称 描述
*Authorization string 鉴权令牌,格式为 Bearer ${AIGATEWAY_TOKEN}
*Content-Type string 可选值:application/json
application/json

Body 参数

参数名称 描述
model string 默认值:Default model configured in the AIGateway channel
模型名称
*input string 欲转换为语音的文本内容
*voice string 默认值:None
可选值:Platform-provided, e.g. alloy
音色,需根据模型支持的类型填写
也支持克隆音色 ID(见 声音克隆)
response_format string 默认值:mp3
可选值:mp3、opus、aac、flac、wav、pcm
返回的音频文件格式
speed number 默认值:1
可选值:0.25~4.0
语速,介于0.25~4.0,1为正常语速

请求示例

curl -v 'https://www.sagelib.com/audio/speech' \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "The quick brown fox jumped over the lazy dog.",
    "voice": "alloy",
    "response_format": "mp3",
    "speed": 1
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/audio/speech"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
    "input": "The quick brown fox jumped over the lazy dog.",
    "voice": "alloy",
    "response_format": "mp3",
    "speed": 1
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
with open("output.mp3", "wb") as f:
    f.write(response.content)
print("Audio saved to output.mp3")
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/audio/speech";

        String jsonRequestBody = """
            {
                "input": "The quick brown fox jumped over the lazy dog.",
                "voice": "alloy",
                "response_format": "mp3",
                "speed": 1
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
            try (InputStream in = response.body(); FileOutputStream out = new FileOutputStream("output.mp3")) {
                in.transferTo(out);
            }
            System.out.println("Audio saved to output.mp3");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/audio/speech"

    jsonRequestBody := `
            {
                "input": "The quick brown fox jumped over the lazy dog.",
                "voice": "alloy",
                "response_format": "mp3",
                "speed": 1
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    fmt.Println("Status Code:", resp.StatusCode)
    _ = body // audio bytes; save as needed
}

说明:voice 除内置音色(如 alloy)外,也支持克隆音色 ID —— 先按 声音克隆 上传参考音频取得文件 ID,再把它填到 voice。

响应参数

合成后的语音音频流,Content-Type取决于response_format参数(如audio/mpeg、audio/wav等)

响应示例

[binary audio data]

状态码

HTTP状态码 描述
200 请求成功
401 访问被拒绝,拒绝原因见响应主体,可能原因为Authorization错误等
403 访问被拒绝,原因见响应主体(如格式错误、参数缺失等)
500 服务器内部错误,原因见响应主体