feat: 集成智谱 CogView-3-Flash 文生图 + CogVideoX-Flash 文生视频
This commit is contained in:
1 parent
c58b29a06f
commit
403676619e
13 files changed
+278
-49
No files matched your search
@@ -12,6 +12,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any, AsyncIterator
|
||||
from urllib.parse import quote
|
||||
|
||||
|
||||
from app.adapters.base import BaseAdapter, json_or_raise
|
||||
@@ -184,6 +185,26 @@ class OpenAICompatAdapter(BaseAdapter):
|
||||
size: str = "720P",
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
# 智谱视频契约:POST /videos/generations(异步)——参数为 quality/with_audio/fps/像素 size,
|
||||
# 不需要 agnes 的 seconds/mode/aspect_ratio/n。
|
||||
if self.media.video_style == "zhipu":
|
||||
endpoint = self.media.video_create_endpoint or "/videos/generations"
|
||||
x = extra or {}
|
||||
payload: dict[str, Any] = {
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"size": size or self.media.video_default_size or "1920x1080",
|
||||
"quality": x.get("quality") or "speed",
|
||||
"fps": int(x.get("fps") or 30),
|
||||
"with_audio": bool(x.get("with_audio") or False),
|
||||
}
|
||||
async with self._lease() as client:
|
||||
resp = await client.post(endpoint, json=payload, headers=self._headers)
|
||||
resp.raise_for_status()
|
||||
data = json_or_raise(resp)
|
||||
return {"video_id": data.get("id") or data.get("request_id") or data.get("task_id"),
|
||||
"raw": data}
|
||||
# ── Agnes 风格(默认):POST {base}/videos,seconds/mode/size/aspect_ratio/n ──
|
||||
endpoint = self.media.video_create_endpoint or "/videos"
|
||||
payload: dict[str, Any] = {
|
||||
"model": model,
|
||||
@@ -211,6 +232,17 @@ class OpenAICompatAdapter(BaseAdapter):
|
||||
|
||||
# ── 视频轮询 ────────────────────────────────────
|
||||
async def poll_video(self, video_id: str, model: str = "", extra: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
# 智谱视频异步查询:GET {poll_base}/async-result/{video_id}(路径参数,无 query)
|
||||
if self.media.video_style == "zhipu":
|
||||
pb = (self.media.video_poll_base or self.base_url).rstrip("/")
|
||||
pp = (self.media.video_poll_path or "/async-result").rstrip("/")
|
||||
url = f"{pb}{pp}/{quote(str(video_id), safe='')}"
|
||||
async with self._lease() as client:
|
||||
resp = await client.get(url, headers=self._headers)
|
||||
resp.raise_for_status()
|
||||
data = json_or_raise(resp)
|
||||
return {"raw": data}
|
||||
|
||||
mname = self.media.video_model_name or model or ""
|
||||
poll_base = self.media.video_poll_base or self.base_url
|
||||
poll_path = self.media.video_poll_path or "/agnesapi"
|
||||
|
||||
+5
-1
@@ -54,7 +54,9 @@ class MediaConfig:
|
||||
video_create_endpoint: str | None = None # 相对 base_url,默认 /videos
|
||||
video_poll_base: str | None = None # 绝对 base(视频查询,默认同 base_url 去掉 /v1)
|
||||
video_poll_path: str | None = None # 默认 /agnesapi(Agnes)
|
||||
video_model_name: str | None = None # 轮询用的 model_name 参数
|
||||
video_model_name: str | None = None # 轮询用的 model_name 参数(Agnes)
|
||||
video_style: str | None = None # 视频契约风格:"zhipu"=智谱异步(videos/generations + /async-result/{id});None=Agnes 风格
|
||||
video_default_size: str | None = None # 视频默认尺寸(智谱为像素串如 1920x1080;Agnes 用 size 档位)
|
||||
|
||||
@property
|
||||
def supports_image(self) -> bool:
|
||||
@@ -304,6 +306,8 @@ def _parse_media(pval: dict[str, Any]) -> MediaConfig:
|
||||
video_poll_base=m.get("video_poll_base"),
|
||||
video_poll_path=m.get("video_poll_path"),
|
||||
video_model_name=m.get("video_model_name"),
|
||||
video_style=m.get("video_style"),
|
||||
video_default_size=m.get("video_default_size"),
|
||||
)
|
||||
|
||||
|
||||
|
||||
+64
-12
@@ -1729,8 +1729,8 @@ async def _video_wait_and_callback(cfg, pkey, pconfig, pmodel_id, video_id, wait
|
||||
await asyncio.sleep(2)
|
||||
continue
|
||||
raw = final.get("raw") or {}
|
||||
status = raw.get("status") or raw.get("state")
|
||||
if status in ("completed", "failed", "succeeded", "success"):
|
||||
status = _video_status(raw)
|
||||
if _video_terminal(status):
|
||||
break
|
||||
await asyncio.sleep(2)
|
||||
# Webhook 回调
|
||||
@@ -1745,8 +1745,8 @@ async def _video_wait_and_callback(cfg, pkey, pconfig, pmodel_id, video_id, wait
|
||||
try:
|
||||
# Bug 2 修复: 按真实状态上报,failed 不再误标成 completed
|
||||
raw = (final.get("raw") if final else None) or {}
|
||||
status = str(raw.get("status") or raw.get("state") or "")
|
||||
if status not in ("completed", "failed", "succeeded", "success"):
|
||||
status = _video_status(raw)
|
||||
if not _video_terminal(status):
|
||||
status = "timeout"
|
||||
payload = {"video_id": video_id, "provider": pkey, "status": status,
|
||||
"result": raw, "x_request_id": rid}
|
||||
@@ -1764,12 +1764,63 @@ _VIDEO_SIZES = ("720P",)
|
||||
_VIDEO_ASPECTS = ("21:9", "16:9", "4:3", "1:1", "3:4", "9:16")
|
||||
|
||||
|
||||
def _validate_video_request(req) -> None:
|
||||
def _video_status(raw: dict | None) -> str:
|
||||
"""视频原始返回的状态取法(统一小写)。
|
||||
|
||||
智谱异步结果是 task_status(PROCESSING/SUCCESS/FAIL),Agnes 用 status / state。
|
||||
"""
|
||||
if not raw:
|
||||
return ""
|
||||
return str(raw.get("task_status") or raw.get("status") or raw.get("state") or "").lower()
|
||||
|
||||
|
||||
def _video_terminal(status: str) -> bool:
|
||||
"""是否已到终态(成功或失败),用于结束轮询。"""
|
||||
s = status.lower()
|
||||
return s in ("success", "succeeded", "completed", "done", "finished",
|
||||
"failed", "fail", "error", "cancelled", "canceled")
|
||||
|
||||
|
||||
# 智谱 CogVideoX-Flash 支持的分辨率(像素串),与 Agnes 的 720P 档位语义不同。
|
||||
_ZHIPU_VIDEO_SIZES = {
|
||||
"720x480", "1024x1024", "1280x960", "960x1280",
|
||||
"1920x1080", "1080x1920", "2048x1080",
|
||||
}
|
||||
|
||||
|
||||
def _validate_video_request(req, media=None) -> None:
|
||||
"""第十一轮 #3:按 Agnes Video 2.5 契约做**入站校验**,把明显不合法的 mode/媒体/时长
|
||||
组合在网关挡下并回**干净 400**(不再白发上游浪费限流额度,也不进熔断)。
|
||||
第十三轮补齐 size / aspect_ratio / n / videos 四项(文档「Flash 专属错误」与
|
||||
「生成模式规则」里明确列出的限制),并保留原有 mode/媒体/时长校验。
|
||||
目前只有 Agnes 配了视频,字段语义按它的文档来;接入其它视频供应商时需按各家契约放宽。"""
|
||||
|
||||
# ── 智谱 CogVideoX(CogVideoX-Flash):仅 text 文生视频;quality/fps/with_audio 走 req.extra ──
|
||||
if media is not None and media.video_style == "zhipu":
|
||||
if (req.mode or "text") != "text":
|
||||
raise HTTPException(400, "智谱视频(CogVideoX)仅支持 text 文生视频模式")
|
||||
if int(req.n or 1) != 1:
|
||||
raise HTTPException(400, "视频接口一次只提交 1 个任务(n 必须为 1,收到 %s)" % req.n)
|
||||
if req.videos or req.images or req.audios or req.first_frame or req.last_frame:
|
||||
raise HTTPException(400, "智谱视频不支持首尾帧 / 参考图 / 参考音频(文生视频只填 prompt)")
|
||||
if req.size and req.size not in _ZHIPU_VIDEO_SIZES:
|
||||
raise HTTPException(400, "智谱视频 size 仅支持 %s,收到 %r" %
|
||||
("/".join(sorted(_ZHIPU_VIDEO_SIZES)), req.size))
|
||||
_x = req.extra or {}
|
||||
_q = _x.get("quality")
|
||||
if _q is not None and _q not in ("speed", "quality"):
|
||||
raise HTTPException(400, "智谱视频 quality 仅支持 speed / quality,收到 %r" % _q)
|
||||
_f = _x.get("fps")
|
||||
if _f is not None:
|
||||
try:
|
||||
_fi = int(_f)
|
||||
except (TypeError, ValueError):
|
||||
_fi = -1
|
||||
if _fi not in (30, 60):
|
||||
raise HTTPException(400, "智谱视频 fps 仅支持 30 / 60,收到 %r" % _f)
|
||||
return
|
||||
|
||||
# ── Agnes 契约(默认)───────────────────────────────
|
||||
mode = req.mode or "text"
|
||||
if mode not in _VIDEO_MODES:
|
||||
raise HTTPException(400, f"视频 mode 仅支持 {'/'.join(_VIDEO_MODES)},收到 {mode!r}")
|
||||
@@ -1811,12 +1862,13 @@ async def videos_create(request: Request, req: VideoCreateRequestExtended, _auth
|
||||
cfg = _cfg()
|
||||
rid = getattr(request.state, "rid", "-")
|
||||
caller = getattr(request.state, "caller", "anonymous")
|
||||
_validate_video_request(req) # 第十一轮 #3:入站按上游契约校验 mode/媒体/时长
|
||||
# Bug 4: 支持路由别名(如 wjd)→ 解析到第一个 type==video 的模型
|
||||
# Bug 4: 支持路由别名(如 wjd)→ 解析到第一个 type==video 的模型。
|
||||
# 解析放在入站校验之前,好让 _validate_video_request 按供应商媒体契约(Agnes / 智谱)分流。
|
||||
resolved = _resolve_media_model(cfg, req.model, "video")
|
||||
if resolved is None:
|
||||
raise HTTPException(404, f"未知视频模型/路由: {req.model}(没有可用的 video 类型模型)")
|
||||
pkey, pconfig, minfo, token, real_model = resolved
|
||||
_validate_video_request(req, pconfig.media) # 入站按供应商媒体契约校验
|
||||
# 顺序修复(本轮):先能力检查(501),再校验回调地址(400),最后限流 —— 避免无效请求白扣 token。
|
||||
if not pconfig.media.supports_video:
|
||||
raise HTTPException(501, f"供应商 {pconfig.name} 不支持视频创建")
|
||||
@@ -1872,7 +1924,7 @@ async def videos_create(request: Request, req: VideoCreateRequestExtended, _auth
|
||||
# 本轮修复: status 同时看 status/state(此前只看 status,而上游若用 state
|
||||
# 会被显示成 "pending" —— 即使其实已完成,与等待循环的判定不一致)
|
||||
raw_final = (final.get("raw") or {}) if final else {}
|
||||
resp["status"] = (raw_final.get("status") or raw_final.get("state") or "pending") if final else "submitted"
|
||||
resp["status"] = (_video_status(raw_final) or "pending") if final else "submitted"
|
||||
elif req.callback_url:
|
||||
# 第六轮修复: 此前只传 callback_url(不传 wait)时,_video_wait_and_callback 第一行
|
||||
# 就 `if not wait: return None` → **webhook 被静默丢弃、永不回调**,而 README 却把
|
||||
@@ -2026,8 +2078,8 @@ async def videos_poll(request: Request, video_id: str, model: str = "", wait: bo
|
||||
log.warning("[rid=%s] 轮询视频 %s 瞬时失败: %s", rid, _logsafe(video_id), e)
|
||||
await asyncio.sleep(2)
|
||||
continue
|
||||
status = (result.get("raw") or {}).get("status") or (result.get("raw") or {}).get("state")
|
||||
if status in ("completed", "failed", "succeeded", "success"):
|
||||
status = _video_status(result.get("raw"))
|
||||
if _video_terminal(status):
|
||||
break
|
||||
await asyncio.sleep(2)
|
||||
if result is None:
|
||||
@@ -2043,12 +2095,12 @@ async def videos_poll(request: Request, video_id: str, model: str = "", wait: bo
|
||||
# 第十轮: 轮询不单独占一行日志,改为按 video_id 快照累积,由 /logs 合成到创建那条里
|
||||
video_poll_push(video_id, {
|
||||
"ts": round(time.time(), 3), "ok": True,
|
||||
"status": (raw.get("status") or raw.get("state") or ""),
|
||||
"status": _video_status(raw),
|
||||
"progress": raw.get("progress"),
|
||||
"raw": json.dumps(raw, ensure_ascii=False)[:1200],
|
||||
})
|
||||
return {"video_id": video_id, "provider": pkey, "served_model": pmodel_id, "raw": raw,
|
||||
"status": raw.get("status") or raw.get("state"), "x_request_id": rid}
|
||||
"status": _video_status(raw), "x_request_id": rid}
|
||||
except AuthConfigError as e:
|
||||
cb.release_probe()
|
||||
raise HTTPException(503, f"供应商 {pconfig.name} 未配置可用凭证: {e}")
|
||||
|
||||
@@ -61,6 +61,8 @@ MEDIA_KEYS = (
|
||||
"video_poll_base",
|
||||
"video_poll_path",
|
||||
"video_model_name",
|
||||
"video_style",
|
||||
"video_default_size",
|
||||
)
|
||||
|
||||
MODEL_TYPES = ("text", "image", "video")
|
||||
|
||||
Reference in new issue
Block a user