产品上手指引
通过API接口实现输入文本转换输出语音文件。
案例程序执行前需在设备中安装对应的model模型包。模型包安装教程可参考模型列表章节。
在运行本示例程序之前,请确保已在 LLM 设备上完成以下准备工作:
llm-model-melotts-en-us
模型包。apt install llm-model-melotts-en-us
ffmpeg
工具。apt install ffmpeg
systemctl restart llm-openai-api
在 PC 端通过 OpenAI API 传入文本信息实现文本转换语音功能,案例程序执行前将下方base_url
的IP部分修改为设备实际IP地址。
from pathlib import Path
from openai import OpenAI
client = OpenAI(
api_key="sk-",
base_url="http://192.168.20.186:8000/v1"
)
speech_file_path = Path(__file__).parent / "speech.mp3"
with client.audio.speech.with_streaming_response.create(
model="melotts-en-us",
voice="alloy",
input="The quick brown fox jumped over the lazy dog."
) as response:
response.stream_to_file(speech_file_path)
参数名称 | 类型 | 必选 | 示例值 | 描述 |
---|---|---|---|---|
input | string | 是 | "你好,欢迎使用系统" | 要生成音频的文本内容,最大长度为 1024 个字符 |
model | string | 是 | melotts-zh-cn | 可用的 TTS 模型,包括 melotts-zh-cn 和 melotts-en-us |
voice | - | 否 | - | 当前不支持语音风格选择 |
response_format | string | 否 | mp3 | 音频输出格式,支持 mp3 , opus , aac , flac , wav , pcm 等 |
speed | number | 否 | 1.0 | 生成语音的速度,范围为 0.25 ~ 2.0,默认值为 1.0 |
speech_file_path
路径下。