fix: feed today-only data to AI daily summary to reduce hallucination
Shrink summary context and prompts to today's trades and positions only, and tighten anti-fabrication rules. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -8,6 +8,8 @@ from hub_ai.context import (
|
||||
build_daily_context,
|
||||
collect_closed_trades_snapshot,
|
||||
format_account_remark,
|
||||
format_summary_context_text,
|
||||
summary_context_hash,
|
||||
)
|
||||
from hub_ai.prompts import SUMMARY_SYSTEM, build_summary_user_prompt
|
||||
from hub_ai.store import append_summary, get_latest_summary, list_summaries
|
||||
@@ -15,13 +17,10 @@ from hub_ai.store import append_summary, get_latest_summary, list_summaries
|
||||
|
||||
def _stats_snapshot_from_ctx(ctx: dict) -> dict:
|
||||
day = ctx.get("trading_day")
|
||||
prev = ctx.get("prev_trading_day")
|
||||
accounts = ctx.get("accounts") or []
|
||||
return {
|
||||
"totals": ctx.get("totals"),
|
||||
"prev_trading_day": prev,
|
||||
"fund_history": ctx.get("fund_history"),
|
||||
"closed_trades": collect_closed_trades_snapshot(accounts, today=day, yesterday=prev),
|
||||
"closed_trades": collect_closed_trades_snapshot(accounts, today=day),
|
||||
"by_account": {
|
||||
str(ac.get("key") or ac.get("id")): {
|
||||
"key": ac.get("key"),
|
||||
@@ -31,9 +30,7 @@ def _stats_snapshot_from_ctx(ctx: dict) -> dict:
|
||||
"trading_usdt": ac.get("trading_usdt"),
|
||||
"available_trading_usdt": ac.get("available_trading_usdt"),
|
||||
"pnl_u": (ac.get("trade_stats") or {}).get("total_pnl_u"),
|
||||
"pnl_u_yesterday": (ac.get("trade_stats_yesterday") or {}).get("total_pnl_u"),
|
||||
"closed_count": (ac.get("trade_stats") or {}).get("closed_count"),
|
||||
"closed_count_yesterday": (ac.get("trade_stats_yesterday") or {}).get("closed_count"),
|
||||
"float_pnl_u": ac.get("float_pnl_u"),
|
||||
"remark": format_account_remark(ac),
|
||||
"monitor_lines": ac.get("monitor_lines") or {},
|
||||
@@ -52,9 +49,16 @@ def generate_daily_summary(
|
||||
) -> dict[str, Any]:
|
||||
ctx = build_daily_context(exchanges, trading_day=trading_day)
|
||||
day = ctx["trading_day"]
|
||||
summary_payload = {
|
||||
"trading_day": day,
|
||||
"totals": ctx.get("totals"),
|
||||
"accounts": ctx.get("accounts"),
|
||||
}
|
||||
summary_text = format_summary_context_text(summary_payload)
|
||||
digest = summary_context_hash(summary_payload)
|
||||
if not force:
|
||||
latest = get_latest_summary(day)
|
||||
if latest and latest.get("context_hash") == ctx.get("context_hash"):
|
||||
if latest and latest.get("context_hash") == digest:
|
||||
return {
|
||||
"ok": True,
|
||||
"cached": True,
|
||||
@@ -64,7 +68,7 @@ def generate_daily_summary(
|
||||
}
|
||||
|
||||
system = SUMMARY_SYSTEM.replace("{trading_day}", day)
|
||||
user = build_summary_user_prompt(ctx["text"], day)
|
||||
user = build_summary_user_prompt(summary_text, day)
|
||||
content = generate_text(system=system, user=user, temperature=0.15)
|
||||
if content.startswith("AI 调用失败"):
|
||||
return {"ok": False, "msg": content, "trading_day": day}
|
||||
@@ -74,7 +78,7 @@ def generate_daily_summary(
|
||||
trading_day=day,
|
||||
content_md=content,
|
||||
model=model_label(),
|
||||
context_hash=ctx.get("context_hash") or "",
|
||||
context_hash=digest,
|
||||
stats_snapshot=stats_snapshot,
|
||||
)
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user