134 lines
4.5 KiB
Python
134 lines
4.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from backend.enrich import llm_enricher
|
|
|
|
|
|
class DummyResponse:
|
|
def __init__(self, metadata):
|
|
self.metadata = metadata
|
|
|
|
|
|
class DummyModel:
|
|
def __init__(self, metadata):
|
|
self.metadata = metadata
|
|
self.calls = []
|
|
|
|
async def __call__(self, messages, structured_model=None, **kwargs):
|
|
self.calls.append(
|
|
{
|
|
"messages": messages,
|
|
"structured_model": structured_model,
|
|
"kwargs": kwargs,
|
|
}
|
|
)
|
|
return DummyResponse(self.metadata)
|
|
|
|
|
|
def test_analyze_news_row_with_llm_uses_agentscope_model(monkeypatch):
|
|
model = DummyModel(
|
|
{
|
|
"id": "news-1",
|
|
"relevance": "high",
|
|
"sentiment": "positive",
|
|
"key_discussion": "Demand remains resilient",
|
|
"summary": "Structured summary",
|
|
"reason_growth": "Orders improved",
|
|
"reason_decrease": "",
|
|
}
|
|
)
|
|
monkeypatch.setattr(llm_enricher, "llm_enrichment_enabled", lambda: True)
|
|
monkeypatch.setattr(llm_enricher, "_get_explain_model", lambda: model)
|
|
monkeypatch.setattr(
|
|
llm_enricher,
|
|
"get_explain_model_info",
|
|
lambda: {"provider": "DASHSCOPE", "model_name": "qwen-max", "label": "DASHSCOPE:qwen-max"},
|
|
)
|
|
|
|
result = llm_enricher.analyze_news_row_with_llm(
|
|
{
|
|
"id": "news-1",
|
|
"title": "Apple expands AI features",
|
|
"summary": "New devices and software updates were announced.",
|
|
}
|
|
)
|
|
|
|
assert result["sentiment"] == "positive"
|
|
assert result["summary"] == "Structured summary"
|
|
assert result["raw_json"]["model_label"] == "DASHSCOPE:qwen-max"
|
|
assert model.calls
|
|
assert model.calls[0]["structured_model"] is llm_enricher.EnrichedNewsItem
|
|
|
|
|
|
def test_analyze_news_rows_with_llm_uses_agentscope_structured_batch(monkeypatch):
|
|
model = DummyModel(
|
|
{
|
|
"items": [
|
|
{
|
|
"id": "news-1",
|
|
"relevance": "high",
|
|
"sentiment": "negative",
|
|
"key_discussion": "Margin pressure",
|
|
"summary": "Batch summary",
|
|
"reason_growth": "",
|
|
"reason_decrease": "Costs rose",
|
|
}
|
|
]
|
|
}
|
|
)
|
|
monkeypatch.setattr(llm_enricher, "llm_enrichment_enabled", lambda: True)
|
|
monkeypatch.setattr(llm_enricher, "_get_explain_model", lambda: model)
|
|
monkeypatch.setattr(
|
|
llm_enricher,
|
|
"get_explain_model_info",
|
|
lambda: {"provider": "DASHSCOPE", "model_name": "qwen-max", "label": "DASHSCOPE:qwen-max"},
|
|
)
|
|
|
|
result = llm_enricher.analyze_news_rows_with_llm(
|
|
[
|
|
{
|
|
"id": "news-1",
|
|
"title": "Apple margins pressured",
|
|
"summary": "Costs increased this quarter.",
|
|
}
|
|
]
|
|
)
|
|
|
|
assert result["news-1"]["sentiment"] == "negative"
|
|
assert result["news-1"]["reason_decrease"] == "Costs rose"
|
|
assert result["news-1"]["raw_json"]["model_label"] == "DASHSCOPE:qwen-max"
|
|
assert model.calls
|
|
assert model.calls[0]["structured_model"] is llm_enricher.EnrichedNewsBatch
|
|
|
|
|
|
def test_analyze_range_with_llm_uses_agentscope_structured_output(monkeypatch):
|
|
model = DummyModel(
|
|
{
|
|
"summary": "该股在区间内震荡下行,相关新闻主要集中在盈利预期和供应链扰动。",
|
|
"trend_analysis": "前半段受利空新闻压制,后半段跌幅收敛。",
|
|
"bullish_factors": ["估值消化后出现部分承接"],
|
|
"bearish_factors": ["盈利预期下修", "供应链扰动持续"],
|
|
}
|
|
)
|
|
monkeypatch.setattr(llm_enricher, "llm_range_analysis_enabled", lambda: True)
|
|
monkeypatch.setattr(llm_enricher, "_get_explain_model", lambda: model)
|
|
monkeypatch.setattr(
|
|
llm_enricher,
|
|
"get_explain_model_info",
|
|
lambda: {"provider": "DASHSCOPE", "model_name": "qwen-max", "label": "DASHSCOPE:qwen-max"},
|
|
)
|
|
|
|
result = llm_enricher.analyze_range_with_llm(
|
|
{
|
|
"ticker": "AAPL",
|
|
"start_date": "2026-03-10",
|
|
"end_date": "2026-03-16",
|
|
"price_change_pct": -3.42,
|
|
}
|
|
)
|
|
|
|
assert result["summary"].startswith("该股在区间内震荡下行")
|
|
assert result["model_label"] == "DASHSCOPE:qwen-max"
|
|
assert result["bearish_factors"] == ["盈利预期下修", "供应链扰动持续"]
|
|
assert model.calls
|
|
assert model.calls[0]["structured_model"] is llm_enricher.RangeAnalysisPayload
|