增强对llm输出json格式的支持
All checks were successful
Gitea Actions Demo / deploy (push) Successful in 22s

This commit is contained in:
konjacpotato
2026-02-15 16:16:41 +08:00
parent f272b57541
commit 9b41c3c75d

View File

@ -23,6 +23,18 @@ def story_edit_task():
logger.info(f"story_edit_task content id: {content.id}, title: {content.link}, platform: {content.platform}") logger.info(f"story_edit_task content id: {content.id}, title: {content.link}, platform: {content.platform}")
story = llm_engine.think(f"故事素材:{content.content}") story = llm_engine.think(f"故事素材:{content.content}")
logger.info(f"story_edit_task content id: {content.id} story: {story}") logger.info(f"story_edit_task content id: {content.id} story: {story}")
# llm生成的结果有时不是json结构会在前后增加一些文本需要提取出json部分进行解析
try:
json_start = story.find("{")
json_end = story.rfind("}") + 1
if json_start != -1 and json_end != -1:
story = story[json_start:json_end]
else:
logger.warning(f"story_edit_task content id: {content.id} llm生成的结果不是有效的json格式无法提取故事内容")
continue
except json.JSONDecodeError:
logger.warning(f"story_edit_task content id: {content.id} llm生成的结果不是有效的json格式无法解析故事内容")
continue
# 将生成的故事写入Article表 # 将生成的故事写入Article表
json_story = json.loads(story) json_story = json.loads(story)
title = json_story.get("title", "无标题") title = json_story.get("title", "无标题")