|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Optional, Dict, Any |
| 5 | +import json |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class TrainerConfig: |
| 10 | + """Configuration class for JudgmentTrainer parameters.""" |
| 11 | + |
| 12 | + deployment_id: str |
| 13 | + user_id: str |
| 14 | + model_id: str |
| 15 | + base_model_name: str = "qwen2p5-7b-instruct" |
| 16 | + rft_provider: str = "fireworks" # Supported: "fireworks", "verifiers" (future) |
| 17 | + num_steps: int = 5 |
| 18 | + num_generations_per_prompt: int = 4 |
| 19 | + num_prompts_per_step: int = 4 |
| 20 | + concurrency: int = 100 |
| 21 | + epochs: int = 1 |
| 22 | + learning_rate: float = 1e-5 |
| 23 | + temperature: float = 1.5 |
| 24 | + max_tokens: int = 50 |
| 25 | + enable_addons: bool = True |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class ModelConfig: |
| 30 | + """ |
| 31 | + Configuration class for storing and loading trained model state. |
| 32 | +
|
| 33 | + This class enables persistence of trained models so they can be loaded |
| 34 | + and used later without retraining. |
| 35 | +
|
| 36 | + Example usage: |
| 37 | + trainer = JudgmentTrainer(config) |
| 38 | + model_config = trainer.train(agent_function, scorers, prompts) |
| 39 | +
|
| 40 | + # Save the trained model configuration |
| 41 | + model_config.save_to_file("my_trained_model.json") |
| 42 | +
|
| 43 | + # Later, load and use the trained model |
| 44 | + loaded_config = ModelConfig.load_from_file("my_trained_model.json") |
| 45 | + trained_model = TrainableModel.from_model_config(loaded_config) |
| 46 | +
|
| 47 | + # Use the trained model for inference |
| 48 | + response = trained_model.chat.completions.create( |
| 49 | + model="current", # Uses the loaded trained model |
| 50 | + messages=[{"role": "user", "content": "Hello!"}] |
| 51 | + ) |
| 52 | + """ |
| 53 | + |
| 54 | + # Base model configuration |
| 55 | + base_model_name: str |
| 56 | + deployment_id: str |
| 57 | + user_id: str |
| 58 | + model_id: str |
| 59 | + enable_addons: bool |
| 60 | + |
| 61 | + # Training state |
| 62 | + current_step: int |
| 63 | + total_steps: int |
| 64 | + |
| 65 | + # Current model information |
| 66 | + current_model_name: Optional[str] = None |
| 67 | + is_trained: bool = False |
| 68 | + |
| 69 | + # Training parameters used (for reference) |
| 70 | + training_params: Optional[Dict[str, Any]] = None |
| 71 | + |
| 72 | + def to_dict(self) -> Dict[str, Any]: |
| 73 | + """Convert ModelConfig to dictionary for serialization.""" |
| 74 | + return { |
| 75 | + "base_model_name": self.base_model_name, |
| 76 | + "deployment_id": self.deployment_id, |
| 77 | + "user_id": self.user_id, |
| 78 | + "model_id": self.model_id, |
| 79 | + "enable_addons": self.enable_addons, |
| 80 | + "current_step": self.current_step, |
| 81 | + "total_steps": self.total_steps, |
| 82 | + "current_model_name": self.current_model_name, |
| 83 | + "is_trained": self.is_trained, |
| 84 | + "training_params": self.training_params, |
| 85 | + } |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def from_dict(cls, data: Dict[str, Any]) -> ModelConfig: |
| 89 | + """Create ModelConfig from dictionary.""" |
| 90 | + return cls( |
| 91 | + base_model_name=data.get("base_model_name", "qwen2p5-7b-instruct"), |
| 92 | + deployment_id=data.get("deployment_id", "my-base-deployment"), |
| 93 | + user_id=data.get("user_id", ""), |
| 94 | + model_id=data.get("model_id", ""), |
| 95 | + enable_addons=data.get("enable_addons", True), |
| 96 | + current_step=data.get("current_step", 0), |
| 97 | + total_steps=data.get("total_steps", 0), |
| 98 | + current_model_name=data.get("current_model_name"), |
| 99 | + is_trained=data.get("is_trained", False), |
| 100 | + training_params=data.get("training_params"), |
| 101 | + ) |
| 102 | + |
| 103 | + def to_json(self) -> str: |
| 104 | + """Convert ModelConfig to JSON string.""" |
| 105 | + return json.dumps(self.to_dict(), indent=2) |
| 106 | + |
| 107 | + @classmethod |
| 108 | + def from_json(cls, json_str: str) -> ModelConfig: |
| 109 | + """Create ModelConfig from JSON string.""" |
| 110 | + data = json.loads(json_str) |
| 111 | + return cls.from_dict(data) |
| 112 | + |
| 113 | + def save_to_file(self, filepath: str): |
| 114 | + """Save ModelConfig to a JSON file.""" |
| 115 | + with open(filepath, "w") as f: |
| 116 | + f.write(self.to_json()) |
| 117 | + |
| 118 | + @classmethod |
| 119 | + def load_from_file(cls, filepath: str) -> ModelConfig: |
| 120 | + """Load ModelConfig from a JSON file.""" |
| 121 | + with open(filepath, "r") as f: |
| 122 | + json_str = f.read() |
| 123 | + return cls.from_json(json_str) |
0 commit comments