snowflake.ml.model.custom_model.ModelContext¶
- class snowflake.ml.model.custom_model.ModelContext(*, artifacts: Optional[Union[dict[str, str], str, catboost.CatBoost, lightgbm.LGBMModel, lightgbm.Booster, CustomModel, sklearn.base.BaseEstimator, sklearn.pipeline.Pipeline, xgboost.XGBModel, xgboost.Booster, torch.nn.Module, torch.jit.ScriptModule, tensorflow.Module, keras.Model, base.BaseEstimator, mlflow.pyfunc.PyFuncModel, transformers.Pipeline, sentence_transformers.SentenceTransformer, HuggingFacePipelineModel]] = None, models: HuggingFacePipelineModel]] = None, **kwargs: Optional[Union[str, catboost.CatBoost, lightgbm.LGBMModel, lightgbm.Booster, CustomModel, sklearn.base.BaseEstimator, sklearn.pipeline.Pipeline, xgboost.XGBModel, xgboost.Booster, torch.nn.Module, torch.jit.ScriptModule, tensorflow.Module, keras.Model, base.BaseEstimator, mlflow.pyfunc.PyFuncModel, transformers.Pipeline, sentence_transformers.SentenceTransformer, HuggingFacePipelineModel]])¶
- Bases: - object- Context for a custom model storing paths to file artifacts and model object references. - Keyword argument values can be string file paths or supported in-memory models. Paths and model references can be accessed with dictionary access methods in the custom model. - For example, in a custom model with context=ModelContext(my_file=’my_file.pkl’, my_model=my_model), the filepath and model reference can be accessed with self.context[‘my_file’] and self.context[‘my_model’] in the inference and init methods. - The use of artifacts and model_refs arguments is deprecated. Set keyword arguments directly instead. - artifacts¶
- A dictionary mapping the name of the artifact to its path. 
 - model_refs¶
- A dictionary mapping the name of the sub-model to its ModelRef object. 
 - Initialize the model context. - Parameters:
- artifacts – A dictionary mapping the name of the artifact to its currently available path. Defaults to None. 
- models – A dictionary mapping the name of the sub-model to the corresponding model object. Defaults to None. 
- **kwargs – Additional keyword arguments to be used as artifacts or models. 
 
- Raises:
- ValueError – Raised when the keyword argument is used as artifacts or models. 
- ValueError – Raised when the artifact name is duplicated. 
- ValueError – Raised when the model name is duplicated. 
 
 - Methods - model_ref(name: str) ModelRef¶
- Get a ModelRef object of a sub-model containing the name and model object, allowing direct method calls. - Parameters:
- name – The name of the sub-model. 
- Returns:
- The ModelRef object representing the sub-model. 
 
 - path(key: str) str¶
- Get the actual path to a specific artifact. This could be used when defining a Custom Model to retrieve
- artifacts. 
 - Parameters:
- key – The name of the artifact. 
- Returns:
- The absolute path to the artifact.