# Create custom model proxies for external models
URL: https://docs.datarobot.com/en/docs/classic-ui/mlops/deployment/custom-models/custom-model-workshop/ext-model-proxy.html

> Create custom model proxies for external models in the Custom Model Workshop.

# Create custom model proxies for external models

To create a custom model as a proxy for an external model, you can add a new proxy model to the [Custom Model Workshop](https://docs.datarobot.com/en/docs/classic-ui/mlops/deployment/custom-models/custom-model-workshop/index.html). A proxy model contains proxy code created to connect with an external model, allowing you to use features like [compliance documentation](https://docs.datarobot.com/en/docs/classic-ui/mlops/deployment/registry/reg-compliance.html), [challenger analysis](https://docs.datarobot.com/en/docs/api/dev-learning/python/mlops/challengers.html), and [custom model tests](https://docs.datarobot.com/en/docs/classic-ui/mlops/deployment/custom-models/custom-model-workshop/custom-model-test.html) with a model running on infrastructure outside DataRobot.

To create a proxy model:

1. (Optional)Add runtime parametersto the custom model through the model metadata (model-metadata.yaml).
2. Add proxy codeto the custom model through the custom model file (custom.py).
3. Create a proxy modelin the Custom Model Workshop.

## Add proxy code

The custom model you create as a proxy for an external model should contain custom code in the `custom.py` file to connect the [proxy model](https://docs.datarobot.com/en/docs/classic-ui/mlops/deployment/custom-models/custom-model-workshop/ext-model-proxy.html#create-a-proxy-model) with the externally-hosted model; this code is the proxy code. See the [custom model assembly](https://docs.datarobot.com/en/docs/api/code-first-tools/drum/index.html) documentation for more information on writing custom model code.

The proxy code in the `custom.py` file should do the following:

- Import the necessary modules and, optionally, theruntime parametersfrommodel-metadata.yaml.
- Connect the custom model to an external model via an HTTPS connection or the network protocol required by your external model.
- Request predictions and convert prediction data as necessary.

To simplify the reuse of proxy code, you can add [runtime parameters](https://docs.datarobot.com/en/docs/api/code-first-tools/drum/custom-model-runtime-parameters.html) through your model metadata in the `model-metadata.yaml` file:

```
# model-metadata.yaml
name: runtime-parameter-example
type: inference
targetType: regression
runtimeParameterDefinitions:
- fieldName: endpoint
  type: string
  description: The name of the endpoint.
- fieldName: API_KEY
  type: credential
  description: The HTTP basic credential containing the endpoint's API key in the password field (the username field is ignored).
```

If you define runtime parameters in the model metadata, you can import them into the `custom.py` file to use in your proxy code. After importing these parameters, you can assign them to variables in your proxy code. This allows you to create a prediction request to connect to and retrieve prediction data from the external model. The following example outlines the basic structure of a `custom.py` file:

```
# custom.py
# Import modules required to make a prediction request.
import json
import ssl
import urllib.request
import pandas as pd
# Import SimpleNamespace to create an object to store runtime parameter variables.
from types import SimpleNamespace
# Import RuntimeParameters to use the runtime parameters set in the model metadata.
from datarobot_drum import RuntimeParameters

# Override the default load_model hook to read the runtime parameters.
def load_model(code_dir):
    # Assign runtime parameters to variables.
    api_key = RuntimeParameters.get("API_KEY")["password"]
    endpoint = RuntimeParameters.get("endpoint")

    # Create scoring endpoint URL.
    url = f"https://{endpoint}.example.com/score"

    # Return an object containing the variables necessary to make a prediction request.
    return SimpleNamespace(**locals())

# Write proxy code to request and convert scoring data from the external model.
def score(data, model, **kwargs):
    # Call make_remote_prediction_request.
    # Convert prediction data as necessary.

def make_remote_prediction_request(payload, url, api_key):
    # Connect to the scoring endpoint URL.
    # Request predictions from the external model.
```

## Create a proxy model

To create a custom model as a proxy for an external model, you can add a new proxy model to the [Custom Model Workshop](https://docs.datarobot.com/en/docs/classic-ui/mlops/deployment/custom-models/custom-model-workshop/index.html). A proxy model contains the proxy code you created to connect with your external model, allowing you to use features like [compliance documentation](https://docs.datarobot.com/en/docs/classic-ui/mlops/deployment/registry/reg-compliance.html), [challenger analysis](https://docs.datarobot.com/en/docs/api/dev-learning/python/mlops/challengers.html), and [custom model tests](https://docs.datarobot.com/en/docs/classic-ui/mlops/deployment/custom-models/custom-model-workshop/custom-model-test.html) with a model running on infrastructure outside DataRobot.

To add a Proxy model through the Custom Model Workshop:

1. ClickModel Registry > Custom Model Workshop.
2. On theModelstab, click+ Add new model.
3. In theAdd Custom Inference Modeldialog box, selectProxy, and then add the model information. FieldDescriptionModel nameName the custom model.Target type / Target nameSelect the target type (binary classification,regression,multiclass classification,anomaly detection, ortext-generation(premium feature)) and enter the name of the target feature.Positive class label / Negative class labelThese fields only display for binary classification models. Specify the value to be used as the positive class label and the value to be used as the negative class label.For a multiclass classification model, these fields are replaced by a field to enter or upload the target classes in.csvor.txtformat.
4. ClickShow Optional Fieldsand, if necessary, enter aPrediction threshold, theLanguageused to build the model, and aDescription.
5. After completing the fields, clickAdd Custom Model.
6. In theAssembletab, underModel Environmenton the right, select a model environment by clicking theBase Environmentdropdown menu on the right and selecting an environment. The model environment is used fortestinganddeployingthe custom model. NoteTheBase Environmentpulldown menu includesdrop-in model environments, if any exist, as well ascustom environmentsthat you can create.
7. UnderModelon the left, add proxy model content by dragging and dropping files or browsing. Alternatively, select aremote integrated repository. If you clickBrowse local file, you have the option of adding aLocal Folder. The local folder is for dependent files and additional assets required by your model, not the model itself. Even if the model file is included in the folder, it will not be accessible to DataRobot unless the file exists at the root level. The root file can then point to the dependencies in the folder. NoteYou must also upload the model requirements and astart_server.shfile to your model's folder unless you are pairing the model with adrop-in environment.
8. On theAssembletab, next toResource settings, click the edit icon () to activate the requiredNetwork accessfor the proxy model.
9. If you provide runtime parameters in the model metadata, after you build the environment and create a new version, you can configure the parameters on theAssembletab underRuntime Parameters.
10. Finally, you canregister the custom modelto create a proxy model you can use to generatecompliance documentation. You can then deploy the proxy model to set upchallenger analysisand runcustom model testson the external model.

---
