From feb191c34d2a5e7191df9af6511cdf4b92d32f6a Mon Sep 17 00:00:00 2001 From: Andrii Ieroshenko Date: Thu, 9 Nov 2023 15:25:57 -0800 Subject: [PATCH] Handle model_provider_id not associating with model --- .../jupyter-ai/jupyter_ai/config_manager.py | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/config_manager.py b/packages/jupyter-ai/jupyter_ai/config_manager.py index 4f000b484..5e35fdde1 100644 --- a/packages/jupyter-ai/jupyter_ai/config_manager.py +++ b/packages/jupyter-ai/jupyter_ai/config_manager.py @@ -59,6 +59,12 @@ class BlockedModelError(Exception): pass +class ConfigValidationError(Exception): + def __init__(self, property_name, message): + self.property_name = property_name + super().__init__(message) + + def _validate_provider_authn(config: GlobalConfig, provider: AnyProvider): # TODO: handle non-env auth strategies if not provider.auth_strategy or provider.auth_strategy.type != "env": @@ -171,7 +177,14 @@ def _init_config(self): # re-write to the file to validate the config and apply any # updates to the config file immediately - self._write_config(config) + try: + self._write_config(config) + except ConfigValidationError as e: + if e.property_name == "model_provider_id": + self.log.warning(f"{str(e)} Setting to None") + config.model_provider_id = None + self._write_config(config) + return properties = self.validator.schema.get("properties", {}) @@ -213,8 +226,9 @@ def _validate_config(self, config: GlobalConfig): # verify model is declared by some provider if not lm_provider: - raise ValueError( - f"No language model is associated with '{config.model_provider_id}'." + raise ConfigValidationError( + "model_provider_id", + f"No language model is associated with '{config.model_provider_id}'.", ) # verify model is not blocked