DSPy: The framework for programming—not prompting—language models
Python
Add input validation to finetuning
dilara653408276bd4
diff --git a/dsp/modules/lm.py b/dsp/modules/lm.pyindex 71cec0ef0..eec79829f 100644--- a/dsp/modules/lm.py+++ b/dsp/modules/lm.py@@ -3,7 +3,9 @@ from asyncio import Futurefrom concurrent.futures import ThreadPoolExecutorfrom copy import deepcopyfrom enum import Enum-from typing import Optional+from typing import Optional, Union, List+import ujson+class LM(ABC):"""Abstract class for language models."""@@ -145,27 +147,95 @@ class LM(ABC):return self.__class__(model=model, **kwargs)++#-------------------------------------------------------------------------------+# Classes for finetuning LMs+#-------------------------------------------------------------------------------++class TrainingMethod(Enum):+ # TODO: add docstringSFT = "SFT"- Contrastive = "Contrastive"+ Preference = "Preference"+++"""Dictionary mapping training methods to the data keys they require."""+TRAINING_METHOD_TO_DATA_KEYS = {+ TrainingMethod.SFT: ["prompt", "completion"],+ TrainingMethod.Preference: ["prompt", "chosen", "rejected"],+}+class TrainableLM(LM, ABC):- def get_finetune(self, train_path: str, val_path: Optional[str], method: TrainingMethod, **kwargs) -> Future['TrainableLM']:+ # TODO: add docstring++ """The training methods supported by this class, should be overwritten."""+ SUPPORTED_TRAINING_METHODS: List[TrainingMethod] = []++ def verify_data_format(+ self,+ method: TrainingMethod,+ data_path: str+ ) -> Optional[AssertionError]:+ # TODO: add docstring+ expected_keys = TRAINING_METHOD_TO_DATA_KEYS[method]+ data = ujson.load(open(data_path))+ for ind, data_dict in enumerate(data):+ err_msg = f"The datapoint at index {ind} is missing the keys required for {method} training.\n"+ err_msg = f" Expected: {expected_keys}"+ err_msg = f" Found: {data_dict.keys()}"+ assert all([key in data_dict for key in expected_keys]), err_msg++ def get_finetune(+ self,+ method: TrainingMethod,+ train_path: str,+ eval_path: Optional[str],+ **kwargs+ ) -> Union[Future['TrainableLM'], Exception]:+ # TODO: add docstring++ # Input verification+ assert method in self.SUPPORTED_TRAINING_METHODS+ self.verify_data_format(method, data_path=train_path)+ if eval_path:+ self.verify_data_format(method, data_path=eval_path)++ # Create new model that will eventually be obtained through+ # future.result() when its "start_training" method completes its+ # executionfuture: Future['TrainableLM'] = Future()-new_lm = deepcopy(self)- # Capture the current instance in the closure and start the training process asynchronously+ # Capture the current instance in the closure and start the training+ # process asynchronouslyexecutor = ThreadPoolExecutor(max_workers=1)- executor.submit(new_lm.start_training, future, train_path=train_path, val_path=val_path, method=method, **kwargs)+ executor.submit(+ new_lm.start_training,+ future,+ method=method,+ train_path=train_path,+ eval_path=eval_path,+ **kwargs+ )executor.shutdown(wait=False)return future@abstractmethod- def start_training(self, future: Future['TrainableLM'], train_path: str, val_path: Optional[str], method: TrainingMethod, **kwargs):- pass+ def start_training(+ self,+ future: Future['TrainableLM'],+ train_path: str,+ val_path: Optional[str],+ method: TrainingMethod,+ **kwargs):+ # TODO: add docstring+ lname = self.__class__.__name__+ raise NotImplementedError(f"{lname} does not implement the 'start_training' method.")@abstractmethod- def stop_training(self) -> None:- pass\ No newline at end of file+ def stop_training(self):+ # TODO: add docstring+ lname = self.__class__.__name__+ raise NotImplementedError(f"{lname} does not implement the 'stop_training' method.")diff --git a/dsp/modules/trainable_openai.py b/dsp/modules/trainable_openai.pyindex a25f4f8d2..a1e6a4589 100644--- a/dsp/modules/trainable_openai.py+++ b/dsp/modules/trainable_openai.py@@ -161,7 +161,8 @@ class TrainableOpenAI(GPT3, TrainableLM):model_type (Literal["chat", "text"], optional): The type of model that was specified. Mainly to decide the optimal prompting strategy. Defaults to "text".system_prompt (Optional[str], optional): The system prompt to use. Defaults to None in init, and "You are a helpful assistant." in format_data_for_vanilla_finetuning.**kwargs: Additional arguments to pass to the API provider.- """+ """+ SUPPORTED_TRAINING_METHODS = [TrainingMethod.SFT]def __init__(self,@@ -231,19 +232,20 @@ class TrainableOpenAI(GPT3, TrainableLM):return wrapped_messagesreturn list(map(format_single_item, data))- def _load_data(self, train_path: str, val_path: Optional[str]):- """Load data from a file and submit it to the OpenAI API for fine-tuning.+ def _submit_data(self, train_path: str, eval_path: Optional[str]):+ """Submit the data files to OpenAI API to be later used for fine-tuning.+API reference: https://platform.openai.com/docs/api-reference/files/objectArgs:- data_path (str): The path to the file containing the data.+ train_data: The path to the file containing the data.Returns:str: The file id of the data to be used for fine-tuning."""datasets = {"train": train_path}- if val_path:- datasets["val"] = val_path+ if eval_path:+ datasets["val"] = eval_pathfor name, path in datasets.items():file = openai.files.create(file=open(f"{path}", "rb"),@@ -329,7 +331,7 @@ class TrainableOpenAI(GPT3, TrainableLM):else:raise RuntimeError("Job not completed yet, cannot retrieve model")- def start_training(self, future: Future['TrainableOpenAI'], train_path: str, val_path: Optional[str], method: TrainingMethod = "SFT", **kwargs):+ def start_training(self, future: Future['TrainableOpenAI'], method: TrainingMethod, train_path: str, eval_path: Optional[str], **kwargs):"""Handles the fine-tuning process for an OpenAIModel instance.@@ -339,14 +341,15 @@ class TrainableOpenAI(GPT3, TrainableLM):**kwargs: Additional arguments for fine-tuning."""try:- if method != "SFT":- raise NotImplementedError("Only SFT finetuning is supported at the moment.")-+ if method not in self.SUPPORTED_TRAINING_METHODS:+ raise NotImplementedError(f"TrainableOpenAI can only support {TrainingMethod.SFT} for the time being")+traindataset = ujson.load(open(train_path))- valdataset = ujson.load(open(val_path)) if val_path else None+ valdataset = ujson.load(open(eval_path)) if eval_path else Noneself._verify_training_arguments(traindataset, valdataset, kwargs)- self._load_data(train_path, val_path)+ self._submit_data(train_path, eval_path)+# Start the remote trainingjob_id = self._start_remote_training(**kwargs)@@ -408,4 +411,4 @@ class TrainableOpenAI(GPT3, TrainableLM):Returns:Future[TrainableLM]: A Future object that will hold the fine-tuned model"""- return super().get_finetune(train_path, val_path, method, **kwargs)\ No newline at end of file+ return super().get_finetune(train_path, val_path, method, **kwargs)