From a80f34137ce52a288b6c4a431a4a6c42e33b37a0 Mon Sep 17 00:00:00 2001
From: "2569718930@qq.com" <2569718930@qq.com>
Date: Sat, 23 May 2026 09:50:08 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20AI=20=E7=A9=BA=E5=93=8D?=
=?UTF-8?q?=E5=BA=94=EF=BC=9A=E7=A7=BB=E9=99=A4=20prompt=20=E4=B8=AD?=
=?UTF-8?q?=E7=9A=84=20HTML=20=E6=A0=87=E7=AD=BE=E7=A4=BA=E4=BE=8B?=
=?UTF-8?q?=EF=BC=8C=E5=8A=A0=E5=85=A5=20finish=5Freason=20=E8=AF=8A?=
=?UTF-8?q?=E6=96=AD=E6=97=A5=E5=BF=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/utils/daily_weather_report.py | 28 +++++++++++++++++++---------
1 file changed, 19 insertions(+), 9 deletions(-)
diff --git a/src/utils/daily_weather_report.py b/src/utils/daily_weather_report.py
index 905af536..b5ce41d7 100644
--- a/src/utils/daily_weather_report.py
+++ b/src/utils/daily_weather_report.py
@@ -103,14 +103,15 @@ def _build_ai_prompt(cities_data: List[Dict[str, Any]], report_date: str) -> str
return (
f"今天是 {report_date}。以下是今天中国主要城市的天气预报数据(JSON格式)。\n\n"
f"{data_json}\n\n"
- "请用自然亲切的中文写一段天气日报。每个城市必须用以下格式逐城播报:\n\n"
- "城市名 天气,最高温。一句话体感/建议。\n\n"
+ "请用自然亲切的中文写一段天气日报。每个城市逐行播报,格式参考:\n\n"
+ "城市名 天气描述,最高N度。一句话体感或穿衣建议。\n\n"
"要求:\n"
- "1. 「天气」必须是 weather_code 对应的中文(晴/多云/阴/雨/雾/雪/雷暴),不得省略\n"
- "2. forecast_high 是预报最高温(℃)\n"
- "3. 开头加问候语「☀️ 早上好!今天是x月x日」\n"
- "4. 逐城播报完毕后直接结束,不写结尾祝福语、不写总结、不写免责声明\n"
- "5. 总体控制在 250 字以内\n"
+ "1. 天气描述用 weather_code 对应的中文词(晴/多云/阴/雨/雾/雪/雷暴),不要跳过\n"
+ "2. 最高温来自 forecast_high,单位℃\n"
+ "3. 开头问候语「☀️ 早上好!今天是x月x日」\n"
+ "4. 播报完直接结束,禁止写结尾祝福、总结、免责声明\n"
+ "5. 输出用 HTML:城市名加 标签,每城一行\n"
+ "6. 总字数不超过 250\n"
"weather_code 参考:0=晴, 1-3=多云, 45-48=雾, 51-67=雨, 71-86=雪, 95-99=雷暴"
)
@@ -151,8 +152,17 @@ def _call_ai(prompt: str) -> Optional[str]:
)
resp.raise_for_status()
data = resp.json()
- content = data.get("choices", [{}])[0].get("message", {}).get("content", "")
- return str(content).strip() if content else None
+ choice = (data.get("choices") or [{}])[0]
+ content = choice.get("message", {}).get("content", "")
+ finish = choice.get("finish_reason", "")
+ if not str(content or "").strip():
+ logger.warning(
+ "daily_weather_report: AI empty content finish_reason={} model={}",
+ finish,
+ model,
+ )
+ return None
+ return str(content).strip()
except Exception as exc:
logger.warning(f"daily_weather_report: AI call failed: {exc}")
return None