feat(ml/serving): inject profile features + sort tasks in tip prompt (#79)
- prompts.py: sort tasks overdue-first → priority desc → age desc before rendering into the LLM prompt (same ordering as ml/features/context.py) - prompts.py: render User profile summary line (completion_rate, dismiss_rate, preferred_hour) when profile_features are present - main.py: add profile_features field to PromptContext; plumb from GenerateRequest into the prompt builder via model_copy - logging_config.py: drop add_logger_name processor (incompatible with PrintLoggerFactory — caused test ordering failures) - test_generate.py: 6 new tests covering sort order, profile rendering, partial fields, empty profile, and end-to-end plumbing through /generate Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -23,6 +23,7 @@ class _Ctx(Protocol):
|
||||
hour_of_day: int
|
||||
day_of_week: int
|
||||
extra: dict
|
||||
profile_features: "dict | None"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -33,13 +34,29 @@ class Prompt:
|
||||
|
||||
|
||||
def _base_user_lines(ctx: "_Ctx") -> list[str]:
|
||||
# Overdue tasks first, then high-priority, then oldest — most actionable context at top
|
||||
tasks = sorted(
|
||||
ctx.tasks,
|
||||
key=lambda t: (not t.get("is_overdue", False), -t.get("priority", 1), -t.get("task_age_days", 0.0)),
|
||||
)
|
||||
lines = [f"Time: {ctx.hour_of_day:02d}:00, day_of_week={ctx.day_of_week}"]
|
||||
if ctx.tasks:
|
||||
overdue = [t for t in ctx.tasks if t.get("is_overdue")]
|
||||
lines.append(f"Tasks: {len(ctx.tasks)} total, {len(overdue)} overdue")
|
||||
for t in ctx.tasks[:5]:
|
||||
if tasks:
|
||||
overdue = [t for t in tasks if t.get("is_overdue")]
|
||||
lines.append(f"Tasks: {len(tasks)} total, {len(overdue)} overdue")
|
||||
for t in tasks[:5]:
|
||||
due = t.get("due_date", "no due date")
|
||||
lines.append(f" - [{t.get('priority','?')}] {t.get('content','?')} (due: {due})")
|
||||
p = getattr(ctx, "profile_features", None) or {}
|
||||
if p:
|
||||
parts: list[str] = []
|
||||
if (v := p.get("completion_rate_30d")) is not None:
|
||||
parts.append(f"completion_rate={float(v):.0%}")
|
||||
if (v := p.get("dismiss_rate_30d")) is not None:
|
||||
parts.append(f"dismiss_rate={float(v):.0%}")
|
||||
if (v := p.get("preferred_hour")) is not None:
|
||||
parts.append(f"preferred_hour={int(v):02d}:00")
|
||||
if parts:
|
||||
lines.append(f"User profile: {', '.join(parts)}")
|
||||
for k, v in ctx.extra.items():
|
||||
lines.append(f"{k}: {v}")
|
||||
return lines
|
||||
|
||||
Reference in New Issue
Block a user