From 9b41c3c75d447336a2cdb586f654956c9f130146 Mon Sep 17 00:00:00 2001 From: konjacpotato Date: Sun, 15 Feb 2026 16:16:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=AF=B9llm=E8=BE=93?= =?UTF-8?q?=E5=87=BAjson=E6=A0=BC=E5=BC=8F=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task/hot_topic/real_estate_story.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/task/hot_topic/real_estate_story.py b/task/hot_topic/real_estate_story.py index a9ab4e2..6106fd2 100644 --- a/task/hot_topic/real_estate_story.py +++ b/task/hot_topic/real_estate_story.py @@ -23,6 +23,18 @@ def story_edit_task(): logger.info(f"story_edit_task content id: {content.id}, title: {content.link}, platform: {content.platform}") story = llm_engine.think(f"故事素材:{content.content}") 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表 json_story = json.loads(story) title = json_story.get("title", "无标题")