利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
import json
import logging
import math
import os
import re
import shutil
import subprocess
import tempfile
import unicodedata
from time import perf_counter
from typing import List
from loguru import logger
from openai import AzureOpenAI, OpenAI
from openai.types.chat import ChatCompletion
from app.config import config
from app.models.llm_provider import DEFAULT_LLM_PROVIDER_ID, get_llm_provider
from app.utils import utils
from app.utils.request_logging import trace_sdk_call
_max_retries = 5
MIN_SCRIPT_PARAGRAPH_NUMBER = 1
MAX_SCRIPT_PARAGRAPH_NUMBER = 10
MAX_SCRIPT_PROMPT_LENGTH = 2000
MAX_SCRIPT_SYSTEM_PROMPT_LENGTH = 8000
# “测试模型链接”只发送一条极小请求。OpenAI SDK 默认 600 秒超时并自动重试
# 2 次,端点不可达(代理、防火墙或 DNS 故障)时,测试按钮会先长时间挂起,
# 最后只留下一句 "Request timed out."(见 issue #1642)。这里给连接测试一个
# 有界的短超时并禁用 SDK 重试,让失败快速、可诊断;正式的脚本生成链路保持
# SDK 默认行为不变,避免误伤慢速推理模型和本地 Ollama 冷启动。
LLM_CONNECTION_TEST_TIMEOUT_SECONDS = 30.0
LLM_CONNECTION_TEST_MAX_RETRIES = 0
# 这些片段来自 OpenAI SDK 的 APITimeoutError / APIConnectionError 以及
# httpx 的底层连接异常文案;命中任意一个即按“端点不可达”处理。
_CONNECTION_FAILURE_MARKERS = (
"timed out",
"timeout",
"connection error",
"connection refused",
"connection reset",
"connection aborted",
"failed to connect",
)
_THINK_BLOCK_RE = re.compile(r"<think\b[^>]*>.*?</think>", re.IGNORECASE | re.DOTALL)
_UNCLOSED_THINK_BLOCK_RE = re.compile(r"<think\b[^>]*>.*$", re.IGNORECASE | re.DOTALL)
_UNOPENED_THINK_BLOCK_RE = re.compile(r"^.*</think\s*>", re.IGNORECASE | re.DOTALL)
_URL_USERINFO_RE = re.compile(
r"((?:https?|wss?)://)([^/\s?#@]+@)", re.IGNORECASE
)
_SENSITIVE_QUERY_RE = re.compile(
r"([?&](?:api[_-]?key|access[_-]?token|token|key|secret|password)=)([^&#\s]+)",
re.IGNORECASE,
)
DEFAULT_SCRIPT_SYSTEM_PROMPT = """
# Role: Video Script Generator
## Goals:
Generate a script for a video, depending on the subject of the video.
## Constrains:
1. the script is to be returned as a string with the specified number of paragraphs.
2. do not under any circumstance reference this prompt in your response.
3. get straight to the point, don't start with unnecessary things like, "welcome to this video".
4. you must not include any type of markdown or formatting in the script, never use a title.
5. only return the raw content of the script.
6. do not include "voiceover", "narrator" or similar indicators of what should be spoken at the beginning of each paragraph or line.
7. you must not mention the prompt, or anything about the script itself. also, never talk about the amount of paragraphs or lines. just write the script.
8. respond in the same language as the video subject.
""".strip()
# Claude Code CLI 默认使用编码 agent 的系统提示词,其中大量约束与文案写作
# 无关,会让脚本和关键词生成偏离要求,因此调用时整体替换掉。
CLAUDE_CODE_SYSTEM_PROMPT = (
"You are a concise copywriter. Follow the user's instructions and output "
"format exactly, and output nothing else."
)
CLAUDE_CODE_DEFAULT_TIMEOUT = 300.0
# `--tools ""` 关闭全部内置工具,`--safe-mode` 关闭 CLAUDE.md、skills、hooks、
# plugins、MCP 等所有用户级定制,同时保持鉴权、模型选择和权限正常工作。
# 二者需要较新的 CLI;低版本会以 "unknown option" 退出,由调用处转成明确提示。
CLAUDE_CODE_MIN_CLI_VERSION = "2.1.260"
# 这些环境变量会让 CLI 改用 API Key 或第三方供应商(Bedrock、Vertex、Foundry、
# Mantle、Gateway 等),从而绕过订阅登录并产生额外计费。逐个列举容易漏项,
# 而且 CLI 后续还会新增供应商,因此按前缀整类剔除:
# ANTHROPIC_* API Key、Auth Token、Base URL、各家供应商端点和 Profile
# CLAUDE_CODE_USE_* 供应商开关
# CLAUDE_CODE_SKIP_*_AUTH 跳过供应商鉴权的开关
CLAUDE_CODE_CONFLICTING_ENV_PREFIXES = ("ANTHROPIC_", "CLAUDE_CODE_USE_")
CLAUDE_CODE_CONFLICTING_ENV_VARS = (
"AWS_BEARER_TOKEN_BEDROCK",
"CLAUDE_CODE_GATEWAY_TOKEN_FILE_DESCRIPTOR",
)
# 这两类变量不能剔除:
# CLAUDE_CODE_OAUTH_TOKEN 是容器内唯一的订阅鉴权方式(不匹配上面的前缀);
# *_CONFIG_DIR 只是指出凭证存放位置,剔除后反而会让已登录的订阅失效。
CLAUDE_CODE_PRESERVED_ENV_VARS = (
"CLAUDE_CODE_OAUTH_TOKEN",
"ANTHROPIC_CONFIG_DIR",
"CLAUDE_CONFIG_DIR",
)
def _is_conflicting_claude_code_env(name: str) -> bool:
"""判断某个环境变量是否会把 CLI 从订阅登录切换到别的鉴权方式。"""
if name in CLAUDE_CODE_PRESERVED_ENV_VARS:
return False
if name in CLAUDE_CODE_CONFLICTING_ENV_VARS:
return True
if name.startswith(CLAUDE_CODE_CONFLICTING_ENV_PREFIXES):
return True
return name.startswith("CLAUDE_CODE_SKIP_") and name.endswith("_AUTH")
def coerce_claude_code_timeout(value, config_key: str = "claude_code_timeout"):
"""
把配置里的超时值解析成正的有限秒数。
TOML 既可能写成 `claude_code_timeout = 300`(int/float),也可能写成
`"300"`(字符串),因此不能直接调用 `strip()`。nan / inf 会让
`subprocess.run(timeout=...)` 永久阻塞,这里一并拒绝。
"""
if value is None:
return CLAUDE_CODE_DEFAULT_TIMEOUT
if isinstance(value, bool):
# bool 是 int 的子类,但 True 秒显然不是用户想要的超时配置。
raise ValueError(f"{config_key} must be a number of seconds, got {value!r}")
if isinstance(value, str):
text = value.strip()
if not text:
return CLAUDE_CODE_DEFAULT_TIMEOUT
try:
seconds = float(text)
except ValueError:
raise ValueError(
f"{config_key} must be a number of seconds, got {value!r}"
) from None
elif isinstance(value, (int, float)):
seconds = float(value)
else:
raise ValueError(f"{config_key} must be a number of seconds, got {value!r}")
if not math.isfinite(seconds):
raise ValueError(f"{config_key} must be a finite number, got {value!r}")
if seconds <= 0:
raise ValueError(f"{config_key} must be greater than 0, got {value!r}")
return seconds
def _resolve_provider_field_value(raw_value, default_value):
"""
只有「未配置」时才回退到 Registry 默认值。
之前用 `raw or default_value`,会把 0 和 false 这类合法取值也当成未配置
替换掉:`claude_code_timeout = 0` 被静默改成 300,而 `"0"` 却报错。默认值
只在 None 或空白字符串时生效,配置校验才能对所有写法保持一致。
"""
if raw_value is None:
return default_value
if isinstance(raw_value, str) and not raw_value.strip():
return default_value
return raw_value
def build_claude_code_env(base_env=None):
"""
构造只依赖订阅登录的子进程环境。
返回 (环境变量字典, 被剔除的变量名列表)。剔除的是会切换鉴权方式或供应商
的变量,`CLAUDE_CODE_OAUTH_TOKEN` 必须保留:容器内没有 keychain,CLI 只能
靠它完成订阅鉴权。
"""
env = dict(os.environ if base_env is None else base_env)
removed = sorted(name for name in env if _is_conflicting_claude_code_env(name))
for name in removed:
env.pop(name, None)
return env, removed
def _normalize_text_response(content, llm_provider: str) -> str:
# 不同 LLM SDK 在异常或被拦截场景下,可能返回 None、空字符串,
# 甚至返回非字符串对象。这里统一做兜底校验,避免后续直接调用
# `.replace()` 时抛出 `NoneType` 之类的属性错误。
if content is None:
raise ValueError(f"[{llm_provider}] returned empty text content")
if not isinstance(content, str):
raise TypeError(
f"[{llm_provider}] returned non-text content: {type(content).__name__}"
)
# MiniMax M3、DeepSeek R1 这类 reasoning 模型可能会把内部推理包在
# `<think>...</think>` 中返回。视频脚本和关键词只需要最终可朗读文本,
# 如果不在服务层统一清理,WebUI、字幕和配音都会把思考过程当正文处理。
content = _THINK_BLOCK_RE.sub("", content)
# DeepSeek R1、QwQ 等模型的对话模板会把起始 `<think>` 放进提示词,
# vLLM/Ollama 等网关返回的正文只剩 `推理...</think>答案`。成对清理之后
# 仍残留的闭合标签之前都是推理内容,也不能进入脚本。
content = _UNOPENED_THINK_BLOCK_RE.sub("", content)
content = _UNCLOSED_THINK_BLOCK_RE.sub("", content).strip()
if not content:
raise ValueError(f"[{llm_provider}] returned empty text content")
# 前面的 ``strip()`` 已经清理首尾空白。这里必须保留正文中的单换行和
# 双换行:脚本生成依赖双换行区分段落,字幕处理也会按行读取用户文案。
return content
def _sanitize_error_message(error: object) -> str:
"""
清理返回给 WebUI/API 的错误信息,避免自定义 base_url 中的凭据泄露。
一些 OpenAI-compatible SDK 会把请求 URL 原样拼进异常信息。如果用户为了
代理网关配置了 `https://user:[email protected]/v1`,直接返回 `str(e)`
就会把密码暴露给页面、API 调用方或后续日志。这里仅处理错误文案,不改变
实际请求地址,避免影响正常调用链路。
"""
message = str(error)
message = _URL_USERINFO_RE.sub(r"\1***:***@", message)
message = _SENSITIVE_QUERY_RE.sub(r"\1***", message)
return message
def _extract_chat_completion_text(response, llm_provider: str) -> str:
# OpenAI 兼容接口在异常场景下,可能返回没有 choices、
# 或者 choices/message/content 为空的响应对象。
# 这里统一做结构校验,避免出现 `NoneType is not subscriptable`
# 这类底层属性访问错误。
choices = getattr(response, "choices", None)
if not choices:
raise ValueError(f"[{llm_provider}] returned empty choices")
first_choice = choices[0]
message = getattr(first_choice, "message", None)
if message is None:
raise ValueError(f"[{llm_provider}] returned empty message")
content = getattr(message, "content", None)
return _normalize_text_response(content, llm_provider)
def _get_response_field(value, key: str):
"""兼容 dict 和 SDK 响应对象的字段读取。"""
if isinstance(value, dict):
return value.get(key)
try:
return value[key]
except (KeyError, TypeError, AttributeError):
return getattr(value, key, None)
def _sdk_transport_options(request_timeout, max_retries) -> dict:
"""把可选的超时/重试约束转换为 OpenAI SDK 客户端构造参数。
两个参数传 None 时返回空字典,客户端继续使用 SDK 默认值(600 秒超时、
2 次自动重试),因此脚本生成等常规链路行为保持不变;只有连接测试这类
需要快速失败的调用方显式传入有界值时,请求预算才会被收紧。
"""
options = {}
if request_timeout is not None:
options["timeout"] = request_timeout
if max_retries is not None:
options["max_retries"] = max_retries
return options
def _explain_connection_test_failure(error_message: str) -> str:
"""为超时/连接类失败补充排查方向,而不是只显示一句 SDK 原始文案。
issue #1642 中用户的 API 本身可用,失败原因是本机到端点的链路问题
(代理、防火墙或 DNS),但 "Request timed out." 没有指向任何可能原因。
其它错误(鉴权、配额等)保持原文返回。
"""
normalized = (error_message or "").lower()
if not any(marker in normalized for marker in _CONNECTION_FAILURE_MARKERS):
return error_message
return (
f"{error_message} (the request did not complete within "
f"{LLM_CONNECTION_TEST_TIMEOUT_SECONDS:.0f}s). Please check that the "
"configured base URL is reachable from this machine, and that no proxy "
"or firewall is blocking the request."
)
def _extract_qwen_generation_text(response) -> str:
"""
从 DashScope Generation 响应中提取文本。
Qwen 使用 `messages` 调用时返回的是 chat 结构:
`output.choices[0].message.content`;旧 completion 形态才会返回
`output.text`。这里两个路径都兼容,避免 `output.text` 为 None 时
继续 `.replace()` 触发不可诊断的 AttributeError。
"""
output = _get_response_field(response, "output")
choices = _get_response_field(output, "choices") if output else None
if choices is not None:
if not choices:
logger.warning("Qwen returned an empty choices list")
raise ValueError("[qwen] returned empty choices")
first_choice = choices[0]
message = _get_response_field(first_choice, "message")
content = _get_response_field(message, "content") if message else None
if content is not None:
return _normalize_text_response(content, "qwen")
text = _get_response_field(output, "text") if output else None
return _normalize_text_response(text, "qwen")
def _generate_response(
prompt: str,
app_config=None,
request_timeout: float | None = None,
max_retries: int | None = None,
) -> str:
sdk_client = None
sdk_stream = None
try:
# WebUI 在视频生成期间允许用户准备下一条文案。调用方可以传入提交瞬间
# 的配置快照,确保模型请求重试期间不会因为后台任务结束并应用新配置,
# 而切换到另一个 Provider、Base URL 或模型。
runtime_app_config = app_config if app_config is not None else config.app
llm_provider = str(
runtime_app_config.get("llm_provider", DEFAULT_LLM_PROVIDER_ID)
).lower()
provider = get_llm_provider(llm_provider)
if provider is None:
raise ValueError(f"{llm_provider}: unsupported llm provider")
logger.info(f"llm provider: {llm_provider}")
api_key = runtime_app_config.get(provider.config_key("api_key"), "")
configured_model = runtime_app_config.get(provider.config_key("model_name"), "")
model_name = provider.resolve_model_name(configured_model)
if configured_model and model_name != configured_model:
logger.warning(
f"{llm_provider} model '{configured_model}' is deprecated, "
f"fallback to '{model_name}'"
)
configured_base_url = runtime_app_config.get(
provider.config_key("base_url"), ""
)
base_url = provider.resolve_base_url(configured_base_url)
if configured_base_url and configured_base_url.strip().rstrip("/") in {
url.rstrip("/") for url in provider.deprecated_base_urls
}:
logger.warning(
f"{llm_provider} base URL '{configured_base_url}' is deprecated, "
f"fallback to '{base_url}'"
)
adapter = provider.adapter
api_version = ""
# Ollama 的默认地址依赖当前是否运行在容器中,无法作为静态 Registry
# 值保存;Registry 仍负责模型和必填规则,运行环境差异在这里解析。
if llm_provider == "ollama":
api_key = "ollama"
if not base_url:
base_url = config.get_default_ollama_base_url()
if adapter == "azure":
api_version = runtime_app_config.get(
provider.config_key("api_version"), "2024-02-15-preview"
)
extra_values = {
field.config_suffix: _resolve_provider_field_value(
runtime_app_config.get(provider.config_key(field.config_suffix)),
field.default_value,
)
for field in provider.extra_fields
}
if provider.requires_api_key and not api_key:
raise ValueError(
f"{llm_provider}: api_key is not set, please set it in the config.toml file."
)
if provider.requires_model_name and not model_name:
raise ValueError(
f"{llm_provider}: model_name is not set, please set it in the config.toml file."
)
if provider.requires_base_url and not base_url:
raise ValueError(
f"{llm_provider}: base_url is not set, please set it in the config.toml file."
)
for field in provider.extra_fields:
if field.required and not extra_values[field.config_suffix]:
raise ValueError(
f"{llm_provider}: {field.config_suffix} is not set, "
"please set it in the config.toml file."
)
if adapter == "qwen":
import dashscope
from dashscope.api_entities.dashscope_response import GenerationResponse
response = trace_sdk_call(
llm_provider, base_url, dashscope.Generation.call,
model=model_name,
messages=[{"role": "user", "content": prompt}],
api_key=api_key,
)
if response:
if isinstance(response, GenerationResponse):
status_code = response.status_code
if status_code != 200:
raise Exception(
f'[{llm_provider}] returned an error response: "{response}"'
)
return _extract_qwen_generation_text(response)
else:
raise Exception(
f'[{llm_provider}] returned an invalid response: "{response}"'
)
else:
raise Exception(f"[{llm_provider}] returned an empty response")
if adapter == "gemini":
from google import genai
from google.genai import types
http_options = types.HttpOptions(base_url=base_url) if base_url else None
generation_config = types.GenerateContentConfig(
temperature=0.5,
top_p=1,
top_k=1,
max_output_tokens=2048,
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HARASSMENT",
threshold="BLOCK_ONLY_HIGH",
),
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold="BLOCK_ONLY_HIGH",
),
types.SafetySetting(
category="HARM_CATEGORY_SEXUALLY_EXPLICIT",
threshold="BLOCK_ONLY_HIGH",
),
types.SafetySetting(
category="HARM_CATEGORY_DANGEROUS_CONTENT",
threshold="BLOCK_ONLY_HIGH",
),
],
)
try:
# 新版 google-genai 通过统一 Client 暴露模型服务。上下文管理器
# 会在请求结束后关闭底层 HTTP 连接,避免频繁生成时积累连接资源。
with genai.Client(
api_key=api_key,
http_options=http_options,
) as client:
response = trace_sdk_call(
llm_provider, base_url, client.models.generate_content,
model=model_name,
contents=prompt,
config=generation_config,
)
generated_text = response.text
except (AttributeError, IndexError, ValueError) as e:
logger.warning(f"gemini returned invalid response content: {str(e)}")
raise ValueError(f"[{llm_provider}] returned invalid response content")
return _normalize_text_response(generated_text, llm_provider)
if adapter == "cloudflare_ai_gateway":
account_id = extra_values["account_id"]
gateway_id = extra_values["gateway_id"]
# Cloudflare 当前推荐的 AI Gateway REST API 兼容 OpenAI SDK。
# Account ID 用于构造统一端点,Gateway ID 通过请求头选择;这里
# 不再调用 Workers AI 的 /ai/run/{model} 专用接口。
client = sdk_client = OpenAI(
api_key=api_key,
base_url=(
f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1"
),
default_headers={"cf-aig-gateway-id": gateway_id},
**_sdk_transport_options(request_timeout, max_retries),
)
response = trace_sdk_call(
llm_provider, base_url, client.chat.completions.create,
model=model_name,
messages=[{"role": "user", "content": prompt}],
)
return _extract_chat_completion_text(response, llm_provider)
if adapter == "litellm":
import litellm
if not model_name:
raise ValueError(
f"{llm_provider}: model_name is not set, please set it in the config.toml file."
)
response = trace_sdk_call(
llm_provider, base_url, litellm.completion,
model=model_name,
messages=[{"role": "user", "content": prompt}],
drop_params=True,
)
if not response:
raise ValueError(f"[{llm_provider}] returned empty response")
if not getattr(response, "choices", None):
raise ValueError(f"[{llm_provider}] returned empty response")
return _extract_chat_completion_text(response, llm_provider)
if adapter == "azure":
# Azure OpenAI SDK 使用 `azure_endpoint` 和 `api_version` 生成专用请求地址,
# 不能继续复用下面普通 OpenAI-compatible 的 `base_url` 初始化逻辑。
# 这里在 Azure 分支内完成请求并立即返回,避免客户端被后续 fallback
# 覆盖,导致用户配置的 Azure 凭证通过校验但实际请求没有被使用。
logger.info(f"requesting azure chat completion, model: {model_name}")
client = sdk_client = AzureOpenAI(
api_key=api_key,
api_version=api_version,
azure_endpoint=base_url,
**_sdk_transport_options(request_timeout, max_retries),
)
response = trace_sdk_call(
llm_provider, base_url, client.chat.completions.create,
model=model_name, messages=[{"role": "user", "content": prompt}]
)
if response:
if isinstance(response, ChatCompletion):
return _extract_chat_completion_text(response, llm_provider)
else:
raise Exception(
f'[{llm_provider}] returned an invalid response: "{response}", please check your network '
f"connection and try again."
)
else:
raise Exception(
f"[{llm_provider}] returned an empty response, please check your network connection and try again."
)
if adapter == "claude_code":
# Claude 订阅(Pro / Max / Team)不签发 API Key,其凭证只能由
# Claude Code 官方客户端自己使用。这里不直接请求 Anthropic API,
# 而是以 headless 模式调用本机已登录的 claude CLI(`claude -p`),
# 由 CLI 完成鉴权,脚本生成只消费它返回的文本。
configured_cli = (extra_values.get("cli_path") or "").strip() or "claude"
cli_path = shutil.which(configured_cli)
if not cli_path and os.path.isfile(configured_cli):
cli_path = configured_cli
if not cli_path:
raise ValueError(
f"{llm_provider}: claude CLI not found ('{configured_cli}'), "
f"install it in the runtime or set "
f"{provider.config_key('cli_path')} in the config.toml file."
)
try:
timeout_seconds = coerce_claude_code_timeout(
extra_values.get("timeout"), provider.config_key("timeout")
)
except ValueError as timeout_error:
raise ValueError(f"{llm_provider}: {timeout_error}") from None
# prompt 通过 stdin 传入,不放在命令行里:Windows 上 npm 安装的
# claude 是 claude.cmd,cmd.exe 会在第一个换行处截断参数,多行
# prompt 和其后的隔离参数都会丢失。
command = [
cli_path,
"-p",
"--output-format",
"json",
"--system-prompt",
CLAUDE_CODE_SYSTEM_PROMPT,
# 关闭全部内置工具,保证只做文本生成。
"--tools",
"",
# 关闭 CLAUDE.md、skills、hooks、plugins、MCP 等用户级定制;
# 鉴权与模型选择不受影响(不能用 --bare,它会禁用 OAuth)。
"--safe-mode",
]
# 模型名留空时沿用 CLI 自己的默认模型,避免这里硬编码的模型 ID
# 随订阅可用模型变化而失效。
if model_name:
command += ["--model", model_name]
cli_env, removed_env = build_claude_code_env()
if removed_env:
# 只记录变量名,不记录取值,避免把密钥写进日志。
logger.warning(
f"{llm_provider}: ignoring conflicting environment variables "
f"so the subscription login is used: {', '.join(removed_env)}"
)
logger.info(f"invoking claude cli, model: {model_name or 'cli default'}")
# CLI 会读取工作目录下的 CLAUDE.md 和项目设置,这些内容会污染
# 文案结果,因此固定在一个临时空目录中执行。
with tempfile.TemporaryDirectory() as work_dir:
try:
completed = subprocess.run(
command,
input=prompt,
capture_output=True,
text=True,
# The CLI always emits UTF-8. Without an explicit encoding,
# text=True decodes with the system locale (e.g. cp1252 on
# non-English Windows), so every non-ASCII character reaches
# the script as mojibake.
encoding="utf-8",
errors="replace",
timeout=timeout_seconds,
cwd=work_dir,
env=cli_env,
)
except subprocess.TimeoutExpired:
raise Exception(
f"[{llm_provider}] claude cli timed out after "
f"{timeout_seconds:.0f}s"
)
# 未登录、用量耗尽这类失败同样会返回 JSON(`is_error` 为真,
# `result` 是可读原因),只是退出码非 0。因此先解析 stdout,
# 只有在拿不到 JSON 时才回退到退出码和 stderr。
stdout = (completed.stdout or "").strip()
try:
payload = json.loads(stdout) if stdout else None
except json.JSONDecodeError:
payload = None
if payload is None:
detail = (completed.stderr or stdout or "").strip()
if "unknown option" in detail.lower():
raise Exception(
f"[{llm_provider}] the installed claude CLI does not support "
f"the required isolation flags; upgrade to "
f"{CLAUDE_CODE_MIN_CLI_VERSION} or newer: {detail[:300]}"
)
if completed.returncode != 0:
raise Exception(
f"[{llm_provider}] claude cli exited with code "
f"{completed.returncode}: {detail[:500]}"
)
raise Exception(
f'[{llm_provider}] returned an invalid response: "{detail[:500]}"'
)
if payload.get("is_error") or completed.returncode != 0:
reason = str(payload.get("result") or "").strip() or (
f"claude cli exited with code {completed.returncode}"
)
# 容器里无法执行交互式 /login,这里直接给出可用的鉴权方式。
if "login" in reason.lower():
reason += (
" (run `claude setup-token` on the host and pass the token "
"to the container as CLAUDE_CODE_OAUTH_TOKEN)"
)
raise Exception(
f'[{llm_provider}] returned an error response: "{reason[:500]}"'
)
return _normalize_text_response(payload.get("result"), llm_provider)
if adapter == "kimi_code":
# Kimi 订阅(Plus+ 的 Kimi Code 权益)不签发按量 API Key,鉴权由
# OAuth 设备授权完成:WebUI 引导用户在浏览器里一键确认,本地保存
# refresh token;access token 15 分钟过期,由 kimi_code_oauth 透明
# 续期。请求走官方 OpenAI 兼容端点,与按量 API 是两套独立额度。
from app.services import kimi_code_oauth
try:
timeout_seconds = kimi_code_oauth.coerce_timeout(
extra_values.get("timeout")
)
except ValueError as timeout_error:
raise ValueError(f"{llm_provider}: {timeout_error}") from None
access_token = kimi_code_oauth.get_valid_access_token()
# 授权区域以凭证为准:token 只对签发它的区域有效。用户在授权后切换
# API Platform 时,若按界面区域发请求会把中国签发的 token 打到
# Global 端点(必然 401),因此请求地址优先跟随 token 的签发区域。
authorized_region = kimi_code_oauth.authorized_region()
if authorized_region:
request_base_url = kimi_code_oauth.api_base_url(authorized_region)
if base_url and authorized_region != kimi_code_oauth.resolve_region(
base_url
):
logger.warning(
f"kimi_code: API Platform selection differs from the "
f"authorized region; using '{authorized_region}' to match "
"the issued token"
)
else:
request_base_url = base_url or kimi_code_oauth.api_base_url(
kimi_code_oauth.resolve_region(base_url)
)
# 模型名留空时默认 kimi-for-coding:订阅权益内的专用编码模型,
# 避免硬编码的通用模型 id 随权益变化而失效。
request_model = model_name or "kimi-for-coding"
logger.info(f"requesting kimi code chat completion, model: {request_model}")
# Provider 自身的宽松超时在连接测试场景下要收紧到测试预算以内。
effective_timeout = timeout_seconds
if request_timeout is not None:
effective_timeout = min(effective_timeout, request_timeout)
client = sdk_client = OpenAI(
api_key=access_token,
base_url=request_base_url,
timeout=effective_timeout,
# 服务端反爬管线要求与 CLI 一致的设备身份头。
default_headers=kimi_code_oauth.api_headers(),
**_sdk_transport_options(None, max_retries),
)
response = trace_sdk_call(
llm_provider, base_url, client.chat.completions.create,
model=request_model, messages=[{"role": "user", "content": prompt}]
)
if response and isinstance(response, ChatCompletion):
return _extract_chat_completion_text(response, llm_provider)
raise Exception(
f'[{llm_provider}] returned an invalid response: "{response}"'
)
if adapter == "modelscope":
content = ""
client = sdk_client = OpenAI(
api_key=api_key,
base_url=base_url,
**_sdk_transport_options(request_timeout, max_retries),
)
response = sdk_stream = trace_sdk_call(
llm_provider, base_url, client.chat.completions.create,
model=model_name,
messages=[{"role": "user", "content": prompt}],
extra_body={"enable_thinking": False},
stream=True,
)
if response:
for chunk in response:
if not chunk.choices:
continue
delta = chunk.choices[0].delta
if delta and delta.content:
content += delta.content
if not content.strip():
raise ValueError("Empty content in stream response")
return _normalize_text_response(content, llm_provider)
else:
raise Exception(f"[{llm_provider}] returned an empty response")
client = sdk_client = OpenAI(
api_key=api_key,
base_url=base_url,
**_sdk_transport_options(request_timeout, max_retries),
)
response = trace_sdk_call(
llm_provider, base_url, client.chat.completions.create,
model=model_name, messages=[{"role": "user", "content": prompt}]
)
if response:
if isinstance(response, ChatCompletion):
return _extract_chat_completion_text(response, llm_provider)
else:
raise Exception(
f'[{llm_provider}] returned an invalid response: "{response}", please check your network '
f"connection and try again."
)
else:
raise Exception(
f"[{llm_provider}] returned an empty response, please check your network connection and try again."
)
except Exception as e:
return f"Error: {_sanitize_error_message(e)}"
finally:
for resource in (sdk_stream, sdk_client):
close = getattr(resource, "close", None)
if callable(close):
try:
close()
except Exception as cleanup_error:
logger.warning(
f"could not close LLM transport: {type(cleanup_error).__name__}"
)
def test_connection() -> tuple[bool, str, float]:
"""
使用当前 Provider 配置发起一次最小请求,验证实际生成链路是否可用。
连接测试直接复用 `_generate_response()`,因此会覆盖 API Key、Base URL、
模型名称和 Provider 专用字段,但不会进入脚本生成的重试逻辑,也不会发送
用户的视频主题或文案。请求使用有界的短超时并禁用 SDK 自动重试:端点
不可达(代理、防火墙或 DNS 故障)时快速失败,并附上排查方向,而不是
挂起数分钟后只留下 "Request timed out."(issue #1642)。返回值依次为
成功状态、错误信息和请求耗时。
"""
started_at = perf_counter()
response = _generate_response(
prompt="Reply with exactly: OK",
request_timeout=LLM_CONNECTION_TEST_TIMEOUT_SECONDS,
max_retries=LLM_CONNECTION_TEST_MAX_RETRIES,
)
elapsed = perf_counter() - started_at
if not response:
error_message = "LLM returned an empty response"
logger.warning(f"llm connection test failed: {error_message}")
return False, error_message, elapsed
if response.startswith("Error:"):
error_message = _explain_connection_test_failure(
response.removeprefix("Error:").strip()
)
logger.warning(f"llm connection test failed: {error_message}")
return False, error_message, elapsed
logger.info(f"llm connection test succeeded, elapsed: {elapsed:.2f}s")
return True, "", elapsed
def _limit_script_text(text: str | None, max_length: int, field_name: str) -> str:
value = (text or "").strip()
if len(value) <= max_length:
return value
# API 层已经用 Pydantic 做长度校验;这里继续兜底,是为了保护
# WebUI 或内部服务直接调用 generate_script 时不会把超长提示词发送给模型,
# 避免 token 成本异常和请求失败。
logger.warning(
f"{field_name} is too long and will be truncated to {max_length} characters."
)
return value[:max_length]
def _normalize_script_paragraph_number(paragraph_number: int | None) -> int:
try:
value = int(paragraph_number or MIN_SCRIPT_PARAGRAPH_NUMBER)
except (TypeError, ValueError):
value = MIN_SCRIPT_PARAGRAPH_NUMBER
if value < MIN_SCRIPT_PARAGRAPH_NUMBER or value > MAX_SCRIPT_PARAGRAPH_NUMBER:
# WebUI 和 API 都会限制范围;这里兜底处理内部调用,避免异常参数直接扩大
# LLM 生成成本或生成空结果。
logger.warning(
f"script paragraph_number is out of range and will be clamped: {value}"
)
return max(MIN_SCRIPT_PARAGRAPH_NUMBER, min(value, MAX_SCRIPT_PARAGRAPH_NUMBER))
return value
def build_script_prompt(
video_subject: str,
language: str = "",
paragraph_number: int = 1,
video_script_prompt: str = "",
custom_system_prompt: str = "",
) -> str:
paragraph_number = _normalize_script_paragraph_number(paragraph_number)
video_script_prompt = _limit_script_text(
video_script_prompt, MAX_SCRIPT_PROMPT_LENGTH, "video_script_prompt"
)
custom_system_prompt = _limit_script_text(
custom_system_prompt, MAX_SCRIPT_SYSTEM_PROMPT_LENGTH, "custom_system_prompt"
)
# 将“脚本生成规则”和“运行时上下文”分开拼接。这样高级用户即使覆盖默认
# system prompt,也不会漏掉视频主题、语言、段落数这些每次生成都必须带上的参数。
prompt = custom_system_prompt or DEFAULT_SCRIPT_SYSTEM_PROMPT
prompt += f"""
# Initialization:
- video subject: {video_subject}
- number of paragraphs: {paragraph_number}
""".rstrip()
if language:
prompt += f"\n- language: {language}"
if video_script_prompt:
prompt += f"""
# Additional User Requirements:
{video_script_prompt}
""".rstrip()
return prompt
def generate_script(
video_subject: str,
language: str = "",
paragraph_number: int = 1,
video_script_prompt: str = "",
custom_system_prompt: str = "",
app_config=None,
) -> str:
paragraph_number = _normalize_script_paragraph_number(paragraph_number)
video_script_prompt = _limit_script_text(
video_script_prompt, MAX_SCRIPT_PROMPT_LENGTH, "video_script_prompt"
)
custom_system_prompt = _limit_script_text(
custom_system_prompt, MAX_SCRIPT_SYSTEM_PROMPT_LENGTH, "custom_system_prompt"
)
prompt = build_script_prompt(
video_subject=video_subject,
language=language,
paragraph_number=paragraph_number,
video_script_prompt=video_script_prompt,
custom_system_prompt=custom_system_prompt,
)
final_script = ""
logger.info(
"generating video script: "
f"subject={video_subject}, paragraph_number={paragraph_number}, "
f"has_custom_prompt={bool(video_script_prompt.strip())}, "
f"has_custom_system_prompt={bool(custom_system_prompt.strip())}"
)
def format_response(response):
# Clean the script
# Remove emphasis and heading markers without changing inline names
# or musical notes such as C#, F# and G#.
response = response.replace("*", "")
response = re.sub(
r"(?m)^[ \t]{0,3}#{1,6}[ \t]+(.*?)[ \t]*$",
lambda match: re.sub(r"[ \t]+#+[ \t]*$", "", match.group(1)),
response,
)
# Link labels are narration; discard destinations before stripping
# bracketed stage directions. Common parenthesized URL components fit.
response = re.sub(
r"\[([^\]\n]+)\]\((?:[^()\n]|\([^()\n]*\))*\)",
lambda match: match.group(1),
response,
)
# Remove markdown syntax. Use non-greedy .*? so each bracket/paren
# group is removed independently; the greedy form would eat all text
# between the first opener and the last closer on the same line.
response = re.sub(r"\[.*?\]", "", response)
response = re.sub(r"\(.*?\)", "", response)
# Split the script into paragraphs
paragraphs = response.split("\n\n")
# Select the specified number of paragraphs
# selected_paragraphs = paragraphs[:paragraph_number]
# Join the selected paragraphs into a single string
return "\n\n".join(paragraphs)
for i in range(_max_retries):
try:
if app_config is None:
response = _generate_response(prompt=prompt)
else:
response = _generate_response(prompt=prompt, app_config=app_config)
if isinstance(response, str) and response.startswith("Error: "):
# _generate_response returns provider failures as text. Passing
# that text through would make the task treat it as narration.
raise ValueError(response)
if response:
candidate = format_response(response).strip()
else:
logging.error("gpt returned an empty response")
candidate = ""
# Some upstream providers may return quota errors as plain text.
if candidate and "当日额度已消耗完" in candidate:
raise ValueError(candidate)
if candidate:
final_script = candidate
break
except Exception as e:
logger.error(f"failed to generate script: {e}")
if i < _max_retries - 1:
logger.warning(f"failed to generate video script, trying again... {i + 1}")
if not final_script:
logger.error("failed to generate video script after retries")
else:
logger.success(f"completed: \n{final_script}")
return final_script.strip()
def _strip_code_fence(text: str) -> str:
"""Strip a surrounding markdown code fence from an LLM response.
Non-OpenAI providers (Claude, Gemini, …) frequently wrap JSON output in a
```json … ``` fence even when asked to return raw JSON. Removing it lets the
first json.loads() succeed instead of falling through to the regex recovery
path (and spuriously logging a warning). Mirrors the DOTALL handling already
used in _parse_social_metadata().
"""
t = (text or "").strip()
if t.startswith("```"):
t = re.sub(r"^```[a-zA-Z0-9]*\s*", "", t)
t = re.sub(r"\s*```$", "", t)
return t.strip()
def _extract_embedded_json(text: str, opening: str):
"""Decode one complete JSON value without absorbing later prose/examples."""
decoder = json.JSONDecoder()
for index, character in enumerate(text or ""):
if character != opening:
continue
try:
value, _end = decoder.raw_decode(text[index:])
except json.JSONDecodeError:
continue
return value
raise ValueError("response contains no complete JSON value")
def generate_terms(
video_subject: str,
video_script: str,
amount: int = 5,
match_script_order: bool = False,
app_config=None,
) -> List[str]:
video_script = utils.remove_pause_tags(video_script or "").strip()
if match_script_order:
goal = (
f"Generate {amount} chronological stock-video search terms that follow "
"the order of topics in the video script."
)
ordering_rule = (
"6. keep the terms in the same order as the script narration; "
"earlier terms must describe earlier visual moments."
)
# 有序关键词模式下,示例数量要和 amount 保持一致,避免模型被固定
# 的 4 个示例误导,导致长文案只返回少量关键词,影响素材覆盖度。
example_terms = [
"opening visual topic",
*[f"script visual topic {index}" for index in range(2, max(amount, 1))],
"final visual topic",
]
output_example = json.dumps(example_terms[:amount], ensure_ascii=False)
else:
goal = (
f"Generate {amount} search terms for stock videos, depending on the "
"subject of a video."
)
ordering_rule = ""
output_example = (
'["search term 1", "search term 2", "search term 3",'
'"search term 4", "search term 5"]'
)
prompt = f"""
# Role: Video Search Terms Generator
## Goals:
{goal}
## Constrains:
1. the search terms are to be returned as a json-array of strings.
2. each search term should consist of 1-3 words, always add the main subject of the video.
3. you must only return the json-array of strings. you must not return anything else. you must not return the script.
4. the search terms must be related to the subject of the video.
5. reply with english search terms only.
{ordering_rule}
## Output Example:
{output_example}
## Context:
### Video Subject
{video_subject}
### Video Script
{video_script}
Please note that you must use English for generating video search terms; Chinese is not accepted.
""".strip()
logger.info(f"subject: {video_subject}, match_script_order: {match_script_order}")
search_terms = []
response = ""
for i in range(_max_retries):
search_terms = []
try:
if app_config is None:
response = _generate_response(prompt)
else:
response = _generate_response(prompt, app_config=app_config)
if response.startswith("Error: "):
# generate_terms 的公开返回类型是 List[str]。如果把 Provider 的
# 错误文案原样返回,下游只做空值判断时会把非空字符串误认为成功,
# 素材下载循环还会按字符遍历错误文案,产生无意义的外部请求。
# 这里统一返回空列表,让任务编排层在真实故障位置立即结束任务。
logger.error(f"failed to generate video terms: {response}")
return []
search_terms = json.loads(_strip_code_fence(response))
except Exception as e:
logger.warning(f"failed to generate video terms: {str(e)}")
if response:
try:
search_terms = _extract_embedded_json(response, "[")
except ValueError as e:
logger.warning(f"failed to generate video terms: {str(e)}")
# Apply the same contract to direct JSON and prose-wrapped recovery.
# Otherwise a nonempty array of numbers or objects reaches material search.
if not isinstance(search_terms, list) or not all(
isinstance(term, str) for term in search_terms
):
logger.error("response is not a list of strings.")
search_terms = []
# The model may ignore the requested count or return blank strings.
# Enforce the prompt contract before material providers are contacted;
# repeated topics remain meaningful in chronological mode.
search_terms = [term.strip() for term in search_terms if term.strip()][:amount]
if search_terms:
break
if i < _max_retries - 1:
logger.warning(f"failed to generate video terms, trying again... {i + 1}")
logger.success(f"completed: \n{search_terms}")
return search_terms
# =============================================================================
# Social publishing metadata
#
# 根据视频主题和脚本生成发布到短视频平台时常用的 title、caption 和 hashtags。
# 这块能力只复用现有 LLM provider,不接入任何外部发布服务,也不影响视频生成主链路。
# =============================================================================
# 不同平台的文案长度和 hashtag 数量偏好不同。这里使用保守上限,避免模型返回
# 过长内容后调用方还需要二次裁剪。
SOCIAL_PLATFORMS = {
"tiktok": {"title_max": 100, "caption_max": 2200, "hashtag_count": 5},
"youtube_shorts": {"title_max": 100, "caption_max": 5000, "hashtag_count": 3},
"instagram_reels": {"title_max": 125, "caption_max": 2200, "hashtag_count": 8},
"facebook_reels": {"title_max": 125, "caption_max": 2200, "hashtag_count": 5},
}
DEFAULT_SOCIAL_PLATFORM = "tiktok"
DEFAULT_SOCIAL_LANGUAGE = "auto"
MAX_SOCIAL_SUBJECT_LENGTH = 500
MAX_SOCIAL_SCRIPT_LENGTH = 8000
MAX_SOCIAL_LANGUAGE_LENGTH = 64
SOCIAL_PLATFORM_LABELS = {
"tiktok": "TikTok",
"youtube_shorts": "YouTube Shorts",
"instagram_reels": "Instagram Reels",
"facebook_reels": "Facebook Reels",
}
# LLM 不可用时的通用兜底标签。这里故意不绑定某个国家或语种,保证 API
# 对中文、英文、越南语等不同场景都能返回可用结构。
DEFAULT_SOCIAL_HASHTAGS = [
"#shorts",
"#viral",
"#trending",
"#fyp",
"#video",
"#reels",
"#creator",
"#content",
]
def _resolve_social_platform(platform: str | None) -> str:
value = (platform or "").strip().lower()
return value if value in SOCIAL_PLATFORMS else DEFAULT_SOCIAL_PLATFORM
def _normalize_social_language(language: str | None) -> str:
value = (language or DEFAULT_SOCIAL_LANGUAGE).strip()
if len(value) > MAX_SOCIAL_LANGUAGE_LENGTH:
logger.warning(
"social metadata language is too long and will be truncated to "
f"{MAX_SOCIAL_LANGUAGE_LENGTH} characters."
)
value = value[:MAX_SOCIAL_LANGUAGE_LENGTH]
return value or DEFAULT_SOCIAL_LANGUAGE
def _limit_social_text(text: str | None, max_length: int, field_name: str) -> str:
value = (text or "").strip()
if len(value) <= max_length:
return value
# API 层会限制长度;这里继续兜底,是为了保护内部调用或未来 WebUI
# 直接调用时不会把超长内容发送给模型,避免 token 成本异常。
logger.warning(
f"{field_name} is too long and will be truncated to {max_length} characters."
)
return value[:max_length]
def _social_language_instruction(language: str | None) -> str:
language = _normalize_social_language(language)
if language.lower() == DEFAULT_SOCIAL_LANGUAGE:
return (
"Use the same language as the video subject and script. If the subject "
"and script use different languages, prefer the script language."
)
return f'Write "title" and "caption" in this language: {language}.'
def _clamp_text(text, max_length: int) -> str:
value = ("" if text is None else str(text)).strip()
if max_length and len(value) > max_length:
return value[:max_length].rstrip()
return value
def _normalize_hashtags(raw, count: int) -> List[str]:
"""
将 LLM 返回的 hashtag 统一整理成 `#tag` 格式。
LLM 可能返回字符串、数组、带空格的词组、重复标签或包含标点的内容。
这里集中清洗,可以让接口响应结构稳定,也避免平台发布时出现空标签、
重复标签或不符合常见格式的 hashtag。
"""
if isinstance(raw, str):
candidates = re.split(r"[\s,]+", raw)
elif isinstance(raw, (list, tuple)):
# 数组里的每一项视为一个完整标签,因此 "du lich" 会变成
# "#dulich",而不是拆成两个标签。
candidates = [str(entry) for entry in raw]
else:
candidates = []
seen = set()
result: List[str] = []
for item in candidates:
# 印地语、泰语等依赖组合符,不能像 \w 那样直接删除。但组合符必须
# 附着于保留的字符;emoji 被删除后,其变体选择符也应丢弃,否则
# #☕️ 会留下只有不可见字符的标签,#coffee☕️ 也无法与 #coffee 去重。
# 数字本身会保留,因此还需清理 emoji 展示选择符和键帽组合符,
# 让 #1️⃣ 与 #1 一致;其它语言组合符和 CJK 字形变体仍按附着规则保留。
characters = []
can_attach_mark = False
for character in item:
if character in {"\ufe0e", "\ufe0f", "\u20e3"}:
can_attach_mark = False
elif character.isalnum() or character == "_":
characters.append(character)
can_attach_mark = True
elif can_attach_mark and unicodedata.category(character).startswith("M"):
characters.append(character)
else:
can_attach_mark = False
tag = "".join(characters)
if not tag:
continue
key = tag.lower()
if key in seen:
continue
seen.add(key)
result.append(f"#{tag}")
if count and len(result) >= count:
break
return result
def build_social_metadata_prompt(
video_subject: str,
video_script: str = "",
language: str = DEFAULT_SOCIAL_LANGUAGE,
platform: str = DEFAULT_SOCIAL_PLATFORM,
) -> str:
video_subject = _limit_social_text(
video_subject, MAX_SOCIAL_SUBJECT_LENGTH, "video_subject"
)
video_script = _limit_social_text(
video_script, MAX_SOCIAL_SCRIPT_LENGTH, "video_script"
)
platform = _resolve_social_platform(platform)
spec = SOCIAL_PLATFORMS[platform]
label = SOCIAL_PLATFORM_LABELS.get(platform, platform)
language_instruction = _social_language_instruction(language)
prompt = f"""
# Role: Short-Video Social Media Copywriter
## Goal
Write engaging publishing metadata for a short video that will be posted on {label}.
## Constraints
1. Respond ONLY with a single valid minified JSON object. No markdown, no code fences, no commentary.
2. The JSON must contain exactly these keys: "title", "caption", "hashtags".
3. "title": a catchy hook, at most {spec["title_max"]} characters.
4. "caption": an engaging description that ends with a call to action, at most {spec["caption_max"]} characters. Do not put hashtags inside the caption.
5. "hashtags": a JSON array of exactly {spec["hashtag_count"]} strings. Each must start with "#", contain no spaces, and be relevant to the topic and to {label}.
6. {language_instruction}
## Output Example
{{"title":"...","caption":"...","hashtags":["#example","#video"]}}
## Context
### Video Subject
{video_subject}
### Video Script
{video_script}
""".strip()
return prompt
def _parse_social_metadata(response: str, platform: str) -> dict:
spec = SOCIAL_PLATFORMS[_resolve_social_platform(platform)]
data = None
try:
data = json.loads(_strip_code_fence(response))
except Exception:
# 部分模型会在 JSON 外层包一段说明文字或 markdown fence。
# API 调用方只需要稳定结构,所以这里尝试提取第一个 JSON object。
data = _extract_embedded_json(response, "{")
if not isinstance(data, dict):
raise ValueError("social metadata response is not a JSON object")
title = _clamp_text(data.get("title", ""), spec["title_max"])
caption = _clamp_text(data.get("caption", ""), spec["caption_max"])
hashtags = _normalize_hashtags(data.get("hashtags", []), spec["hashtag_count"])
if not title and not caption:
raise ValueError("social metadata response is missing both title and caption")
return {"title": title, "caption": caption, "hashtags": hashtags}
def _fallback_social_metadata(
video_subject: str, video_script: str, platform: str
) -> dict:
spec = SOCIAL_PLATFORMS[_resolve_social_platform(platform)]
subject = (video_subject or "").strip()
script = (video_script or "").strip()
title = subject
if not title and script:
# 中日文句末通常没有空格;英文仍要求空白,避免拆开 2.0 等数字。
title = re.split(r"(?<=[.!?])\s+|(?<=[。!?])\s*", script, maxsplit=1)[0]
return {
"title": _clamp_text(title, spec["title_max"]),
"caption": _clamp_text(script or subject, spec["caption_max"]),
"hashtags": _normalize_hashtags(DEFAULT_SOCIAL_HASHTAGS, spec["hashtag_count"]),
}
def generate_social_metadata(
video_subject: str,
video_script: str = "",
language: str = DEFAULT_SOCIAL_LANGUAGE,
platform: str = DEFAULT_SOCIAL_PLATFORM,
) -> dict:
"""
生成短视频发布文案元数据。
返回结构固定为 `{"title": str, "caption": str, "hashtags": List[str]}`。
如果 LLM 不可用或返回格式异常,会降级为通用启发式结果,保证 API
调用方始终拿到可展示、可发布前编辑的数据结构。
"""
platform = _resolve_social_platform(platform)
language = _normalize_social_language(language)
video_subject = _limit_social_text(
video_subject, MAX_SOCIAL_SUBJECT_LENGTH, "video_subject"
)
video_script = _limit_social_text(
video_script, MAX_SOCIAL_SCRIPT_LENGTH, "video_script"
)
prompt = build_social_metadata_prompt(
video_subject=video_subject,
video_script=video_script,
language=language,
platform=platform,
)
logger.info(f"generating social metadata: platform={platform}, language={language}")
response = ""
for i in range(_max_retries):
try:
response = _generate_response(prompt)
if isinstance(response, str) and response.startswith("Error: "):
logger.error(f"failed to generate social metadata: {response}")
break
metadata = _parse_social_metadata(response, platform)
logger.success(f"completed: \n{metadata}")
return metadata
except Exception as e:
logger.warning(f"failed to parse social metadata: {str(e)}")
if i < _max_retries - 1:
logger.warning(
f"failed to generate social metadata, trying again... {i + 1}"
)
logger.warning("falling back to heuristic social metadata")
return _fallback_social_metadata(video_subject, video_script, platform)
if __name__ == "__main__":
video_subject = "生命的意义是什么"
script = generate_script(
video_subject=video_subject, language="zh-CN", paragraph_number=1
)
print("######################")
print(script)
search_terms = generate_terms(
video_subject=video_subject, video_script=script, amount=5
)
print("######################")
print(search_terms)