fix: 修复 VLM 解析
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 2m51s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 2m51s
This commit is contained in:
@@ -1023,6 +1023,15 @@ class EmojiManager:
|
|||||||
- 必须是表情包,非普通截图。
|
- 必须是表情包,非普通截图。
|
||||||
- 图中文字不超过5个。
|
- 图中文字不超过5个。
|
||||||
请确保你的最终输出是严格的JSON对象,不要添加任何额外解释或文本。
|
请确保你的最终输出是严格的JSON对象,不要添加任何额外解释或文本。
|
||||||
|
输出格式:
|
||||||
|
```json
|
||||||
|
{{
|
||||||
|
"detailed_description": "",
|
||||||
|
"keywords": [],
|
||||||
|
"refined_sentence": "",
|
||||||
|
"is_compliant": true
|
||||||
|
}}
|
||||||
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
image_data_for_vlm, image_format_for_vlm = image_base64, image_format
|
image_data_for_vlm, image_format_for_vlm = image_base64, image_format
|
||||||
@@ -1042,9 +1051,7 @@ class EmojiManager:
|
|||||||
if not vlm_response_str:
|
if not vlm_response_str:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
match = re.search(r"\{.*\}", vlm_response_str, re.DOTALL)
|
vlm_response_json = self._parse_json_response(vlm_response_str)
|
||||||
if match:
|
|
||||||
vlm_response_json = json.loads(match.group(0))
|
|
||||||
description = vlm_response_json.get("detailed_description", "")
|
description = vlm_response_json.get("detailed_description", "")
|
||||||
emotions = vlm_response_json.get("keywords", [])
|
emotions = vlm_response_json.get("keywords", [])
|
||||||
refined_description = vlm_response_json.get("refined_sentence", "")
|
refined_description = vlm_response_json.get("refined_sentence", "")
|
||||||
@@ -1196,6 +1203,29 @@ class EmojiManager:
|
|||||||
logger.error(f"[错误] 删除异常处理文件时出错: {remove_error}")
|
logger.error(f"[错误] 删除异常处理文件时出错: {remove_error}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _parse_json_response(cls, response: str) -> dict[str, Any] | None:
|
||||||
|
"""解析 LLM 的 JSON 响应"""
|
||||||
|
try:
|
||||||
|
# 尝试提取 JSON 代码块
|
||||||
|
json_match = re.search(r"```json\s*(.*?)\s*```", response, re.DOTALL)
|
||||||
|
if json_match:
|
||||||
|
json_str = json_match.group(1)
|
||||||
|
else:
|
||||||
|
# 尝试直接解析
|
||||||
|
json_str = response.strip()
|
||||||
|
|
||||||
|
# 移除可能的注释
|
||||||
|
json_str = re.sub(r"//.*", "", json_str)
|
||||||
|
json_str = re.sub(r"/\*.*?\*/", "", json_str, flags=re.DOTALL)
|
||||||
|
|
||||||
|
data = json.loads(json_str)
|
||||||
|
return data
|
||||||
|
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
logger.warning(f"JSON 解析失败: {e}, 响应: {response[:200]}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
emoji_manager = None
|
emoji_manager = None
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user