提示词生成歌曲
通过提示词 prompt描述歌曲风格与主题,调用 POST /api/v1/song/generate 提交生歌任务。该接口为异步模式:提交后返回 task_id,需轮询 GET /api/v1/task/query 获取生成进度与结果。
请求参数说明
| 参数 | 必填 | 说明 |
|---|
| model | 是 | 模型版本,当前支持 v3.5 |
| task_type | 是 | 固定为 normal(提示词模式) |
| prompt | 是 | 提示词 prompt:歌曲风格、情绪、主题等文字描述 |
| lyric | 否 | 自定义歌词;不传则由模型根据提示词 prompt 生成 |
| n | 否 | 生成歌曲数量,默认 2,最大 2 |
1. 提交生歌任务
curl -X POST "https://open.yinchaoyongxian.com/api/v1/song/generate" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "v3.5",
"task_type": "normal",
"prompt": "一首轻快的流行歌曲,主题是夏日海边的回忆",
"n": 2
}'
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"task_type": "normal",
"choices": [],
"create_at": 1710000000
}
2. 轮询任务状态
歌曲生成通常需要数十秒。建议每隔 2~5 秒调用一次 query 接口,直到 choices 中歌曲的 status 为 done 或 fail。
| status | 含义 |
|---|
| pending | 排队中 |
| running | 生成中 |
| stream | 可流式播放(pipe_url 可用) |
| done | 生成完成(audio_url 可用) |
| fail | 生成失败,查看 error 字段 |
import time
import requests
API_KEY = "YOUR_API_KEY"
BASE = "https://open.yinchaoyongxian.com"
headers = {"Authorization": f"Bearer {API_KEY}"}
task_id = "3fa85f64-5717-4562-b3fc-2c963f66afa6"
while True:
resp = requests.get(
f"{BASE}/api/v1/task/query",
params={"task_id": task_id},
headers=headers,
)
task = resp.json()
for song in task.get("choices", []):
print(song["id"], song["status"])
if song["status"] == "done":
print("audio_url:", song["audio_url"])
elif song["status"] == "fail":
print("error:", song["error"])
if all(s["status"] in ("done", "fail") for s in task.get("choices", [])):
break
time.sleep(3)
相关接口