fallback: 小果API不可用时降级到关键词分类

This commit is contained in:
知微
2026-06-21 01:34:22 +08:00
parent 4774b0d53f
commit cab782bce5
+28 -4
View File
@@ -138,6 +138,29 @@ def translate_sentiment(s):
return m.get(s.lower() if isinstance(s, str) else "", s)
def fallback_classify(batch):
"""关键词降级分类(小果API不可用时)"""
positive_kw = ['突破', '增长', '利好', '加单', '订单', '放量', '新高', '获批', '量产',
'超预期', '投产', '融资', '增持', '回购', '降息', '减税', '补贴',
'国产替代', '自主可控', '准入']
negative_kw = ['管制', '限制', '制裁', '利空', '减持', '抛售', '下跌', '跌停',
'风险', '违约', '调查', '暂停', '取消', '下滑', '亏损', '裁员',
'诉讼', '退市', '做空', '关税', '禁令']
for a in batch:
text = a['title'] + (a.get('content') or '')
pos = sum(1 for kw in positive_kw if kw in text)
neg = sum(1 for kw in negative_kw if kw in text)
if pos > neg:
a['sentiment'] = '利好'
elif neg > pos:
a['sentiment'] = '利空'
else:
a['sentiment'] = '中性'
a['summary'] = a['title'][:80]
return batch
def main():
conn = get_conn()
signals = conn.execute(
@@ -199,11 +222,12 @@ def main():
results = call_xiaoguo(batch)
if not results:
print(" 小果分析失败", flush=True)
conn.close()
return
print(" 小果API不可用,降级到关键词分类", flush=True)
fallback_classify(batch)
results = None # batch already has sentiment/summary set
# 合并结果(用索引位置匹配)
if results and isinstance(results, list):
# 小果LLM返回结果,按索引匹配
for i, r in enumerate(results):
if i < len(batch):
batch[i]["sentiment"] = translate_sentiment(r.get("sentiment", r.get("情感", "")))