fix:批量修复了一些问题

This commit is contained in:
shangfangjian
2026-09-19 22:51:35 +08:00
parent 835d7217d0
commit 8e6ad5394e
44 changed files with 4921 additions and 395 deletions
+16 -3
View File
@@ -10,6 +10,8 @@ from fastapi.middleware.cors import CORSMiddleware
from src.core.config import settings
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
@@ -19,9 +21,11 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
ensure_admin_password_hashed,
migrate_plaintext_sensitive_settings,
)
from src.core.security_check import assert_security_on_startup
await init_db() # 验证连接,不建表
await migrate_plaintext_sensitive_settings() # 明文敏感配置 → 加密(幂等)
await ensure_admin_password_hashed() # .env 明文密码 → scrypt 哈希(幂等)
await assert_security_on_startup() # 启动安全校验(生产拒绝/开发警告)
yield
await close_client()
@@ -74,14 +78,23 @@ def create_app() -> FastAPI:
@app.get("/health/ready")
async def health_ready():
"""就绪检查: 验证数据库连接。"""
"""就绪检查:验证数据库连接。
数据库不可达时返回 HTTP 503,而非 200 + not_ready ——
这样 K8s/Compose 的 readinessProbe 才能正确判定「未就绪」并停止流量。
"""
from src.db.base import engine
from fastapi.responses import JSONResponse
try:
async with engine.begin() as conn:
await conn.run_sync(lambda conn: None)
return {"status": "ready"}
except Exception:
return {"status": "not_ready"}
except Exception as e:
logger.warning("就绪检查失败(数据库不可达): %s", e)
return JSONResponse(
status_code=503,
content={"status": "not_ready", "reason": "database_unreachable"},
)
return app
+6
View File
@@ -167,6 +167,12 @@ class _RateLimiter:
self._hits[key] = timestamps
return True
def remaining(self, key: str) -> int:
"""当前窗口内剩余可用次数。"""
now = time.time()
timestamps = [t for t in self._hits.get(key, []) if t > now - self.window_seconds]
return max(0, self.max_requests - len(timestamps))
# 全局限流实例: /api/v1/predict 每分钟 10 次
_predict_limiter = _RateLimiter(max_requests=10, window_seconds=60)
+122 -2
View File
@@ -7,7 +7,7 @@ from __future__ import annotations
import logging
import time
from datetime import date, datetime, timezone
from datetime import date, datetime, timedelta, timezone
from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel
@@ -28,7 +28,7 @@ from src.core.runtime_config import (
set_runtime_value,
)
from src.db.base import AsyncSession, get_db_read
from src.db.models import Injury, MatchStats
from src.db.models import Injury, Match, MatchStats
logger = logging.getLogger(__name__)
@@ -317,3 +317,123 @@ async def test_datasource(name: str):
"https://v3.football.api-sports.io/status",
headers={"x-apisports-key": api_key},
)
# ── 数据源健康/最近采集状态(只读,不触发采集) ──────────────────────
@router.get("/ingest/status")
async def ingest_status(db: AsyncSession = Depends(get_db_read)):
"""各数据源采集健康概览(只读,不触发任何采集)。
返回尽量可得的信息;基于现有表近似的数据会标明 approximation。
失败追踪目前依赖系统日志缓冲,无专用采集失败表。
"""
# ── bzzoiro: 落库目标是 matches 表,无专用采集时间戳 ──
# 近似:以 matches 表最大 match_date(已覆盖的最远比赛日) 与 created_at 作为参考
bzzoiro_key = await get_runtime_value("BZZOIRO_KEY")
bzzoiro_base = await get_runtime_value("BZZOIRO_BASE")
row = (
await db.execute(
select(
func.count().label("cnt"),
func.max(Match.match_date).label("latest_match_date"),
func.max(Match.created_at).label("latest_row_at"),
).where(Match.match_status == "finished")
)
).one()
bzzoiro = {
"name": "bzzoiro",
"label": "Bzzoiro",
"key_configured": bool(bzzoiro_key),
"base_url": (bzzoiro_base.rstrip("/") if bzzoiro_base else None) or settings.BZZOIRO_BASE,
"reachable": None, # 不主动探测
"last_success_at": row.latest_row_at.isoformat() if row.latest_row_at else None,
"latest_match_date": row.latest_match_date.isoformat() if row.latest_match_date else None,
"recent_count": row.cnt or 0,
"note": "approx:基于 matches.finished 表,last_success_at 为行写入时间而非精确采集完成时间",
"last_failure": _last_failure_log("bzzoiro"),
}
# ── understat: 落库到 match_stats(source=understat),有精确 retrieved_at ──
row = (
await db.execute(
select(
func.count().label("cnt"),
func.max(MatchStats.retrieved_at).label("latest_retrieved"),
).where(MatchStats.source == "understat")
)
).one()
understat = {
"name": "understat",
"label": "Understat",
"key_configured": True, # 无需 Key
"reachable": None,
"last_success_at": row.latest_retrieved.isoformat() if row.latest_retrieved else None,
"recent_count": row.cnt or 0,
"note": "基于 match_stats.source=understat 的 retrieved_at",
"last_failure": _last_failure_log("understat"),
}
# ── injuries: 落库到 injuries 表,有精确 retrieved_at;区分 Key/无数据/有数据 ──
api_key = await get_runtime_value("API_FOOTBALL_KEY")
row = (
await db.execute(
select(
func.count().label("cnt"),
func.max(Injury.retrieved_at).label("latest_retrieved"),
)
)
).one()
if not api_key:
injuries_status, injuries_note = "key_not_configured", "API_FOOTBALL_KEY 未配置"
elif not row.cnt:
injuries_status, injuries_note = "no_data", "本地无伤停数据,请先采集"
else:
injuries_status, injuries_note = "has_data", f"{row.cnt} 条伤停记录"
injuries = {
"name": "injuries",
"label": "Injuries (API-Football)",
"key_configured": bool(api_key),
"reachable": None,
"status": injuries_status,
"last_success_at": row.latest_retrieved.isoformat() if row.latest_retrieved else None,
"recent_count": row.cnt or 0,
"note": injuries_note,
"last_failure": _last_failure_log("injuries"),
}
return {"sources": [bzzoiro, understat, injuries]}
def _last_failure_log(source: str) -> dict | None:
"""从系统日志缓冲中查找某数据源的最近一次错误(仅作参考,非专用失败表)。"""
entries = get_entries(min_level="ERROR", keyword=source, limit=5)
if not entries:
return None
e = entries[0]
return {
"at": datetime.fromtimestamp(e["ts"]).isoformat(),
"logger": e["logger"],
"detail": e["message"][:200],
"note": "approx:来自内存日志缓冲,非专用采集失败表;进程重启后清零",
}
@router.get("/stats")
async def admin_stats(db: AsyncSession = Depends(get_db_read)):
"""管理区统计(只读):最近预测次数。轻量聚合,无 LLM 调用。"""
from sqlalchemy import func, text
from src.db.models import Prediction
day_ago = datetime.now(timezone.utc) - timedelta(days=1)
week_ago = datetime.now(timezone.utc) - timedelta(days=7)
r = (
await db.execute(
select(
func.count().label("total"),
func.count().filter(Prediction.created_at >= day_ago).label("last_24h"),
func.count().filter(Prediction.created_at >= week_ago).label("last_7d"),
)
)
).one()
return {"predictions": {"total": r.total, "last_24h": r.last_24h, "last_7d": r.last_7d}}
+9 -5
View File
@@ -28,13 +28,16 @@ async def backtest(req: BacktestRequest):
"""对历史比赛运行回测。
该接口会对每场已完赛比赛各发起一次 LLM 预测,成本高 —— 因此需要
`X-API-Key` 鉴权(见审查报告 P2-7)。
管理员鉴权(require_admin)。
对每场已完赛比赛:
1. 用比赛之前的数据构建上下文 (防未来信息泄漏)
2. 调 LLM 预测
3. 用实际比分回填
1. 用比赛之前的数据构建上下文 (防未来信息泄漏,cutoff=match_date-1天)
2. 调 LLM 预测(强制 use_cache=False,避免缓存命中导致反复 settle 同一行)
3. 用实际比分回填(settle)
4. 统计准确率 / RMSE / 校准度
限流:单请求上限 200 场(默认 20),避免一次打爆 LLM 额度。
回测写入 run_type='backtest',与实盘(live)互不覆盖(唯一键含 run_type)。
"""
try:
summary = await run_backtest(
@@ -53,10 +56,11 @@ async def backtest(req: BacktestRequest):
"summary": {
"total": summary.total,
"scored": summary.scored,
"success": summary.success,
"degraded": summary.degraded,
"accuracy_1x2": summary.accuracy_1x2,
"avg_score_rmse": summary.avg_score_rmse,
"avg_subjective_confidence": summary.avg_subjective_confidence,
"calibration": summary.calibration,
},
"results": [
{
+8 -2
View File
@@ -19,13 +19,19 @@ router = APIRouter(prefix="/api/v1", tags=["eval"])
async def settle(req: SettleRequest, db: AsyncSession = Depends(get_db)):
"""回填实际结果。
status 为 degraded/failed 的预测无法结算
status 为 degraded/failed 的预测无法结算(返回 400);
记录不存在返回 404。
"""
try:
pred = await settle_prediction(req.prediction_id, req.home_goals, req.away_goals)
return {"id": pred.id, "settled": pred.settled}
except ValueError as e:
logger.warning("settle failed: %s", e)
msg = str(e)
# degraded/failed 拒绝:明确的 400,而非与"未找到"混为一谈
if "无法结算" in msg:
logger.warning("settle rejected: %s", msg)
raise HTTPException(400, msg)
logger.warning("settle failed: %s", msg)
raise HTTPException(404, "预测记录不存在")
except Exception as e:
logger.exception("settle error")
+1
View File
@@ -73,6 +73,7 @@ async def _run_bzzoiro(leagues: list[str], date_from: str | None, date_to: str |
logger.warning("bzzoiro 部分联赛存在错误: %s", sample)
if merged["errors"]:
logger.warning("bzzoiro 采集错误 %d 条: %s", len(merged["errors"]), merged["errors"][:3])
logger.debug("bzzoiro 采集明细: leagues=%s", list(merged["leagues"].keys()))
except Exception:
logger.exception("bzzoiro 采集任务失败")
+108 -4
View File
@@ -4,13 +4,13 @@ from __future__ import annotations
from datetime import datetime, timedelta
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy import select
from sqlalchemy import or_, select
from sqlalchemy.orm import selectinload
from src.api.deps import require_admin
from src.api.schemas import MatchListOut, MatchOut
from src.api.schemas import MatchListOut, MatchOut, PredictionOut
from src.db.base import AsyncSession, get_db_read
from src.db.models import League, Match
from src.db.models import League, Match, Prediction
router = APIRouter(prefix="/api/v1", tags=["data"])
@@ -114,12 +114,26 @@ async def list_matches(
async def get_match(match_id: int, db: AsyncSession = Depends(get_db_read)):
stmt = (
select(Match)
.options(selectinload(Match.league), selectinload(Match.home_team), selectinload(Match.away_team))
.options(
selectinload(Match.league),
selectinload(Match.home_team),
selectinload(Match.away_team),
selectinload(Match.stats),
)
.where(Match.id == match_id)
)
m = (await db.execute(stmt)).scalar_one_or_none()
if m is None:
raise HTTPException(404, "match not found")
# 最近预测(倒序,最多 5 条)——复用 PredictionOut 结构,只读,不触发 LLM
preds = (
await db.execute(
select(Prediction)
.where(Prediction.match_id == match_id)
.order_by(Prediction.created_at.desc())
.limit(5)
)
).scalars().all()
return MatchOut(
id=m.id,
league_code=m.league.code if m.league else None,
@@ -135,4 +149,94 @@ async def get_match(match_id: int, db: AsyncSession = Depends(get_db_read)):
match_stage=m.match_stage,
home_xg=m.stats.home_xg if m.stats else None,
away_xg=m.stats.away_xg if m.stats else None,
recent_predictions=[
PredictionOut(
id=p.id, match_id=p.match_id, provider=p.provider, model=p.model,
prompt_version=p.prompt_version, mode=p.mode or "single",
pred_home_goals=p.pred_home_goals, pred_away_goals=p.pred_away_goals,
alt_pred_home_goals=p.alt_pred_home_goals, alt_pred_away_goals=p.alt_pred_away_goals,
pred_1x2=p.pred_1x2, subjective_confidence=p.subjective_confidence,
reasoning=p.reasoning, status=p.status or "success",
agent_outputs=p.agent_outputs, agent_weights=p.agent_weights,
created_at=p.created_at, actual_home_goals=p.actual_home_goals,
actual_away_goals=p.actual_away_goals, settled=p.settled,
)
for p in preds
],
)
@router.get("/matches/{match_id}/context", dependencies=[Depends(require_admin)])
async def match_context(match_id: int, db: AsyncSession = Depends(get_db_read)):
"""比赛上下文(只读,不触发 LLM):双方近况 + 历史交锋。
全部基于现有数据聚合:
- recent_home / recent客队:该队最近 5 场已完赛(进球/结果)
- h2h:双方最近 5 次交手
若数据不足,对应列表为空(前端展示空态)。
"""
m = (
await db.execute(
select(Match)
.options(selectinload(Match.home_team), selectinload(Match.away_team))
.where(Match.id == match_id)
)
).scalar_one_or_none()
if m is None:
raise HTTPException(404, "match not found")
home_id = m.home_team_id
away_id = m.away_team_id
def _row_to_dict(row):
return {
"match_date": row.match_date.isoformat() if row.match_date else None,
"home_team": row.home_team.name_zh or row.home_team.name if row.home_team else None,
"away_team": row.away_team.name_zh or row.away_team.name if row.away_team else None,
"home_goals": row.home_goals,
"away_goals": row.away_goals,
}
# 主队近况(已完赛,含主/客场)
home_recent = (
await db.execute(
select(Match)
.where(Match.match_status == "finished", Match.home_team_id == home_id)
.order_by(Match.match_date.desc())
.limit(5)
.options(selectinload(Match.home_team), selectinload(Match.away_team))
)
).scalars().all()
# 客队近况
away_recent = (
await db.execute(
select(Match)
.where(Match.match_status == "finished", Match.away_team_id == away_id)
.order_by(Match.match_date.desc())
.limit(5)
.options(selectinload(Match.home_team), selectinload(Match.away_team))
)
).scalars().all()
# 历史交锋(双方已完赛)
h2h = (
await db.execute(
select(Match)
.where(
Match.match_status == "finished",
or_(
(Match.home_team_id == home_id) & (Match.away_team_id == away_id),
(Match.home_team_id == away_id) & (Match.away_team_id == home_id),
),
)
.order_by(Match.match_date.desc())
.limit(5)
.options(selectinload(Match.home_team), selectinload(Match.away_team))
)
).scalars().all()
return {
"home_recent": [_row_to_dict(r) for r in home_recent],
"away_recent": [_row_to_dict(r) for r in away_recent],
"h2h": [_row_to_dict(r) for r in h2h],
}
+71 -18
View File
@@ -42,7 +42,7 @@ async def predict(req: PredictRequest):
if m.match_status == "finished":
raise HTTPException(400, "该比赛已完赛,不再支持预测")
# 2. LLM 调用(不持有任何 DB 连接)
# 2. 预测调用(不持有任何 DB 连接)
try:
result = await predict_match(
req.match_id,
@@ -63,32 +63,77 @@ async def predict(req: PredictRequest):
logger.exception("predict unexpected error")
raise HTTPException(500, "预测失败,请查看服务器日志")
# baseline 模式:结果已是 dict,需独立落库(prediction_id)
if req.mode == "baseline":
prediction_id = await _persist_baseline(req.match_id, result)
else:
prediction_id = result.prediction_id
# 3. 结果映射(无 DB 访问)
logger.info(
"预测完成 match=%s mode=%s pred=%s:%s (%s)",
req.match_id, req.mode, result.pred_home_goals, result.pred_away_goals, result.pred_1x2,
req.match_id, req.mode,
result.get("pred_home_goals") if isinstance(result, dict) else result.pred_home_goals,
result.get("pred_away_goals") if isinstance(result, dict) else result.pred_away_goals,
result.get("pred_1x2") if isinstance(result, dict) else result.pred_1x2,
)
result_dict = result if isinstance(result, dict) else None
return PredictOut(
prediction_id=result.prediction_id,
provider=result.provider,
model=result.model,
prompt_version=getattr(result, "prompt_version", None),
mode=getattr(result, "mode", "single"),
pred_home_goals=result.pred_home_goals,
pred_away_goals=result.pred_away_goals,
alt_pred_home_goals=result.alt_pred_home_goals,
alt_pred_away_goals=result.alt_pred_away_goals,
pred_1x2=result.pred_1x2,
subjective_confidence=result.subjective_confidence,
reasoning=result.reasoning,
agent_outputs=getattr(result, "agent_outputs", None),
agent_weights=getattr(result, "agent_weights", None),
context=result.context,
latency_ms=result.latency_ms,
prediction_id=prediction_id,
provider=result.get("provider") if result_dict else result.provider,
model=result.get("model") if result_dict else result.model,
prompt_version=result.get("prompt_version") if result_dict else getattr(result, "prompt_version", None),
mode=req.mode,
pred_home_goals=result.get("pred_home_goals") if result_dict else result.pred_home_goals,
pred_away_goals=result.get("pred_away_goals") if result_dict else result.pred_away_goals,
alt_pred_home_goals=result.get("alt_pred_home_goals") if result_dict else result.alt_pred_home_goals,
alt_pred_away_goals=result.get("alt_pred_away_goals") if result_dict else result.alt_pred_away_goals,
pred_1x2=result.get("pred_1x2") if result_dict else result.pred_1x2,
subjective_confidence=result.get("subjective_confidence") if result_dict else result.subjective_confidence,
reasoning=result.get("reasoning") if result_dict else result.reasoning,
status=result.get("status", "success") if result_dict else getattr(result, "status", "success"),
agent_outputs=result.get("agent_outputs") if result_dict else getattr(result, "agent_outputs", None),
agent_weights=result.get("agent_weights") if result_dict else getattr(result, "agent_weights", None),
context=result.get("context", "") if result_dict else result.context,
latency_ms=result.get("latency_ms", 0) if result_dict else result.latency_ms,
prompt_tokens=result.get("prompt_tokens") if result_dict else getattr(result, "prompt_tokens", None),
completion_tokens=result.get("completion_tokens") if result_dict else getattr(result, "completion_tokens", None),
rate_limit_remaining=_predict_limiter.remaining(get_client_ip(request)),
)
async def _persist_baseline(match_id: int, baseline: dict) -> int:
"""将基线预测结果写入 prediction 表,复用 upsert 语义。"""
from src.db.unit_of_work import get_uow
from src.llm.predict import _upsert_prediction
async with get_uow() as session:
pred = await _upsert_prediction(
session,
match_id=match_id,
provider_name="baseline",
model="baseline",
mode="baseline",
run_type="baseline",
values={
"prompt_version": "baseline_v1",
"prompt_tokens": 0,
"completion_tokens": 0,
"latency_ms": 0,
"pred_home_goals": baseline["pred_home_goals"],
"pred_away_goals": baseline["pred_away_goals"],
"pred_1x2": baseline["pred_1x2"],
"subjective_confidence": baseline["subjective_confidence"],
"reasoning": baseline["reasoning"],
"raw_response": baseline.get("raw", baseline),
"status": "success",
},
)
return pred.id
@router.get("/predictions", response_model=list[PredictionOut], dependencies=[Depends(require_admin)])
async def list_predictions(
match_id: int | None = None,
@@ -110,10 +155,14 @@ async def list_predictions(
mode=p.mode or "single",
pred_home_goals=p.pred_home_goals,
pred_away_goals=p.pred_away_goals,
alt_pred_home_goals=p.alt_pred_home_goals,
alt_pred_away_goals=p.alt_pred_away_goals,
pred_1x2=p.pred_1x2,
subjective_confidence=p.subjective_confidence,
reasoning=p.reasoning,
status=p.status or "success",
agent_outputs=p.agent_outputs,
agent_weights=p.agent_weights,
created_at=p.created_at,
actual_home_goals=p.actual_home_goals,
actual_away_goals=p.actual_away_goals,
@@ -137,10 +186,14 @@ async def get_prediction(prediction_id: int, db: AsyncSession = Depends(get_db_r
mode=p.mode or "single",
pred_home_goals=p.pred_home_goals,
pred_away_goals=p.pred_away_goals,
alt_pred_home_goals=p.alt_pred_home_goals,
alt_pred_away_goals=p.alt_pred_away_goals,
pred_1x2=p.pred_1x2,
subjective_confidence=p.subjective_confidence,
reasoning=p.reasoning,
status=p.status or "success",
agent_outputs=p.agent_outputs,
agent_weights=p.agent_weights,
created_at=p.created_at,
actual_home_goals=p.actual_home_goals,
actual_away_goals=p.actual_away_goals,
+18 -1
View File
@@ -29,6 +29,8 @@ class MatchOut(BaseModel):
match_stage: str | None
home_xg: float | None = None
away_xg: float | None = None
# 该场比赛的最近预测摘要(按时间倒序,最多 5 条;无预测为空)
recent_predictions: list[PredictionOut] = []
class MatchListOut(BaseModel):
@@ -42,7 +44,13 @@ class PredictRequest(BaseModel):
provider: str | None = None
model: str | None = None
prompt_version: str | None = None
mode: str = "multi" # multi(默认, 5专家+终裁) | single(单次调用)
mode: str = Field(
"multi",
description="multi(默认,5专家+终裁) | single(单次) | baseline(极简统计基线,不调用 LLM)",
)
use_cache: bool = True
backtest: bool = False
cutoff_at: str | None = Field(None, description="显式截止时间 ISO8601,用于回测防未来信息")
class PredictOut(BaseModel):
@@ -58,6 +66,13 @@ class PredictOut(BaseModel):
pred_1x2: str | None
subjective_confidence: float | None
reasoning: str | None
status: str = "success"
# 成本信息(可选;单次/多专家均有)
latency_ms: int | None = None
prompt_tokens: int | None = None
completion_tokens: int | None = None
# 限流提示:当请求被节流时告知用户剩余配额(可选)
rate_limit_remaining: int | None = None
agent_outputs: list[dict] | None = None
agent_weights: dict | None = None
context: str
@@ -78,7 +93,9 @@ class PredictionOut(BaseModel):
pred_1x2: str | None
subjective_confidence: float | None
reasoning: str | None
status: str = "success"
agent_outputs: list[dict] | None = None
agent_weights: dict | None = None
created_at: datetime
actual_home_goals: int | None
actual_away_goals: int | None
+121
View File
@@ -0,0 +1,121 @@
"""生产环境启动安全校验:缺失关键配置则拒绝启动(生产)或警告(开发)。
校验项:
- SECRET_KEY 非空且非弱默认值
- 鉴权已配置(密码哈希 / .env 明文密码 / API Key 任一)
- DATABASE_URL 不使用示例弱密码(football:football)
与 deps.py 的 fail-closed 互补:此处是「启动时一次性校验 + 明确报错」,
避免生产带着危险配置上线却只在被攻击时才暴露。
"""
from __future__ import annotations
import logging
import sys
from src.core.config import settings
from src.core.runtime_config import get_admin_password_hash
logger = logging.getLogger(__name__)
# 明显的弱 SECRET_KEY 黑名单(大小写无关)
_WEAK_SECRET_KEYS = {
"", "changeme", "secret", "password", "123456", "admin",
"default", "dev", "development", "test", "example",
"openssl rand -base64 32", # 有人把生成指令直接粘进去
}
_MIN_SECRET_KEY_LEN = 16
# 示例弱数据库密码(仅识别最明显的;自定义强密码不受影响)
_WEAK_DB_PATTERNS = ("football:football@", "admin:admin@", "password@", "123456@")
class SecurityCheckError(Exception):
"""生产环境安全校验失败。"""
async def _auth_configured() -> bool:
"""运行时鉴权是否已配置(含数据库密码哈希/.env 明文/API Key)。"""
if await get_admin_password_hash():
return True
if settings.ADMIN_PASSWORD or settings.ADMIN_API_KEY:
return True
return False
def _check_secret_key() -> list[str]:
"""返回 SECRET_KEY 的问题列表(空=通过)。"""
problems: list[str] = []
key = settings.SECRET_KEY
if not key:
problems.append("SECRET_KEY 未设置,加密与会话签名无法保障")
return problems
if key.lower().strip() in _WEAK_SECRET_KEYS:
problems.append(f"SECRET_KEY 为弱默认值({key[:20]}...),请生成强随机值: openssl rand -base64 32")
elif len(key) < _MIN_SECRET_KEY_LEN:
problems.append(f"SECRET_KEY 过短({len(key)} 字符),建议至少 {_MIN_SECRET_KEY_LEN}")
return problems
def _check_database_url() -> list[str]:
problems: list[str] = []
url = settings.DATABASE_URL.lower()
for pat in _WEAK_DB_PATTERNS:
if pat in url:
problems.append(f"DATABASE_URL 使用示例弱密码({pat.rstrip('@')}),生产环境必须更换")
break
return problems
async def validate_security() -> dict:
"""执行安全校验。
返回 {"ok": bool, "errors": [...], "warnings": [...]}。
errors 为阻断性问题,warnings 为建议。
"""
errors: list[str] = []
warnings: list[str] = []
errors.extend(_check_secret_key())
if not await _auth_configured():
errors.append("管理鉴权未配置:请设置 ADMIN_PASSWORD 或 ADMIN_API_KEY")
warnings.extend(_check_database_url())
# 生产环境:DB 弱密码也升级为阻断
if settings.APP_ENV == "production" and warnings:
errors.extend(warnings)
warnings = []
ok = not errors
return {"ok": ok, "errors": errors, "warnings": warnings}
async def assert_security_on_startup() -> None:
"""启动入口:生产环境校验失败则拒绝启动,开发环境仅警告。"""
result = await validate_security()
for w in result["warnings"]:
logger.warning("[security-check] %s", w)
if result["ok"]:
if result["warnings"]:
logger.warning("[security-check] 存在 %d 项警告,建议修复", len(result["warnings"]))
else:
logger.info("[security-check] 安全校验通过")
return
# 阻断
is_prod = settings.APP_ENV == "production"
level = logging.ERROR if is_prod else logging.WARNING
for e in result["errors"]:
logger.log(level, "[security-check] %s", e)
if is_prod:
logger.critical(
"[security-check] 生产环境安全校验失败,拒绝启动。请修复上述 %d 项问题后重试。",
len(result["errors"]),
)
# 明确退出,避免带着危险配置上线
sys.exit(1)
logger.warning("[security-check] 开发环境存在 %d 项问题(未阻断),请尽快修复", len(result["errors"]))
+1 -1
View File
@@ -35,7 +35,7 @@ def load_agent_prompt(name: str, version: str = "v1") -> str:
@dataclass
class AgentSpec:
"""领域专家 agent 定义。"""
name: str # h2h / form / standings / injuries / xg
name: str # h2h / form / home_away / injuries / stats
system_prompt: str # system message
slice_fn: object # async (header, before) -> str 切片函数
+21 -5
View File
@@ -97,9 +97,12 @@ class MultiPredictResult:
reasoning: str | None
agent_outputs: list[dict]
agent_weights: dict | None
status: str = "success"
context: str
latency_ms: int | None
raw: dict | None
latency_ms: int | None = None
prompt_tokens: int | None = None
completion_tokens: int | None = None
raw: dict | None = None
async def _agent_provider(agent_id: str, *, tier: str) -> LLMProvider:
@@ -158,7 +161,10 @@ async def run_specialists(
reports: list[AgentReport] = []
for spec, r in zip(SPECIALIST_SPECS, results):
if isinstance(r, Exception):
logger.warning("agent %s raised: %s", spec.name, r)
logger.warning(
"专家调用失败 match=%s agent=%s error=%s",
header.match_id, spec.name, str(r)[:120],
)
reports.append(AgentReport(agent=spec.name, status="error", analysis=str(r)[:200]))
else:
reports.append(r)
@@ -256,8 +262,8 @@ async def predict_match_multi(
else:
# 所有专家无数据/均失败:跳过终裁,标记 degraded
logger.warning(
"match %s: 所有 %d 位专家均无有效数据,跳过终裁,标记 degraded",
match_id, len(reports),
"预测降级 match=%s mode=%s status=degraded experts=%d/%d 均无有效数据",
match_id, "multi", ok_reports, len(reports),
)
# 无有效专家时不调用 aggregator provider,避免多余开销
# model 使用 settings 默认值占位(无实际 LLM 调用)
@@ -337,6 +343,13 @@ async def predict_match_multi(
},
)
logger.info(
"预测完成 match=%s mode=%s status=%s pred=%s:%s (%s) latency=%sms, experts=%d/%d, prediction_id=%s",
match_id, "multi", pred_status,
pred.pred_home_goals, pred.pred_away_goals, pred.pred_1x2,
latency_ms, ok_reports, len(reports), pred.id,
)
return MultiPredictResult(
prediction_id=pred.id,
provider=pred.provider,
@@ -350,9 +363,12 @@ async def predict_match_multi(
pred_1x2=pred.pred_1x2,
subjective_confidence=pred.subjective_confidence,
reasoning=pred.reasoning,
status=pred_status,
agent_outputs=pred.agent_outputs,
agent_weights=agent_weights,
context=_reports_to_json(reports),
latency_ms=latency_ms,
prompt_tokens=pred.prompt_tokens,
completion_tokens=pred.completion_tokens,
raw=final,
)
+13
View File
@@ -70,6 +70,8 @@ class BacktestSummary:
"""回测汇总统计。"""
total: int
scored: int
success: int = 0 # status=success 的预测数(有完整比分+1x2)
degraded: int = 0 # status=degraded 的预测数(专家失败/无有效数据)
accuracy_1x2: float | None = None
avg_score_rmse: float | None = None
avg_subjective_confidence: float | None = None
@@ -194,6 +196,17 @@ async def run_backtest(
if r is not None:
summary.results.append(r)
summary.scored += 1
# success:有完整预测比分+1x2;degraded:多专家模式无有效结论
if r.pred_1x2 is not None and r.pred_home is not None and r.pred_away is not None:
summary.success += 1
else:
summary.degraded += 1
logger.info(
"回测汇总 mode=%s total=%d scored=%d success=%d accuracy=%s%%",
mode, summary.total, summary.scored, summary.success,
f"{(sum(1 for r in summary.results if r.correct_1x2) / summary.scored * 100):.1f}" if summary.scored else "n/a",
)
# 汇总统计
if summary.scored > 0:
+113
View File
@@ -0,0 +1,113 @@
"""极简基线预测:主客场场均进球估计(不调用 LLM,不产生费用)。
用于与 LLM 预测做 eval 对比。这是最简单的统计基线,仅供研究参考,
文档与 reasoning 均明确标注「非投注建议」。
"""
from __future__ import annotations
import logging
from datetime import datetime
from sqlalchemy import case, func, select
from src.db.base import AsyncSession, AsyncSessionLocal
from src.db.models import Match
logger = logging.getLogger(__name__)
async def _avg_goals(
db: AsyncSession,
*,
team_id: int,
side: str,
league_id: int,
before: datetime | None,
) -> float:
"""某队在该联赛已完赛场次的场均进球(side=home/away)。"""
if side == "home":
goals_col = Match.home_goals
team_col = Match.home_team_id
else:
goals_col = Match.away_goals
team_col = Match.away_team_id
stmt = (
select(func.avg(goals_col).label("avg_goals"), func.count().label("cnt"))
.where(
Match.match_status == "finished",
team_col == team_id,
Match.league_id == league_id,
goals_col.is_not(None),
)
)
if before is not None:
stmt = stmt.where(Match.match_date < before)
row = (await db.execute(stmt)).one()
return float(row.avg_goals) if row.avg_goals is not None and row.cnt > 0 else 0.0
async def predict_baseline(
match_id: int,
*,
backtest: bool = False,
cutoff_at: datetime | None = None,
) -> dict:
"""极简基线预测:主场场均进球 vs 客场场均进球。
返回与 PredictResult 兼容的字典:
provider=model="baseline", 不调用 LLM,latency_ms≈0。
"""
async with AsyncSessionLocal() as db:
match = await db.get(Match, match_id)
if match is None:
raise ValueError(f"match {match_id} not found")
before = None
if backtest and match.match_dt:
from datetime import timedelta
before = match.match_dt - timedelta(days=1)
elif cutoff_at is not None:
before = cutoff_at
home_avg = await _avg_goals(
db, team_id=match.home_team_id, side="home",
league_id=match.league_id, before=before,
)
away_avg = await _avg_goals(
db, team_id=match.away_team_id, side="away",
league_id=match.league_id, before=before,
)
pred_home = max(0, min(10, round(home_avg)))
pred_away = max(0, min(10, round(away_avg)))
# 主场轻微加成(可选,这里保持极简不额外加权)
if pred_home > pred_away:
pred_1x2 = "1"
elif pred_home < pred_away:
pred_1x2 = "2"
else:
pred_1x2 = "X"
return {
"pred_home_goals": float(pred_home),
"pred_away_goals": float(pred_away),
"alt_pred_home_goals": None,
"alt_pred_away_goals": None,
"pred_1x2": pred_1x2,
"subjective_confidence": 0.5,
"prompt_tokens": 0,
"completion_tokens": 0,
"reasoning": (
f"基线估计(非投注建议): 主队主场场均进球 {home_avg:.2f} → 预测 {pred_home}; "
f"客队客场场均进球 {away_avg:.2f} → 预测 {pred_away}"
),
"provider": "baseline",
"model": "baseline",
"prompt_version": "baseline_v1",
"mode": "baseline",
"status": "success",
"latency_ms": 0,
"raw": {"home_avg": round(home_avg, 2), "away_avg": round(away_avg, 2)},
}
+43 -9
View File
@@ -25,6 +25,10 @@ async def settle_prediction(prediction_id: int, home_goals: int, away_goals: int
pred.actual_home_goals = home_goals
pred.actual_away_goals = away_goals
pred.settled = True
logger.info(
"结算完成 prediction_id=%s match=%s actual=%s:%s mode=%s",
prediction_id, pred.match_id, home_goals, away_goals, pred.mode or "single",
)
return pred
@@ -109,8 +113,14 @@ async def get_eval_summary(
rows = list((await session.execute(stmt)).scalars().all())
from collections import defaultdict
buckets: dict[tuple[str, str], dict] = defaultdict(lambda: {
buckets: dict[tuple[str, str, str], dict] = defaultdict(lambda: {
"total": 0, "correct_1x2": 0, "score_errors": [], "conf_sum": 0.0, "conf_count": 0,
# 置信度校准分桶(仅 settled 且 pred 完整者计入)
"conf_buckets": {
"low(0-0.5)": {"total": 0, "correct": 0},
"medium(0.5-0.7)": {"total": 0, "correct": 0},
"high(0.7-1)": {"total": 0, "correct": 0},
},
})
evaluated = 0
skipped_incomplete = 0
@@ -118,35 +128,59 @@ async def get_eval_summary(
if (p.pred_home_goals is None or p.pred_away_goals is None or p.pred_1x2 is None):
skipped_incomplete += 1
continue
key = (p.provider, p.model)
key = (p.provider, p.model, p.prompt_version or "")
b = buckets[key]
b["total"] += 1
evaluated += 1
if p.actual_home_goals is None or p.actual_away_goals is None:
continue
actual = _actual_1x2(p.actual_home_goals, p.actual_away_goals)
if p.pred_1x2 == actual:
b["correct_1x2"] += 1
if p.pred_home_goals is not None and p.pred_away_goals is not None:
correct = False
if p.actual_home_goals is not None and p.actual_away_goals is not None:
actual = _actual_1x2(p.actual_home_goals, p.actual_away_goals)
if p.pred_1x2 == actual:
b["correct_1x2"] += 1
correct = True
if (
p.pred_home_goals is not None and p.pred_away_goals is not None
and p.actual_home_goals is not None and p.actual_away_goals is not None
):
err = ((p.pred_home_goals - p.actual_home_goals) ** 2 +
(p.pred_away_goals - p.actual_away_goals) ** 2) ** 0.5
b["score_errors"].append(err)
if p.subjective_confidence is not None:
b["conf_sum"] += p.subjective_confidence
b["conf_count"] += 1
# 仅当有实际结果可用于校准时,才落入置信度分桶
if p.actual_home_goals is not None and p.actual_away_goals is not None:
conf = p.subjective_confidence
if conf < 0.5:
bucket = "low(0-0.5)"
elif conf < 0.7:
bucket = "medium(0.5-0.7)"
else:
bucket = "high(0.7-1)"
b["conf_buckets"][bucket]["total"] += 1
if correct:
b["conf_buckets"][bucket]["correct"] += 1
summary = []
for (prov, model), b in sorted(buckets.items()):
for (prov, model, ver), b in sorted(buckets.items()):
acc = (b["correct_1x2"] / b["total"] * 100) if b["total"] else 0
avg_err = (sum(b["score_errors"]) / len(b["score_errors"])) if b["score_errors"] else None
avg_conf = (b["conf_sum"] / b["conf_count"]) if b["conf_count"] else None
# 校准分桶 → 命中率
calibration = {}
for name, cb in b["conf_buckets"].items():
hit_rate = round(cb["correct"] / cb["total"] * 100, 1) if cb["total"] else None
calibration[name] = {"total": cb["total"], "hit_rate": hit_rate}
summary.append({
"provider": prov,
"model": model,
"prompt_version": ver or None,
"total": b["total"],
"accuracy_1x2": round(acc, 1),
"avg_score_rmse": round(avg_err, 2) if avg_err is not None else None,
"avg_subjective_confidence": round(avg_conf, 2) if avg_conf is not None else None,
"calibration": calibration,
})
return {
"summary": summary,
+21 -6
View File
@@ -101,8 +101,9 @@ class PredictResult:
subjective_confidence: float | None
reasoning: str | None
context: str
latency_ms: int | None
raw: dict | None
status: str = "success"
latency_ms: int | None = None
raw: dict | None = None
async def _upsert_prediction(
@@ -158,15 +159,22 @@ async def predict_match(
backtest: bool = False,
cutoff_at=None,
) -> "PredictResult | MultiPredictResult":
"""预测入口。mode=multi(默认)走多 agent;mode=single 走单次调用。
"""预测入口。mode=multi(默认)走多 agent;mode=single 走单次调用;mode=baseline 走无 LLM 基线
Args:
use_cache: 是否允许返回进程内缓存结果。回测必须传 False——
mode: multi(默认,5 专家+终裁) / single(单次) / baseline(极简统计基线,不调用 LLM)。
use_cache:是否允许返回进程内缓存结果。回测必须传 False——
缓存命中不会新建 prediction 行,调用方会对同一个 prediction_id
反复 settle,把不同比赛的真实比分覆盖到同一条记录上。
backtest: 是否回测模式。True 时 cutoff 自动设为 match_date-1天。
cutoff_at: 显式截止时间,优先级高于 backtest 自动计算。
backtest:是否回测模式。True 时 cutoff 自动设为 match_date-1天。
cutoff_at:显式截止时间,优先级高于 backtest 自动计算。
"""
if mode == "baseline":
from src.llm.baseline import predict_baseline
return await predict_baseline(
match_id, backtest=backtest, cutoff_at=cutoff_at,
)
if mode == "single":
return await _predict_single(
match_id,
@@ -300,6 +308,7 @@ async def _predict_single(
pred_1x2=pred.pred_1x2,
subjective_confidence=pred.subjective_confidence,
reasoning=pred.reasoning,
status=pred.status,
context=ctx.text,
latency_ms=resp.latency_ms,
raw=resp.raw,
@@ -308,4 +317,10 @@ async def _predict_single(
# 5. 写入缓存(仅当允许缓存时)
if use_cache:
_set_cached(match_id, settings.LLM_PROVIDER, provider.model, version, tpl_hash, result)
logger.info(
"预测完成 match=%s mode=%s status=%s pred=%s:%s (%s) latency=%sms",
match_id, "single", "success",
validated.pred_home_goals, validated.pred_away_goals, validated.pred_1x2,
resp.latency_ms,
)
return result
-1
View File
@@ -6,7 +6,6 @@
1. 主客队近期状态差异
2. 主客场因素
3. 历史交锋心理优势
4. 联赛排名差距
严格按此 JSON 输出,不要其他内容:
```json