fix(emoji): 为表情包描述生成增加 VLM 调用重试机制

VLM 在生成表情包描述时,其 API 调用可能因网络波动或服务暂时不可用而失败。此前的实现会在首次调用失败后直接中断流程。

为了提高该功能的健壮性,本次提交引入了重试逻辑:
- 在调用 VLM 生成描述时,最多尝试 3 次。
- 如果调用失败或返回空结果,将记录错误并等待 1 秒后重试。
- 这能有效应对暂时性网络或服务问题,显著提高表情包描述生成的成功率。
This commit is contained in:
tt-P607
2025-10-22 01:08:29 +08:00
parent 5bd59fe415
commit 3054329115

View File

@@ -970,14 +970,36 @@ class EmojiManager:
if not image_base64_frames:
raise RuntimeError("GIF表情包转换失败")
prompt = "这是一个GIF动图表情包的关键帧。请用不超过250字详细描述它的核心内容1. 动态画面展现了什么变化2. 它传达了什么核心情绪或玩的是什么梗3. 通常在什么场景下使用?请确保描述既包含关键信息,又能充分展现其内涵。"
description, _ = await self.vlm.generate_response_for_image(
prompt, image_base64_frames, "jpeg", temperature=0.3, max_tokens=600
)
description = None
for i in range(3):
try:
logger.info(f"[VLM调用] 正在为GIF表情包生成描述 (第 {i+1}/3 次)...")
description, _ = await self.vlm.generate_response_for_image(
prompt, image_base64_frames, "jpeg", temperature=0.3, max_tokens=600
)
if description and description.strip():
break
except Exception as e:
logger.error(f"VLM调用失败 (第 {i+1}/3 次): {e}", exc_info=True)
if i < 2:
logger.warning("表情包识别失败将在1秒后重试...")
await asyncio.sleep(1)
else:
prompt = "这是一个表情包。请用不超过250字详细描述它的核心内容1. 画面描绘了什么2. 它传达了什么核心情绪或玩的是什么梗3. 通常在什么场景下使用?请确保描述既包含关键信息,又能充分展现其内涵。"
description, _ = await self.vlm.generate_response_for_image(
prompt, image_base64, image_format, temperature=0.3, max_tokens=600
)
description = None
for i in range(3):
try:
logger.info(f"[VLM调用] 正在为静态表情包生成描述 (第 {i+1}/3 次)...")
description, _ = await self.vlm.generate_response_for_image(
prompt, image_base64, image_format, temperature=0.3, max_tokens=600
)
if description and description.strip():
break
except Exception as e:
logger.error(f"VLM调用失败 (第 {i+1}/3 次): {e}", exc_info=True)
if i < 2:
logger.warning("表情包识别失败将在1秒后重试...")
await asyncio.sleep(1)
# 4. 检查VLM描述是否有效
if not description or not description.strip():