DSPy: The framework for programming—not prompting—language models
Python
Merge branch 'main' into dev_finetune
Isaac Millerac5fd2e06a89
diff --cc dspy/clients/lm.pyindex eeb61f4f5,186c622b9..d8dadec5b--- a/dspy/clients/lm.py+++ b/dspy/clients/lm.py@@@ -102,68 -63,21 +102,76 @@@ class LMdef inspect_history(self, n: int = 1):_inspect_history(self, n)+ def launch(self):+ """Send a request to the provider to launch the model, if needed."""+ if is_self_hosted_model(self.model):+ self_hosted_model_launch(self.model, self.launch_kwargs)+ elif is_anyscale_model(self.model):+ anyscale_model_launch(self.model, self.launch_kwargs)+ msg = f"`launch()` is called for the auto-launched model {self.model}"+ msg += " -- no action is taken!"+ logger.info(msg)++ def kill(self):+ """Send a request to the provider to kill the model, if needed."""+ if is_self_hosted_model(self.model):+ self_hosted_model_kill(self.model, self.launch_kwargs)+ elif is_anyscale_model(self.model):+ anyscale_model_kill(self.model, self.launch_kwargs)+ msg = f"`kill()` is called for the auto-launched model {self.model}"+ msg += " -- no action is taken!"+ logger.info(msg)++ def finetune(self,+ train_method: TrainingMethod,+ train_data: List[Dict[str, Any]],+ train_kwargs: Optional[Dict[str, Any]]=None,+ cache_finetune: bool = True,+ provider: str = "openai",+ ) -> FinetuneJob:+ """Start model fine-tuning, if supported."""+ # Fine-tuning is experimental and requires the experimental flag+ from dspy import settings as settings+ err = "Fine-tuning is an experimental feature."+ err += " Set `dspy.settings.experimental` to `True` to use it."+ assert settings.experimental, err++ # Initialize the finetune job+ FinetuneJobClass = get_provider_finetune_job_class(provider=provider)+ finetune_job = FinetuneJobClass(+ model=self.model,+ provider=provider,+ train_data=train_data,+ train_kwargs=train_kwargs,+ )++ # Start asyncronous training+ executor = ThreadPoolExecutor(max_workers=1)+ executor.submit(+ execute_finetune_job,+ finetune_job,+ lm=self,+ cache_finetune=cache_finetune+ )+ executor.shutdown(wait=False)++ return finetune_job+def copy(self, **kwargs):- """Returns a copy of the language model with the same parameters."""- kwargs = {**self.kwargs, **kwargs}- # model = kwargs.pop("model") or self.model- init_kwargs = dict(model=self.model, model_type=self.model_type, cache=self.cache, temperature=self.kwargs["temperature"], max_tokens=self.kwargs["max_tokens"])- init_kwargs = {**init_kwargs, **kwargs}- return self.__class__(**init_kwargs)+ """Returns a copy of the language model with possibly updated parameters."""++ import copy+ new_instance = copy.deepcopy(self)+ new_instance.history = []++ for key, value in kwargs.items():+ if hasattr(self, key):+ setattr(new_instance, key, value)+ if (key in self.kwargs) or (not hasattr(self, key)):+ new_instance.kwargs[key] = value++ return new_instance+@functools.lru_cache(maxsize=None)