Custom Policy with Nemotron 3.5 Content Safety NIM
Learn how to restrict conversations to allowed topics using a custom policy with Nemotron 3.5 Content Safety.
A custom policy lets you define your own safety rules for a specific use case, for example limiting a customer service bot to company-related topics. The Nemotron 3.5 Content Safety NIM supports two mutually exclusive custom-field modes: custom_policy (Mode A) accepts free-text behavioral instructions and replaces the built-in safety behavior entirely; custom_taxonomy (Mode B) accepts a structured list of harm categories that replaces the built-in 24-category taxonomy. This tutorial uses custom_policy. To apply both the built-in taxonomy and a custom policy simultaneously, use two separate model entries in config.yml: one with request_categories: "/categories" for the built-in taxonomy and one with custom_policy for your rules.
By following this tutorial, you learn how to configure a custom policy and interact with both on-topic and off-topic requests.
Prerequisites
- The NVIDIA NeMo Guardrails library installed.
- A personal NVIDIA API key generated on build.nvidia.com.
Configure Guardrails
-
Create a configuration directory.
-
Save the following as
config/config.yml.The
custom_policyfield is passed to the model’s chat template as free-text behavioral instructions (Mode A). The model applies your policy and returnsUser Safety: safeorUser Safety: unsafe. Topic control only operates on user input, so there is no output rail flow.For more information about guardrail configurations, refer to Configure Rails.
-
Save the following as
config/prompts.yml.Nemotron 3.5 Content Safety builds the full safety prompt server-side, including your
custom_policy. Theprompts.ymlpasses only the user turn to classify. Thenemotron_content_safety_parse_prompt_safetyoutput parser reads theUser Safety: safe/unsafeverdict line.
Run the Guardrails Chat Application
-
Set the NVIDIA_API_KEY environment variable.
-
Run the interactive chat application.
-
Enter an off-topic request.
The custom policy prohibits questions about politics. The content safety input rail detects a policy violation and returns the refusal text. The main model is never called.
-
Enter an on-topic request.
This request is within the allowed topics, so the content safety rail passes it through to the main model.
Import the NeMo Guardrails Library in Python
-
Install the IPython REPL and run it.
-
Load the guardrails configuration you created earlier.
-
Verify the guardrails with an off-topic political question.
The content safety rail blocks the request.
-
Verify the guardrails with an on-topic question.
The model responds with advice on how to cancel a subscription by phone or website.
Deploy Nemotron 3.5 Content Safety NIM Locally
This section shows how to run the Nemotron 3.5 Content Safety model locally while still using the build.nvidia.com hosted main model. The prerequisites are:
- The NeMo Guardrails library installed.
- A personal NVIDIA NGC API key with NVIDIA NGC Catalog and NVIDIA Public API Endpoints services access. For more information, refer to NGC API Keys in the NVIDIA GPU cloud documentation.
- Docker installed.
- NVIDIA Container Toolkit installed.
- GPUs meeting the memory requirements for the Nemotron 3.5 Content Safety NIM.
To run the Nemotron 3.5 Content Safety NIM in a Docker container, follow these steps:
-
Update the
config.ymlfile to point to a local NIM deployment. Add abase_urlfield underparameters: -
Start the Nemotron 3.5 Content Safety NIM Docker container.
-
Export your personal NGC API key to an environment variable.
Log in to the NGC registry.
-
Download the container.
-
Create a model cache directory on the host machine.
-
Run the container with the cache directory mounted.
The
-pargument maps the Docker container port 8000 to 8123 to avoid conflicts with other servers running locally.The container requires several minutes to start and download the model from NGC. Monitor progress with
docker logs nemotron-content-safety. -
Confirm the service is ready.
This returns the following response.
-
-
Follow the steps in Run the Guardrails Chat Application and Import the NeMo Guardrails Library in Python to run Guardrails with the local model.