|
12 | 12 |
|
13 | 13 | from ragas._analytics import EvaluationEvent, _analytics_batcher |
14 | 14 | from ragas.callbacks import ChainType, new_group |
15 | | -from ragas.dataset_schema import MultiTurnSample, SingleTurnSample |
| 15 | +from ragas.dataset_schema import MetricAnnotation, MultiTurnSample, SingleTurnSample |
16 | 16 | from ragas.executor import is_event_loop_running |
| 17 | +from ragas.losses import BinaryMetricLoss, MSELoss |
17 | 18 | from ragas.prompt import PromptMixin |
18 | 19 | from ragas.run_config import RunConfig |
19 | 20 | from ragas.utils import ( |
@@ -232,12 +233,77 @@ def init(self, run_config: RunConfig): |
232 | 233 | def train( |
233 | 234 | self, |
234 | 235 | path: str, |
235 | | - demonstration_config: DemonstrationConfig, |
236 | | - instruction_config: InstructionConfig, |
237 | | - callbacks: Callbacks, |
| 236 | + demonstration_config: t.Optional[DemonstrationConfig] = None, |
| 237 | + instruction_config: t.Optional[InstructionConfig] = None, |
| 238 | + callbacks: t.Optional[Callbacks] = None, |
| 239 | + run_config: t.Optional[RunConfig] = None, |
| 240 | + batch_size: t.Optional[int] = None, |
| 241 | + with_debugging_logs=False, |
| 242 | + raise_exceptions: bool = True, |
238 | 243 | ) -> None: |
239 | 244 |
|
240 | | - raise NotImplementedError("Training is not implemented for this metric.") |
| 245 | + if not path.endswith(".json"): |
| 246 | + raise ValueError("Train data must be in json format") |
| 247 | + |
| 248 | + if instruction_config is None: |
| 249 | + from ragas.config import InstructionConfig |
| 250 | + |
| 251 | + instruction_config = InstructionConfig() |
| 252 | + |
| 253 | + if demonstration_config is None: |
| 254 | + from ragas.config import DemonstrationConfig |
| 255 | + |
| 256 | + demonstration_config = DemonstrationConfig() |
| 257 | + |
| 258 | + dataset = MetricAnnotation.from_json(path, metric_name=self.name) |
| 259 | + |
| 260 | + optimizer = instruction_config.optimizer |
| 261 | + llm = instruction_config.llm or self.llm |
| 262 | + if llm is None: |
| 263 | + raise ValueError( |
| 264 | + f"Metric '{self.name}' has no valid LLM provided (self.llm is None). Please initantiate a the metric with an LLM to run." # noqa |
| 265 | + ) |
| 266 | + if optimizer.llm is None: |
| 267 | + optimizer.llm = llm |
| 268 | + |
| 269 | + if instruction_config.loss is None: |
| 270 | + if self.output_type is None: |
| 271 | + raise ValueError( |
| 272 | + f"Output type for metric '{self.name}' is not defined. Please set the output type in the metric or in the instruction config." |
| 273 | + ) |
| 274 | + |
| 275 | + if self.output_type.name == MetricOutputType.BINARY.name: |
| 276 | + loss_fun = BinaryMetricLoss() |
| 277 | + elif ( |
| 278 | + self.output_type.name == MetricOutputType.CONTINUOUS.name |
| 279 | + or self.output_type.name == MetricOutputType.DISCRETE.name |
| 280 | + ): |
| 281 | + loss_fun = MSELoss() |
| 282 | + else: |
| 283 | + raise NotImplementedError( |
| 284 | + f"Output type '{self.output_type.name}' not implemented" |
| 285 | + ) |
| 286 | + else: |
| 287 | + loss_fun = instruction_config.loss |
| 288 | + |
| 289 | + optimizer.metric = self |
| 290 | + |
| 291 | + optimizer_config = instruction_config.optimizer_config or {} |
| 292 | + optimized_prompts = optimizer.optimize( |
| 293 | + dataset[self.name], |
| 294 | + loss_fun, |
| 295 | + optimizer_config, |
| 296 | + callbacks=callbacks, |
| 297 | + run_config=run_config, |
| 298 | + batch_size=batch_size, |
| 299 | + with_debugging_logs=with_debugging_logs, |
| 300 | + raise_exceptions=raise_exceptions, |
| 301 | + ) |
| 302 | + prompts = self.get_prompts() |
| 303 | + for key, val in optimized_prompts.items(): |
| 304 | + prompts[key].instruction = val |
| 305 | + self.set_prompts(**prompts) |
| 306 | + return |
241 | 307 |
|
242 | 308 |
|
243 | 309 | @dataclass |
|
0 commit comments