
Neural Modules (NeMo) Guardrails is an open-source toolkit developed by NVIDIA, to safeguard the development of LLMs by preventing harmful or offensive content. It provides features to control the LLMs and helps mitigate the risks of models by adding an essential layer of protection that assists organizations in building reliable systems without exposing the user to unwanted information.
This article explains how to set up the NeMo Guardrails for dolly-v2-3b LLM using the HuggingFace pipeline and control the response generation that matches the prompt provided.
To set up guardrails on your server, install the necessary dependency packages to infer the model.
Install the required packages
Below is what each package represents:
nemoguardrails: Provides the necessary modules to use NeMo Guardrails toolkit such as RailsConfig, register_llm_provider, and LLMRailslangchain: Import the HuggingFacePipeline that initializes the model in useTo set up Guardrails, import the necessary modules. Then, set the guardrails policies, initialize the dolly-v2-3b model with the HuggingFace pipeline, and infer it as described in the steps below.
In your Jupyter Lab interface, create a new Python3 Kernel Notebook
Import the necessary modules
Below is what each module represents:
lru_cache: It's used for memorization which is an optimization technique to improve the efficiency of functionsHuggingFacePipeline: Creates pipelines for pre-trained LLMs and NLP tasksdevice_count: Determines the number of available CUDA devicesget_llm_instance_wrapper: Gets an instance for a specified LLMregister_llm_provider: Specifies the source for LLM modelsLLMRails: Generates a response from a prompt by creating railsRailsConfig: Initializes the custom configurations you defined for response generationClick Run or press Ctrl + Enter to import the modules
In a new code cell, create the custom guardrails
The above code sets the YAML content used to define the model in use. It also defines the CoLang content where the guardrails for expected user prompts and model responses are listed. Then the conversation flow is defined to avoid misdirection and hallucination in the responses generated by the bot.
Edit the prompts in the above code and the response definitions along with flow definitions to match your use case. For the guardrails to work, be sure to follow the CoLang syntax.
Define the model function
The above command defines a function get_dolly_v2_3b_llm that configures the model, its temperatureandmax_lengthparameters. The model can either either run on a CUDA-enabled GPU or CPU. The function returns the initialized model withllm`.
Initiate the pipeline
The above command creates an LLM instance using the specified model and parameters. Then, the instance is registered with the source of the model for text generation tasks.
Initialize the custom configurations
The above command initializes the yaml_content and colang_content you defined earlier, then it creates the LLM Rails by passing config.
Generate response. Replace the prompt What do you think of the president with your desired text prompt
The above code asynchronously generates a response considering the guardrails you set up.
Output:
You have inferred the dolly-v2-3b model using the HuggingFace pipeline and applied custom guardrails using the NeMo toolkit. The guardrails control the model's response so that it does not go off-topic or hallucinate. This protects the user from harmful information.
For more information and implementation samples, please visit the following resources:
0 Comments
Be the first to comment and share your perspective with the community.