- S1 Solver (fast model) - Iterative solving with feedback-based repair
- S2 Solver (slow model) - Single attempt escalation when S1 fails or shows no improvement
Classes
CLASS SOFAISamplingStrategy
SOFAI (Slow and Fast AI) two-solver sampling strategy.
Uses S1 Solver (fast model) in a loop with targeted feedback from validation
results. If S1 Solver fails after exhausting the budget or shows no
improvement, escalates to a single attempt with S2 Solver (slow model).
The strategy leverages ValidationResult.reason fields to provide targeted
feedback for repair, enabling more effective iterative improvement.
Args:
s1_solver_backend: Backend for the fast S1 solver used in the iterative repair loop.s2_solver_backend: Backend for the slow S2 solver used as a final escalation step.s2_solver_mode: How to invoke the S2 solver when S1 fails.loop_budget: Maximum number of S1 repair attempts before escalating to S2. Must be greater than 0. Defaults to3.judge_backend: Optional backend for LLM-as-Judge validation. IfNone, falls back to the session backend.feedback_strategy: Detail level of repair feedback provided to the S1 solver.
FUNC repair
old_ctx: The context WITHOUT the last action + output.new_ctx: The context including the last action + output.past_actions: List of actions executed.past_results: List of generation results.past_val: List of validation results.
- Tuple of (Message component with repair feedback, new context).
FUNC select_from_failure
sampled_actions: List of actions that have been executed (without success).sampled_results: List of (unsuccessful) generation results for these actions.sampled_val: List of validation results for the results.
- The index of the result that should be selected as
.value.
FUNC sample
SOFAI Flow Overview:
-
PHASE 1 - S1 Solver Loop:
- Generate candidate solution with fast S1 model
- Validate against requirements
- If success: return immediately
- If failure: generate repair feedback and iterate
- If no improvement detected: early exit to Phase 2
-
PHASE 2 - S2 Solver Escalation:
- Prepare context based on s2_solver_mode:
- fresh_start: clean slate with original prompt
- continue_chat: full S1 conversation history
- best_attempt: best S1 result with feedback summary
- Generate single attempt with slow S2 model
- Validate and return result (success or failure)
- Prepare context based on s2_solver_mode:
action: The component to sample (Instruction, Message, etc.).context: The session context (must be ChatContext).backend: Session backend (used for validation fallback).requirements: Requirements to validate against.validation_ctx: Optional separate validation context (unused).format: Output format for structured outputs.model_options: Model options to pass to backends.tool_calls: True if tool calls should be used.
- SamplingResult with success status and all generation history.