Add Multimodal Content Safety Using a Vision Model
Learn how to add safety checks to images and text using Nemotron 3.5 Content Safety, a multimodal safety NIM that inspects both text and images in a single inference call.
Multimodal content safety rails run only on LLMRails.
The IORails engine has no equivalent.
For the rails that each engine runs, refer to Rail Engine Support.
By following this tutorial, you learn how to:
- Configure multimodal content safety rails for images and text.
- Use a vision model as LLM-as-a-Judge to evaluate content safety.
- Test with safe and unsafe image requests.
The NeMo Guardrails library supports multimodal content safety for input and output rails. You can provide images as base64-encoded data or URLs, depending on the model.
Ensure image size and prompt length do not exceed the model’s maximum context length.
Prerequisites
- The NVIDIA NeMo Guardrails library installed.
- A personal NVIDIA API key generated on build.nvidia.com.
- An OpenAI API key (for the main model used to generate responses).
Configure Guardrails
-
Create a configuration directory and add
config.yml.config.yml -
Add
prompts.yml.prompts.yml
Test Multimodal Safety
This example sends image requests and tests safety checks on a handgun image. The content safety rail uses Nemotron 3.5 Content Safety NIM to evaluate both the text and the image. The main model uses OpenAI to generate responses.
-
Set your API keys.
-
Install the IPython REPL and run it to interpret the Python code below.
-
Import libraries.
demo.py -
Load the configuration.
demo.py -
Fetch the image as base64.
The hosted NIM endpoint fetches image URLs server-side. Some image hosts (including Wikimedia) block non-browser HTTP clients with a 403. To avoid this, download the image locally and pass it as a base64 data URL instead.
demo.py -
Send a safe image reasoning request.
demo.py -
Send an unsafe request.
demo.py
Use Base64-Encoded Images
Some models such as Llama Vision require base64-encoded images instead of URLs.