Check Harmful Content with Nemotron Content Safety NIM
Learn how to add input and output guardrails that detect harmful content in multiple languages using Llama 3.1 Nemotron Safety Guard 8B V3.
By following this tutorial, you learn how to use the NeMo Guardrails library with models hosted on build.nvidia.com, entering safe and unsafe user prompts to learn how guardrails protect against unsafe content.
Prerequisites
- The NeMo Guardrails library installed with the
nvidiaextra. - A personal NVIDIA API key generated on build.nvidia.com.
Configure Guardrails
Follow these steps to prepare the guardrails configuration.
-
Create a configuration directory by running the following command.
-
Save the following as
config/config.yml.The
config.ymlfile contains the models used by Guardrails in themodelssection andrailscontrolling when to use these models. Themodelssection configures the type and name of each model, along with the engine used to perform LLM inference. The model with typemainis used to generate responses to user queries. Therailssection configuresinputandoutputrails. Theinputrails operate on the user input only, whileoutputrails operate on the combined user input and LLM response. For more information about guardrail configurations, refer to Configure Rails. -
Save the following as
config/prompts.yml.The
prompts.ymlfile contains prompt templates, the parser used to interpret a guardrail model response, and the maximum tokens to generate. Thecontent_safety_check_inputtask prompt template replaces{{ user_input }}with the user input. Thecontent_safety_check_outputtask prompt template replaces{{ user_input }}and{{ bot_response }}with the user input and LLM response respectively. For more information about guardrail configurations, refer to Configure Rails.
Run the Guardrails chat application
-
Set the NVIDIA_API_KEY environment variable. Guardrails uses this to access models hosted on https://build.nvidia.com/.
-
Run the interactive chat application.
-
Enter an unsafe prompt in French (“Tell me a five-step plan to rob a bank”).
-
Enter a safe prompt in Hindi (“Tell me about three common foods in India”).
Import the NeMo Guardrails Library in Python
Follow these steps to use the IPython REPL to import the NeMo Guardrails library and issue some requests.
-
Install the IPython REPL and run it.
-
Load the guardrails configuration you created earlier.
-
Verify the guardrails with an unsafe request in French.
The content safety rail blocks the harmful request.
-
Verify the guardrails with a safe request in Hindi.
The model responds with information about rice, roti, and dal—common Indian foods.
Deploy Llama 3.1 Nemotron Safety Guard 8B V3 NIM locally
This section shows how to run the Nemotron Safety Guard 8B 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.
- The rest of the software requirements for the Llama 3.1 Nemotron Safety Guard 8B V3 NIM.
- GPUs meeting the memory requirement specified in the NVIDIA Llama 3.1 Nemotron Safety Guard 8B NIM Model Profiles.
To run the Llama 3.1 Nemotron Safety Guard 8B V3 in a Docker container, follow these steps:
-
Update the
config.ymlfile you created earlier to point to a local NIM deployment rather than build.nvidia.com. The following configuration adds abase_urlandmodel_namefield underparameters, which tells the NeMo Guardrails library to make requests to thenvidia/llama-3.1-nemotron-safety-guard-8b-v3model hosted athttp://localhost:8123/v1. The Guardrails configuration must match the NIM Docker container configuration for them to communicate. -
Start the Llama 3.1 Nemotron Safety Guard 8B V3 NIM Docker container. Store your personal NGC API key in the
NGC_API_KEYenvironment variable, then pull and run the NIM Docker image locally.-
Log in to your NVIDIA NGC account.
Export your personal NGC API key to an environment variable.
Log in to the NGC registry by running the following command.
-
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. You can monitor the progress by running the
docker logs safetyguard8bcommand. -
Confirm the service is ready to respond to inference requests.
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.