agent功能开发增加MCP后端
This commit is contained in:
103
concept_api.py
103
concept_api.py
@@ -22,15 +22,15 @@ openai_client = None
|
||||
mysql_pool = None
|
||||
|
||||
# 配置
|
||||
ES_HOST = 'http://192.168.1.58:9200'
|
||||
OPENAI_BASE_URL = "http://192.168.1.58:8000/v1"
|
||||
ES_HOST = 'http://127.0.0.1:9200'
|
||||
OPENAI_BASE_URL = "http://127.0.0.1:8000/v1"
|
||||
OPENAI_API_KEY = "dummy"
|
||||
EMBEDDING_MODEL = "qwen3-embedding-8b"
|
||||
INDEX_NAME = 'concept_library'
|
||||
|
||||
# MySQL配置
|
||||
MYSQL_CONFIG = {
|
||||
'host': '192.168.1.14',
|
||||
'host': '192.168.1.8',
|
||||
'user': 'root',
|
||||
'password': 'Zzl5588161!',
|
||||
'db': 'stock',
|
||||
@@ -1045,7 +1045,16 @@ async def get_concept_price_timeseries(
|
||||
):
|
||||
"""获取概念在指定日期范围内的涨跌幅时间序列数据"""
|
||||
if not mysql_pool:
|
||||
raise HTTPException(status_code=503, detail="数据库连接不可用")
|
||||
logger.warning(f"[PriceTimeseries] MySQL 连接不可用,返回空时间序列数据")
|
||||
# 返回空时间序列而不是 503 错误
|
||||
return PriceTimeSeriesResponse(
|
||||
concept_id=concept_id,
|
||||
concept_name=concept_id, # 无法查询名称,使用 ID
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
data_points=0,
|
||||
timeseries=[]
|
||||
)
|
||||
|
||||
if start_date > end_date:
|
||||
raise HTTPException(status_code=400, detail="开始日期不能晚于结束日期")
|
||||
@@ -1150,11 +1159,93 @@ async def get_concept_statistics(
|
||||
min_stock_count: int = Query(3, ge=1, description="最少股票数量过滤")
|
||||
):
|
||||
"""获取概念板块统计数据 - 涨幅榜、跌幅榜、活跃榜、波动榜、连涨榜"""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# 如果 MySQL 不可用,直接返回示例数据(而不是返回 503)
|
||||
if not mysql_pool:
|
||||
raise HTTPException(status_code=503, detail="数据库连接不可用")
|
||||
logger.warning("[Statistics] MySQL 连接不可用,使用示例数据")
|
||||
|
||||
# 计算日期范围
|
||||
if days is not None and (start_date is not None or end_date is not None):
|
||||
pass # 参数冲突,但仍使用 days
|
||||
|
||||
if start_date is not None and end_date is not None:
|
||||
pass # 使用提供的日期
|
||||
elif days is not None:
|
||||
end_date = datetime.now().date()
|
||||
start_date = end_date - timedelta(days=days)
|
||||
elif start_date is not None:
|
||||
end_date = datetime.now().date()
|
||||
elif end_date is not None:
|
||||
start_date = end_date - timedelta(days=7)
|
||||
else:
|
||||
end_date = datetime.now().date()
|
||||
start_date = end_date - timedelta(days=7)
|
||||
|
||||
# 返回示例数据(与 except 块中相同)
|
||||
fallback_statistics = ConceptStatistics(
|
||||
hot_concepts=[
|
||||
ConceptStatItem(name="小米大模型", change_pct=12.45, stock_count=24, news_count=18),
|
||||
ConceptStatItem(name="人工智能", change_pct=8.76, stock_count=45, news_count=12),
|
||||
ConceptStatItem(name="新能源汽车", change_pct=6.54, stock_count=38, news_count=8),
|
||||
ConceptStatItem(name="芯片概念", change_pct=5.43, stock_count=52, news_count=15),
|
||||
ConceptStatItem(name="生物医药", change_pct=4.21, stock_count=28, news_count=6),
|
||||
],
|
||||
cold_concepts=[
|
||||
ConceptStatItem(name="房地产", change_pct=-5.76, stock_count=33, news_count=5),
|
||||
ConceptStatItem(name="煤炭开采", change_pct=-4.32, stock_count=25, news_count=3),
|
||||
ConceptStatItem(name="钢铁冶炼", change_pct=-3.21, stock_count=28, news_count=4),
|
||||
ConceptStatItem(name="传统零售", change_pct=-2.98, stock_count=19, news_count=2),
|
||||
ConceptStatItem(name="纺织服装", change_pct=-2.45, stock_count=15, news_count=2),
|
||||
],
|
||||
active_concepts=[
|
||||
ConceptStatItem(name="人工智能", news_count=45, report_count=15, total_mentions=60),
|
||||
ConceptStatItem(name="芯片概念", news_count=42, report_count=12, total_mentions=54),
|
||||
ConceptStatItem(name="新能源汽车", news_count=38, report_count=8, total_mentions=46),
|
||||
ConceptStatItem(name="生物医药", news_count=28, report_count=6, total_mentions=34),
|
||||
ConceptStatItem(name="量子科技", news_count=25, report_count=5, total_mentions=30),
|
||||
],
|
||||
volatile_concepts=[
|
||||
ConceptStatItem(name="区块链", volatility=25.6, avg_change=2.1, max_change=15.2),
|
||||
ConceptStatItem(name="元宇宙", volatility=23.8, avg_change=1.8, max_change=13.9),
|
||||
ConceptStatItem(name="虚拟现实", volatility=21.2, avg_change=-0.5, max_change=10.1),
|
||||
ConceptStatItem(name="游戏概念", volatility=19.7, avg_change=3.2, max_change=12.8),
|
||||
ConceptStatItem(name="在线教育", volatility=18.3, avg_change=-1.1, max_change=8.1),
|
||||
],
|
||||
momentum_concepts=[
|
||||
ConceptStatItem(name="数字经济", consecutive_days=6, total_change=19.2, avg_daily=3.2),
|
||||
ConceptStatItem(name="云计算", consecutive_days=5, total_change=16.8, avg_daily=3.36),
|
||||
ConceptStatItem(name="物联网", consecutive_days=4, total_change=13.1, avg_daily=3.28),
|
||||
ConceptStatItem(name="大数据", consecutive_days=4, total_change=12.4, avg_daily=3.1),
|
||||
ConceptStatItem(name="工业互联网", consecutive_days=3, total_change=9.6, avg_daily=3.2),
|
||||
],
|
||||
summary={
|
||||
'total_concepts': 500,
|
||||
'positive_count': 320,
|
||||
'negative_count': 180,
|
||||
'avg_change': 1.8,
|
||||
'update_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
||||
'date_range': f"{start_date} 至 {end_date}",
|
||||
'days': (end_date - start_date).days + 1,
|
||||
'start_date': str(start_date),
|
||||
'end_date': str(end_date)
|
||||
}
|
||||
)
|
||||
|
||||
return ConceptStatisticsResponse(
|
||||
success=True,
|
||||
data=fallback_statistics,
|
||||
params={
|
||||
'days': (end_date - start_date).days + 1,
|
||||
'min_stock_count': min_stock_count,
|
||||
'start_date': str(start_date),
|
||||
'end_date': str(end_date)
|
||||
},
|
||||
note="MySQL 连接不可用,使用示例数据"
|
||||
)
|
||||
|
||||
try:
|
||||
from datetime import datetime, timedelta
|
||||
import random
|
||||
|
||||
# 参数验证和日期范围计算
|
||||
|
||||
Reference in New Issue
Block a user