Enhance Planning Exercise Suggestion with LLM-Rerank and Client Overrides
Deploy Development / deploy (push) Successful in 43s
Test Suite / pytest-backend (push) Successful in 43s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 13s
Test Suite / k6 /health Baseline (push) Successful in 47s
Test Suite / playwright-tests (push) Successful in 1m14s

- Implemented optional LLM-Rerank functionality in the planning exercise suggestion process, allowing for improved exercise ranking based on user-defined criteria.
- Updated the `suggestPlanningExercises` API to accept `planned_exercise_ids` for client-side overrides, enhancing flexibility in exercise selection.
- Enhanced the `ExercisePickerModal` to reflect LLM ranking status and support new planning context features.
- Incremented application version to 0.8.170 and updated changelog to document the new features and improvements in the planning AI capabilities.
This commit is contained in:
Lars committed 2026-05-22 22:09:28 +02:00
1 parent 128a9d752e
commit 207817376d
12 files changed
+463 -14

No files matched your search

+75 -1
View File
@@ -17,6 +17,7 @@ from planning_exercise_profiles import (
load_exercise_match_profiles_bulk,
score_exercise_against_target,
)
from planning_exercise_llm_rank import try_llm_rerank_planning_hits
# Planungs-Berechtigung + Sektionen (bestehende Implementierung)
from routers.training_planning import (
@@ -40,6 +41,7 @@ VALID_INTENTS = {
}
_CANDIDATE_POOL_LIMIT = 400
_LLM_RERANK_PRE_LIMIT = 32
class PlanningExerciseSuggestRequest(BaseModel):
@@ -51,6 +53,8 @@ class PlanningExerciseSuggestRequest(BaseModel):
progression_graph_id: Optional[int] = Field(default=None, ge=1)
query: Optional[str] = ""
intent_hint: Optional[str] = None
planned_exercise_ids: Optional[List[int]] = None
include_llm_rank: bool = False
limit: int = Field(default=20, ge=1, le=50)
exercise_kind_any: Optional[List[str]] = None
@@ -240,6 +244,42 @@ def _skill_jaccard(a: Set[int], b: Set[int]) -> float:
return inter / union if union else 0.0
def _apply_client_planned_override(
cur,
pack: Dict[str, Any],
body: PlanningExerciseSuggestRequest,
) -> Dict[str, Any]:
"""Client-Plan (ungespeichertes Formular) überschreibt DB-Stand."""
if not body.planned_exercise_ids:
return pack
planned_ids: List[int] = []
seen: Set[int] = set()
for raw in body.planned_exercise_ids:
try:
eid = int(raw)
except (TypeError, ValueError):
continue
if eid < 1 or eid in seen:
continue
seen.add(eid)
planned_ids.append(eid)
if not planned_ids:
return pack
pack["planned_exercise_ids"] = planned_ids
if not body.anchor_exercise_id:
anchor_id = _resolve_anchor_from_plan(planned_ids, None)
pack["anchor_exercise_id"] = anchor_id
if anchor_id:
titles = _load_exercise_titles(cur, [anchor_id])
pack["anchor_title"] = titles.get(anchor_id)
pack["anchor_skill_ids"] = sorted(_load_skill_ids_for_exercise(cur, anchor_id))
else:
pack["anchor_title"] = None
pack["anchor_skill_ids"] = []
return pack
def build_planning_exercise_context_pack(
cur,
*,
@@ -327,6 +367,7 @@ def suggest_planning_exercises(
body: PlanningExerciseSuggestRequest,
) -> Dict[str, Any]:
pack = build_planning_exercise_context_pack(cur, tenant=tenant, body=body)
pack = _apply_client_planned_override(cur, pack, body)
query = _normalize_query(body.query)
intent = resolve_planning_exercise_intent(query, body.intent_hint)
weights = _intent_weights(intent)
@@ -497,6 +538,38 @@ def suggest_planning_exercises(
)
hits.sort(key=lambda h: (-h["score"], h.get("title") or ""))
llm_applied = False
retrieval_phase = "profile_v1"
if body.include_llm_rank:
pre_limit = max(int(body.limit), _LLM_RERANK_PRE_LIMIT)
pool_hits = hits[:pre_limit]
pool_hits, llm_applied = try_llm_rerank_planning_hits(
cur,
hits=pool_hits,
skills_by_ex=skills_by_ex,
query=query,
intent=intent,
context_summary={
"unit_title": pack.get("unit_title"),
"group_name": pack.get("group_name"),
"section_title": pack.get("section_title"),
"planned_count": len(planned_set),
"anchor_title": pack.get("anchor_title"),
"intent": intent,
},
target_profile_summary=target_profile_summary,
limit=int(body.limit),
)
if llm_applied:
retrieval_phase = "profile_v1+llm_rank"
tail = hits[pre_limit:]
hits = pool_hits + tail
else:
hits = pool_hits[: int(body.limit)]
else:
hits = hits[: int(body.limit)]
hits = hits[: int(body.limit)]
context_summary = {
@@ -512,7 +585,8 @@ def suggest_planning_exercises(
return {
"context_summary": context_summary,
"target_profile_summary": target_profile_summary,
"retrieval_phase": "profile_v1",
"retrieval_phase": retrieval_phase,
"llm_rank_applied": llm_applied,
"intent_resolved": intent,
"query_normalized": query or None,
"hits": hits,