small fixes
Browse files
chains/exercises/run_fluster_with_diagnosis.py
CHANGED
|
@@ -1,10 +1,55 @@
|
|
| 1 |
# chains/exercises/run_fluster_with_diagnosis.py
|
| 2 |
import asyncio
|
| 3 |
-
from typing import Tuple
|
| 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
|
| 7 |
from config.chain_configs import chain_configs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
async def _async_fluster_with_diagnosis(
|
|
@@ -47,8 +92,8 @@ async def _async_fluster_with_diagnosis(
|
|
| 47 |
(t0_idx, track0_text), (t2_idx, track2_text) = await asyncio.gather(track0_coro, track2_coro)
|
| 48 |
|
| 49 |
# 2) Parse each final text => list of Exercises
|
| 50 |
-
fluster0_exs = parse_fluster_text_to_exercises(track0_text)
|
| 51 |
-
fluster2_exs = parse_fluster_text_to_exercises(track2_text)
|
| 52 |
|
| 53 |
# 3) Diagnose + fix each exercise
|
| 54 |
diag0_results, fixed0_exs = await diagnose_and_fix_all(fluster0_exs, diagnoser_config)
|
|
|
|
| 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
|
| 7 |
from config.chain_configs import chain_configs
|
| 8 |
+
from config.llm_config import llms
|
| 9 |
+
|
| 10 |
+
import json
|
| 11 |
+
from typing import List
|
| 12 |
+
from pydantic import ValidationError
|
| 13 |
+
|
| 14 |
+
from config.chain_configs import chain_configs
|
| 15 |
+
from app.helpers.exercise_standardizer import ExerciseSet, Exercise
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
async def parse_fluster_text_to_exercises(fluster_text: str) -> List[Exercise]:
|
| 19 |
+
"""
|
| 20 |
+
Calls the fluster chain's template_structurize + llm_structurize
|
| 21 |
+
with a structured output approach, expecting an ExerciseSet Pydantic object.
|
| 22 |
+
Returns the list of Exercise objects.
|
| 23 |
+
|
| 24 |
+
Raises ValueError if the LLM fails or refuses to parse,
|
| 25 |
+
or if the LLM's output is invalid JSON for our schema.
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
# 1) Get the parse-related template & LLM from the fluster config
|
| 29 |
+
config = chain_configs["fluster"]
|
| 30 |
+
template_structurize = config["template_structurize"]
|
| 31 |
+
llm_structurize = config["llm_structurize"]
|
| 32 |
+
|
| 33 |
+
# 2) Format the prompt
|
| 34 |
+
prompt_value = await template_structurize.aformat_prompt(fluster=fluster_text)
|
| 35 |
+
messages = prompt_value.to_messages()
|
| 36 |
+
|
| 37 |
+
# 3) Call the LLM with structured output
|
| 38 |
+
# If your version of langchain_openai supports .with_structured_output(ExerciseSet):
|
| 39 |
+
response = await llm_structurize.with_structured_output(ExerciseSet).ainvoke(messages)
|
| 40 |
+
|
| 41 |
+
# 4) Extract the parsed object
|
| 42 |
+
exercise_set = response.choices[0].message.parsed # This should be an ExerciseSet instance
|
| 43 |
+
|
| 44 |
+
if exercise_set is None:
|
| 45 |
+
# If the LLM refused or the format was invalid, we might get None
|
| 46 |
+
raise ValueError(
|
| 47 |
+
f"LLM refused or returned invalid structured data.\nRaw content:\n{response.choices[0].message.content}"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# 5) Return the exercises list
|
| 51 |
+
# If there's also an .id field at exercise_set.id, you can store it if needed.
|
| 52 |
+
return exercise_set.exercises
|
| 53 |
|
| 54 |
|
| 55 |
async def _async_fluster_with_diagnosis(
|
|
|
|
| 92 |
(t0_idx, track0_text), (t2_idx, track2_text) = await asyncio.gather(track0_coro, track2_coro)
|
| 93 |
|
| 94 |
# 2) Parse each final text => list of Exercises
|
| 95 |
+
fluster0_exs = await parse_fluster_text_to_exercises(track0_text)
|
| 96 |
+
fluster2_exs = await parse_fluster_text_to_exercises(track2_text)
|
| 97 |
|
| 98 |
# 3) Diagnose + fix each exercise
|
| 99 |
diag0_results, fixed0_exs = await diagnose_and_fix_all(fluster0_exs, diagnoser_config)
|
config/chain_configs.py
CHANGED
|
@@ -16,7 +16,7 @@ from config.templates import (
|
|
| 16 |
template_write_fluster_a,
|
| 17 |
template_write_fluster_b,
|
| 18 |
template_refine_fluster,
|
| 19 |
-
template_sanitize_fluster, template_isolate_exercises,
|
| 20 |
)
|
| 21 |
from chains.diagnoser.diagnoser_chain import DiagnoserChain
|
| 22 |
from chains.distractors.distractors_chain import DistractorsChain
|
|
|
|
| 16 |
template_write_fluster_a,
|
| 17 |
template_write_fluster_b,
|
| 18 |
template_refine_fluster,
|
| 19 |
+
template_sanitize_fluster, template_isolate_exercises, template_fix_exercise,
|
| 20 |
)
|
| 21 |
from chains.diagnoser.diagnoser_chain import DiagnoserChain
|
| 22 |
from chains.distractors.distractors_chain import DistractorsChain
|