Adding llm_fix to use selected models for the fixing step
Browse files
chains/exercises/run_fluster_with_diagnosis.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# chains/exercises/run_fluster_with_diagnosis.py
|
| 2 |
import asyncio
|
| 3 |
-
from typing import Tuple, List
|
| 4 |
|
| 5 |
from app.helpers.exercise_standardizer import structurize_exercise, ExerciseSet, Exercise, exercise_to_string
|
| 6 |
from chains.exercises.runner_without import write_fluster_track
|
|
@@ -64,6 +64,9 @@ async def _async_fluster_with_diagnosis(
|
|
| 64 |
fluster_config = chain_configs["fluster"]
|
| 65 |
diagnoser_config = chain_configs["diagnoser"]
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
# 1) Generate track0 & track2 in parallel
|
| 68 |
track0_coro = write_fluster_track(
|
| 69 |
user_input_text,
|
|
@@ -85,8 +88,8 @@ async def _async_fluster_with_diagnosis(
|
|
| 85 |
fluster2_exs = await parse_fluster_text_to_exercises(track2_text)
|
| 86 |
|
| 87 |
# 3) Diagnose + fix each exercise
|
| 88 |
-
diag0_results, fixed0_exs = await diagnose_and_fix_all(fluster0_exs, diagnoser_config)
|
| 89 |
-
diag2_results, fixed2_exs = await diagnose_and_fix_all(fluster2_exs, diagnoser_config)
|
| 90 |
|
| 91 |
# 4) Convert the final exercises to strings for display
|
| 92 |
# (Or you can store them back into a bigger data structure.)
|
|
@@ -155,9 +158,9 @@ async def write_fluster_track(
|
|
| 155 |
|
| 156 |
|
| 157 |
async def diagnose_and_fix_all(
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
) -> tuple[List[str], List[Exercise]]:
|
| 161 |
"""
|
| 162 |
For each exercise, run the 'diagnose_only' from the DiagnoserChain,
|
| 163 |
then interpret the results (scorecard) to see if we need a fix,
|
|
@@ -166,6 +169,7 @@ async def diagnose_and_fix_all(
|
|
| 166 |
Returns:
|
| 167 |
- a list of strings (one per exercise) summarizing the diagnosis,
|
| 168 |
- a list of possibly fixed exercises.
|
|
|
|
| 169 |
"""
|
| 170 |
diag_chain = diagnoser_config["class"](
|
| 171 |
templates_diagnose=diagnoser_config["templates_diagnose"],
|
|
@@ -195,7 +199,7 @@ async def diagnose_and_fix_all(
|
|
| 195 |
fluster_config = chain_configs["fluster"]
|
| 196 |
|
| 197 |
if "❌" in scorecard:
|
| 198 |
-
ex_fixed = await fix_exercise(ex, scorecard, fluster_config)
|
| 199 |
fixed_exs.append(ex_fixed)
|
| 200 |
else:
|
| 201 |
fixed_exs.append(ex)
|
|
@@ -236,9 +240,10 @@ async def diagnose_exercise(ex: Exercise) -> str:
|
|
| 236 |
|
| 237 |
from pydantic import ValidationError
|
| 238 |
|
| 239 |
-
async def fix_exercise(ex: Exercise, diag_str: str, cfg: dict) -> Exercise:
|
| 240 |
tmpl_fix = cfg["template_fix_exercise"]
|
| 241 |
-
|
|
|
|
| 242 |
llm_cast = cfg["llm_structurize"] # already in chain_configs
|
| 243 |
|
| 244 |
# 1️⃣ first call – creative rewrite
|
|
|
|
| 1 |
# chains/exercises/run_fluster_with_diagnosis.py
|
| 2 |
import asyncio
|
| 3 |
+
from typing import Tuple, List, Any
|
| 4 |
|
| 5 |
from app.helpers.exercise_standardizer import structurize_exercise, ExerciseSet, Exercise, exercise_to_string
|
| 6 |
from chains.exercises.runner_without import write_fluster_track
|
|
|
|
| 64 |
fluster_config = chain_configs["fluster"]
|
| 65 |
diagnoser_config = chain_configs["diagnoser"]
|
| 66 |
|
| 67 |
+
llm1 = llms.get(model_choice_1, fluster_config["default_llm_a"])
|
| 68 |
+
llm2 = llms.get(model_choice_2, fluster_config["default_llm_b"])
|
| 69 |
+
|
| 70 |
# 1) Generate track0 & track2 in parallel
|
| 71 |
track0_coro = write_fluster_track(
|
| 72 |
user_input_text,
|
|
|
|
| 88 |
fluster2_exs = await parse_fluster_text_to_exercises(track2_text)
|
| 89 |
|
| 90 |
# 3) Diagnose + fix each exercise
|
| 91 |
+
diag0_results, fixed0_exs = await diagnose_and_fix_all(fluster0_exs, diagnoser_config, llm_fix=llm1)
|
| 92 |
+
diag2_results, fixed2_exs = await diagnose_and_fix_all(fluster2_exs, diagnoser_config, llm_fix=llm2)
|
| 93 |
|
| 94 |
# 4) Convert the final exercises to strings for display
|
| 95 |
# (Or you can store them back into a bigger data structure.)
|
|
|
|
| 158 |
|
| 159 |
|
| 160 |
async def diagnose_and_fix_all(
|
| 161 |
+
exercises: List[Exercise],
|
| 162 |
+
diagnoser_config: dict,
|
| 163 |
+
llm_fix: Any) -> tuple[List[str], List[Exercise]]:
|
| 164 |
"""
|
| 165 |
For each exercise, run the 'diagnose_only' from the DiagnoserChain,
|
| 166 |
then interpret the results (scorecard) to see if we need a fix,
|
|
|
|
| 169 |
Returns:
|
| 170 |
- a list of strings (one per exercise) summarizing the diagnosis,
|
| 171 |
- a list of possibly fixed exercises.
|
| 172 |
+
:param llm_fix:
|
| 173 |
"""
|
| 174 |
diag_chain = diagnoser_config["class"](
|
| 175 |
templates_diagnose=diagnoser_config["templates_diagnose"],
|
|
|
|
| 199 |
fluster_config = chain_configs["fluster"]
|
| 200 |
|
| 201 |
if "❌" in scorecard:
|
| 202 |
+
ex_fixed = await fix_exercise(ex, scorecard, fluster_config, llm_fix)
|
| 203 |
fixed_exs.append(ex_fixed)
|
| 204 |
else:
|
| 205 |
fixed_exs.append(ex)
|
|
|
|
| 240 |
|
| 241 |
from pydantic import ValidationError
|
| 242 |
|
| 243 |
+
async def fix_exercise(ex: Exercise, diag_str: str, cfg: dict, llm_fix:None) -> Exercise:
|
| 244 |
tmpl_fix = cfg["template_fix_exercise"]
|
| 245 |
+
if not llm_fix:
|
| 246 |
+
llm_fix = cfg["llm_fix_exercise"]
|
| 247 |
llm_cast = cfg["llm_structurize"] # already in chain_configs
|
| 248 |
|
| 249 |
# 1️⃣ first call – creative rewrite
|