DashScope(阿里云百炼)集成用于在 OpenMontage 中调用阿里云百炼的图像生成、文本转语音(TTS)和语音识别(ASR)能力。使用前需在 .env 中设置 DASHSCOPE_API_KEY,密钥可在 https://dashscope.aliyun.com/ 获取。
【主要能力与接口】 注意:DashScope 的 /compatible-mode/v1/ 仅支持 /chat/completions 和 /embeddings;图像生成、TTS、ASR 均需使用 DashScope 原生端点,而非 OpenAI 兼容路径。三者均使用 Authorization: Bearer $DASHSCOPE_API_KEY 认证。
图像生成:POST https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation。默认模型 qwen-image-2.0-pro,另支持 qwen-image-max、wan2.7-image、z-image-turbo。请求体包含 model、input.messages(user 角色,content 内含 text 提示词)与 parameters(size、n、prompt_extend、watermark)。尺寸使用星号分隔,如 "1024*1024",而非 "1024x1024"。返回 output.choices[0].message.content[0].image 为 URL,约 24 小时有效,需另行下载。
文本转语音:同一端点,请求体不同。默认模型 qwen3-tts-flash,另支持 qwen3-tts-instruct-flash、qwen-tts-2025-05-22。请求体包含 model 与 input(text、voice: "Cherry"、language_type: "Auto")。返回 output.audio.url 为 WAV 文件,约 24 小时有效,需另行下载。
语音识别(词级时间戳):POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/transcription,需加请求头 X-DashScope-Async: enable。模型必须使用 qwen3-asr-flash-filetrans,同步版 qwen3-asr-flash 不支持词级时间戳。请求体含 model 与 input.file_url(公网 URL),parameters 含 enable_words: true、language_hints(如 ["zh","en"])。返回 task_id 后轮询 GET /api/v1/tasks/{task_id} 直至 SUCCEEDED,再下载 output.result.transcription_url 对应的 JSON,其中包含 transcripts[].sentences[].words[]。原始时间戳 begin_time/end_time 为毫秒,工具会归一化为秒。
【在 OpenMontage 中的使用方式】 图像(通过选择器):from tools.graphics.image_selector import ImageSelector;调用 ImageSelector().execute({"preferred_provider": "dashscope", "prompt": "...", "output_path": "..."})。 TTS(通过选择器):from tools.audio.tts_selector import TTSSelector;调用 TTSSelector().execute({"preferred_provider": "dashscope", "text": "...", "voice": "Cherry", "output_path": "..."})。 ASR(直接调用,用于字幕词级时间戳):from tools.analysis.dashscope_asr import DashscopeAsr;调用 DashscopeAsr().execute({"audio_url": "https://...", "output_path": "..."})。返回的 result.data["words"] 为扁平列表,每项含 text、begin_time_seconds、end_time_seconds。
【推荐流程】 图像:先生成样例,检查 prompt_extend 默认开启会重写提示词以获得更好效果;若需严格遵循原文提示词可关闭。TTS:正式生成前先输出 10–15 秒样例,确认音色与节奏后再全量生成。ASR:音频必须处于公网可访问 URL,需先上传至任意公网存储(如 S3),本地路径会被拒绝并报错。字幕:基于 result.data["words"] 构建,按语言语义而非固定字数分组为字幕短语。
【主要参数】 图像(dashscope_image):prompt(必填);model 默认 qwen-image-2.0-pro;size 默认 "1024*1024"(星号分隔);n 为 1–6;negative_prompt 最多 500 字符;prompt_extend 默认 true;watermark 默认 false;seed 用于复现。 TTS(dashscope_tts):text(必填,qwen3-tts-flash 最多 600 字符);model 默认 qwen3-tts-flash;voice 默认 "Cherry",另有 "Ethan"、"Chelsie" 等;language_type 默认 "Auto",可选 "Chinese"、"English"、"Japanese"、"Korean";instructions 为自然语言表达指令,仅 qwen3-tts-instruct-flash 支持。 ASR(dashscope_asr):audio_url(必填,须公网可访问);model 为 qwen3-asr-flash-filetrans;language_hints 默认 ["zh","en"];enable_words 默认 true;poll_interval_seconds 默认 5.0;timeout_seconds 默认 300。
【常见问题】 图像尺寸报错:使用星号格式,如 "2048*2048"。TTS 无音频 URL:检查 output.audio.url,为空可能是模型名或音色错误。ASR 提示文件不可访问:audio_url 必须公网可达,DashScope 服务器会主动拉取文件,本地路径和需鉴权的 URL 均不可用。ASR 轮询超时:可调大 timeout_seconds,长音频转写耗时更长。ASR 无词级时间戳:确认 enable_words: true 且模型为 qwen3-asr-flash-filetrans。认证错误 401:确认已设置 DASHSCOPE_API_KEY 并使用 Bearer 头。
【安全】 不得将 API 密钥打印或写入日志、元数据、补丁或项目产物;.env.example 仅保留空变量名。工具的 _safe_error() 方法会在错误信息中隐去密钥。