|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from typing import Any, Callable, List, Optional, Union, Dict, TYPE_CHECKING |
| 3 | +from .config import TrainerConfig, ModelConfig |
| 4 | +from judgeval.scorers import ExampleScorer, ExampleAPIScorerConfig |
| 5 | + |
| 6 | +if TYPE_CHECKING: |
| 7 | + from judgeval.tracer import Tracer |
| 8 | + from .trainable_model import TrainableModel |
| 9 | + |
| 10 | + |
| 11 | +class BaseTrainer(ABC): |
| 12 | + """ |
| 13 | + Abstract base class for training providers. |
| 14 | +
|
| 15 | + This class defines the interface that all training provider implementations |
| 16 | + must follow. Each provider (Fireworks, Verifiers, etc.) will have its own |
| 17 | + concrete implementation of this interface. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + config: TrainerConfig, |
| 23 | + trainable_model: "TrainableModel", |
| 24 | + tracer: "Tracer", |
| 25 | + project_name: Optional[str] = None, |
| 26 | + ): |
| 27 | + """ |
| 28 | + Initialize the base trainer. |
| 29 | +
|
| 30 | + Args: |
| 31 | + config: TrainerConfig instance with training parameters |
| 32 | + trainable_model: TrainableModel instance to use for training |
| 33 | + tracer: Tracer for observability |
| 34 | + project_name: Project name for organizing training runs |
| 35 | + """ |
| 36 | + self.config = config |
| 37 | + self.trainable_model = trainable_model |
| 38 | + self.tracer = tracer |
| 39 | + self.project_name = project_name or "judgment_training" |
| 40 | + |
| 41 | + @abstractmethod |
| 42 | + async def generate_rollouts_and_rewards( |
| 43 | + self, |
| 44 | + agent_function: Callable[[Any], Any], |
| 45 | + scorers: List[Union[ExampleAPIScorerConfig, ExampleScorer]], |
| 46 | + prompts: List[Any], |
| 47 | + num_prompts_per_step: Optional[int] = None, |
| 48 | + num_generations_per_prompt: Optional[int] = None, |
| 49 | + concurrency: Optional[int] = None, |
| 50 | + ) -> Any: |
| 51 | + """ |
| 52 | + Generate rollouts and compute rewards using the current model snapshot. |
| 53 | +
|
| 54 | + Args: |
| 55 | + agent_function: Function/agent to call for generating responses |
| 56 | + scorers: List of scorer objects to evaluate responses |
| 57 | + prompts: List of prompts to use for training |
| 58 | + num_prompts_per_step: Number of prompts to use per step |
| 59 | + num_generations_per_prompt: Generations per prompt |
| 60 | + concurrency: Concurrency limit |
| 61 | +
|
| 62 | + Returns: |
| 63 | + Provider-specific dataset format for training |
| 64 | + """ |
| 65 | + pass |
| 66 | + |
| 67 | + @abstractmethod |
| 68 | + async def run_reinforcement_learning( |
| 69 | + self, |
| 70 | + agent_function: Callable[[Any], Any], |
| 71 | + scorers: List[Union[ExampleAPIScorerConfig, ExampleScorer]], |
| 72 | + prompts: List[Any], |
| 73 | + ) -> ModelConfig: |
| 74 | + """ |
| 75 | + Run the iterative reinforcement learning fine-tuning loop. |
| 76 | +
|
| 77 | + Args: |
| 78 | + agent_function: Function/agent to call for generating responses |
| 79 | + scorers: List of scorer objects to evaluate responses |
| 80 | + prompts: List of prompts to use for training |
| 81 | +
|
| 82 | + Returns: |
| 83 | + ModelConfig: Configuration of the trained model |
| 84 | + """ |
| 85 | + pass |
| 86 | + |
| 87 | + @abstractmethod |
| 88 | + async def train( |
| 89 | + self, |
| 90 | + agent_function: Callable[[Any], Any], |
| 91 | + scorers: List[Union[ExampleAPIScorerConfig, ExampleScorer]], |
| 92 | + prompts: List[Any], |
| 93 | + ) -> ModelConfig: |
| 94 | + """ |
| 95 | + Start the reinforcement learning fine-tuning process. |
| 96 | +
|
| 97 | + This is the main entry point for running the training. |
| 98 | +
|
| 99 | + Args: |
| 100 | + agent_function: Function/agent to call for generating responses |
| 101 | + scorers: List of scorer objects to evaluate responses |
| 102 | + prompts: List of prompts to use for training |
| 103 | +
|
| 104 | + Returns: |
| 105 | + ModelConfig: Configuration of the trained model |
| 106 | + """ |
| 107 | + pass |
| 108 | + |
| 109 | + @abstractmethod |
| 110 | + def _extract_message_history_from_spans(self) -> List[Dict[str, str]]: |
| 111 | + """ |
| 112 | + Extract message history from spans for training purposes. |
| 113 | +
|
| 114 | + Returns: |
| 115 | + List of message dictionaries with 'role' and 'content' keys |
| 116 | + """ |
| 117 | + pass |
0 commit comments