Skip to content

Commit 0cd1a25

Browse files
committed
增强ai_analyzer.py中的错误处理,确保AI模型返回空响应时记录错误并返回适当提示。更新提示文本以使用中文,提升用户体验。修改notifier.py中的摘要格式,添加方括号以突出显示摘要内容。
1 parent 3f9ec7c commit 0cd1a25

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

MarketPulse/ai_analyzer.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def run(self, articles: list):
3939
model=self.model_name, contents=prompt
4040
)
4141

42+
if not response or not response.text:
43+
logging.error("AI模型返回了空响应。")
44+
return None
45+
4246
clean_json_str = (
4347
response.text.strip().replace("```json", "").replace("```", "")
4448
)
@@ -48,7 +52,7 @@ def run(self, articles: list):
4852

4953
except json.JSONDecodeError as e:
5054
logging.error(
51-
f"AI响应JSON解析失败: {e}. 响应内容: '{response.text[:200]}...'"
55+
f"AI响应JSON解析失败: {e}. 响应内容: '{response.text[:200] if response and response.text else 'None'}...'"
5256
)
5357
return None
5458
except Exception as e:
@@ -79,7 +83,7 @@ def _create_prompt(self, articles: list) -> str:
7983
ensure_ascii=False,
8084
)
8185
return f"""
82-
你是一个高效的信息提取专家。请为以下新闻列表中的每一篇文章生成一个不超过50字的精炼摘要
86+
你是一个高效的信息提取专家。请使用中文为以下新闻列表中的每一篇文章生成一个不超过50字的精炼摘要
8387
新闻文章列表: {input_articles_str}
8488
请严格按照以下JSON数组格式返回结果,每个对象包含文章的`id`和你的`summary`。
8589
[
@@ -110,7 +114,7 @@ def _create_prompt(self, articles: list) -> str:
110114
ensure_ascii=False,
111115
)
112116
return f"""
113-
你是一位顶级的金融分析师。请基于以下JSON格式的新闻文章列表,为每一篇文章提供独立的、专业的市场分析和投资建议。
117+
你是一位顶级的金融分析师。请使用中文,基于以下JSON格式的新闻文章列表,为每一篇文章提供独立的、专业的市场分析和投资建议。
114118
请严格按照指定的JSON数组格式输出你的分析结果。
115119
116120
新闻文章列表:
@@ -131,13 +135,11 @@ def _create_prompt(self, articles: list) -> str:
131135
}}
132136
}}
133137
134-
---
135-
### 特别指令:顶级新闻社VIP分析标准
138+
特别指令:顶级新闻社VIP分析标准
136139
对于来源为以下列表中的新闻,你的分析必须遵循更高标准:
137-
- **顶级新闻社列表**: {list(config.TOP_TIER_NEWS_SOURCES)}
138-
- **市场影响分析**: 必须给出最具体的影响范围,严禁使用模糊词汇。
139-
- **来源可信度**: 必须给出95%-100%之间的一个具体数值。
140-
---
140+
- 顶级新闻社列表: {list(config.TOP_TIER_NEWS_SOURCES)}
141+
- 市场影响分析: 必须给出最具体的影响范围,严禁使用模糊词汇。
142+
- 来源可信度: 必须给出95%-100%之间的一个具体数值。
141143
"""
142144

143145

@@ -182,7 +184,7 @@ def __init__(self):
182184

183185
def _create_prompt(self, content: str) -> str:
184186
return f"""
185-
你是一个专业的市场评论员。请将以下市场分析内容总结归纳在100字以内,提取最核心的投资洞见。
187+
你是一个专业的市场评论员。请使用中文将以下市场分析内容总结归纳在100字以内,提取最核心的投资洞见。
186188
你的总结需要精炼、专业、直击要点。
187189
188190
待总结的内容:
@@ -206,6 +208,11 @@ def run(self, content: str):
206208
response = self.client.models.generate_content(
207209
model=self.model_name, contents=prompt
208210
)
211+
212+
if not response or not response.text:
213+
logging.error("AI模型返回了空响应。")
214+
return "AI总结生成失败:模型返回空响应。"
215+
209216
summary = response.text.strip()
210217
logging.info("成功生成市场分析总结。")
211218
return summary

MarketPulse/notifier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def send_summary_notification(analyses, articles_map, batch_info=(1, 1)):
121121

122122
# 摘要作为小标题,建议中包含资产信息
123123
part = (
124-
f"{analysis.get('summary', 'N/A')}\n"
124+
f"[{analysis.get('summary', 'N/A')}]\n"
125125
f" - 建议: {action} {asset_name} ({asset_ticker}) (信心: {confidence})\n"
126126
f" - 理由: {insight.get('reasoning', 'N/A')}\n"
127127
f" - 来源: {source_display}"

0 commit comments

Comments
 (0)