# Create custom model proxies for external models
URL: https://docs.datarobot.com/en/docs/workbench/nxt-registry/nxt-model-workshop/nxt-ext-model-proxy.html

> Create custom model proxies for external models in the 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 [Workshop](https://docs.datarobot.com/en/docs/workbench/nxt-registry/nxt-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/workbench/nxt-registry/nxt-model-directory/nxt-compliance-doc.html), [challenger analysis](https://docs.datarobot.com/en/docs/workbench/nxt-console/nxt-mitigation/nxt-challengers.html), and [custom model tests](https://docs.datarobot.com/en/docs/workbench/nxt-registry/nxt-model-workshop/nxt-test-custom-model.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 modelandassemble the modelfiles in the 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/workbench/nxt-registry/nxt-model-workshop/nxt-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 [Workshop](https://docs.datarobot.com/en/docs/workbench/nxt-registry/nxt-model-workshop/index.html). A proxy model contains the proxy code you created to connect with your external model.

To add a proxy model through the Model Workshop:

1. ClickRegistry > Workshop.
2. On theWorkshoptab, click+ Add model.
3. In theAdd a modelpanel, selectProxy, and then add the model information: FieldDescriptionModel nameA descriptive name for the custom model.Target typeThe type of prediction the model makes. Depending on the prediction type, you must configure additional settings:Binary: For a binary classification model, enter thePositive class labelandNegative class label. Then, optionally, enter aPrediction threshold.Regression: No additional settings.Time Series (Binary):Preview feature. For a binary classification model, enter thePositive class labeland theNegative class labeland configure thetime series settings. Then, optionally, enter aPrediction threshold.Time Series (Regression):Preview featureConfigure thetime series settings.Multiclass: For a multiclass classification model, enter or upload (.csv,.txt) theTarget classesfor your target, one class per line. To ensure that the classes are applied correctly to your model's predictions, enter classes in the same order as your model's predicted class probabilities.Text Generation:Premium feature. No additional settings.Anomaly Detection: No additional settings.Vector Database:Preview featureNo additional settings.Location:Premium feature. No additional settings.Agentic Workflow:Premium feature. No additional settings.MCP Server:Premium feature. No additional settings. An MCP server is a tool that allows you to expose the DataRobot API as a tool that can be used by agents.Target nameThe dataset's column name that the model will predict on. This field isn't available for multiclass or anomaly detection models.Optional settingsPrediction thresholdFor binary classification models, a decimal value ranging from 0 to 1. Any prediction score exceeding this value is assigned to the positive class.LanguageThe programming language used to build the model.DescriptionA description of the model's contents and purpose.
4. After completing the fields, clickAdd model.

## Assemble a proxy model

To assemble the proxy model, bring the proxy code you created to connect with your external model into the Workshop and ensure the proxy model has network access.

1. On theAssembletab, navigate to theEnvironmentsection and select a model environment from theBase environmentdropdown menu. Model environmentsThe model environment is used fortestingand deploying the custom model. TheBase Environmentdropdown list includesdrop-in model environmentsand anycustom environmentsthat you can create. By default, the custom model uses the most recent version of the selected environment with a successful build.
2. To populate theDependenciessection, you can upload arequirements.txtfile in theFilessection, allowing DataRobot to build the optimal image.
3. In theFilessection, add the required proxy model files. If you aren't pairing the model with adrop-in environment, this includes the custom model environment requirements and astart_server.shfile. You can add files in several ways: ElementDescription1FilesDrag files into the group box for upload.2Choose from sourceClick to browse forLocal Filesor aLocal Folder. Topull files from a remote repository, use theUploadmenu.3UploadClick to browse forLocal Filesor aLocal Folderor to pull files from aremote repository.4CreateCreate a new file, empty or as a template, and save it to the custom model:Create model-metadata.yaml: Creates a basic, editable example of a runtime parameters file.Create blank file: Creates an empty file. Click the edit icon () next toUntitledto provide a file name and extension, then add your custom contents. File considerationsIf you add a new file with the same name as an existing file, when you clickSave, the old file is replaced in theFilessection. When you add files from aLocal Folder, make sure the model file is already at the root of the custom model. Uploaded folders are 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.
4. On theAssembletab, in theSettingssection, next toResources, click ()EditandsetNetwork accesstoPublic.
5. If you provided runtime parameters in the model metadata, after you build the environment and create a new version, you canconfigure those parameters in theRuntime parameterssection.
6. Finally, you canregister the custom modelto create a deployable proxy model.

---
