Large Language Models (LLMs) are ubiquitous in various applications such as chat applications, voice assistants, travel agents, and call centers. As new LLMs are released, they improve their response generation. However, people are increasingly using ChatGPT and other LLMs, which may provide prompts with personal identifiable information or toxic language. To protect against these types of data, a library called Guardrails-AI is being explored. This library aims to address these issues by providing a secure and efficient way to generate responses.
This article was published as a part of the Data Science Blogathon.
Guardrails-AI is an open-source project allowing us to build Responsible and Reliable AI applications with Large Language Models. Guardrails-AI applies guardrails both to the input User Prompts and the Responses generated by the Large Language Models. It even supports for generation of structured output straight from the Large Language Models.
Guardrails-AI uses various guards to validate User Prompts, which often contain Personal Identifiable Information, Toxic Language, and Secret Passwords. These validations are crucial for working with closed-source models, which may pose serious data security risks due to the presence of PII data and API Secrets. Guardrails also checks for Prompt Injection and Jailbreaks, which hackers may use to gain confidential information from Large Language Models. This is especially important when working with closed-source models that are not locally running.
On the other hand, guardrails can be even applied to the responses generated by the Large Language Models. Sometimes, Large Language Models generate outputs that might contain toxic language, or the LLM might hallucinate the answer or it may include competitor information in its generation. All these must be validated before the response can be sent to the end user. So guardrails come with different Components to stop them.
Guardrails comes with Guardrails Hub. In this Hub, different Components are developed by the open-source community. Each Component is a different Validator, which validates either the input Prompt or the Large Language Model answer. We can download these validators and work with them in our code.
In this section, we will get started with the Guardrails AI. We will start by downloading the Guardrails AI. For this, we will work with the following code.
!pip install -q guardrails-ai
The above command will download and install the guardrails-ai library for Python. The guardrails-ai contains a hub where there are many individual guardrail Components that can be applied to Sser Prompts and the Large Language Model generated answers. Most of these Components are created by the open-source community.
To work with these Components from the Gaurdrails Hub, we need to sign up to the Gaurdrails Hub with our GitHub account. You can click the link here(https://hub.guardrailsai.com/) to sign up for Guardrails Hub. After signing up, we get a token, which we can pass to guardrails configured to work with these Components.
Now we will run the below command to configure our Guardrails.
!guardrails configure
Before running the above command, we can go to this link https://hub.guardrailsai.com/tokens to get the API Token. Now when we run this command, it prompts us for an API token, and the token we have just received, we will pass it here. After passing the token, we will get the following output.
We see that we have successfully logged in. Now we can download different Components from the Guardrails Hub.
Let’s start by importing the toxic language detector:
!guardrails hub install hub://guardrails/toxic_language
The above will download the Toxic Language Component from the Guardrails Hub. Let us test it through the below code:
from guardrails.hub import ToxicLanguage
from guardrails import Guard
guard = Guard().use(
ToxicLanguage, threshold=0.5,
validation_method="sentence",
on_fail="exception")
guard.validate("You are a great person. We work hard every day
to finish our tasks")
Running the code will produce the following above output. We get a ValidationOutcome object that contains different fields. We see that the validation_passed field is set to True, meaning that our input has passed the toxic language validation.
Now let us try with some toxic inputs:
try:
guard.validate(
"Please look carefully. You are a stupid idiot who can't do \
anything right. You are a good person"
)
except Exception as e:
print(e)
Here above, we have given a toxic input. We have enclosed the validate() function inside the try-except block because this will produce an exception. From running the code and observing the output, we did see that an exception was generated and we see a Validation Failed Error. It was even able to output the particular sentence where the toxicity is present.
One of the necessary things to perform before sending a User Prompt to the LLM is to detect the PII data present. Therefore we need to validate the User Prompt for any Personal Identifiable Information before passing it to the LLM.
Now let us download this Component from the Gaurdrails Hub and test it with the below code:
!guardrails hub install hub://guardrails/detect_pii
from guardrails import Guard
from guardrails.hub import DetectPII
guard = Guard().use(
DetectPII(
pii_entities=["EMAIL_ADDRESS","PHONE_NUMBER"]
)
)
result = guard.validate("Please send these details to my email address")
if result.validation_passed:
print("Prompt doesn't contain any PII")
else:
print("Prompt contains PII Data")
result = guard.validate("Please send these details to my email address \
[email protected]")
if result.validation_passed:
print("Prompt doesn't contain any PII")
else:
print("Prompt contains PII Data")
When working with LLMs for code generation, there will be cases where the users might input the API Keys or other crucial information within the code. These need to be detected before the text is passed to the closed-source Large Language Models through the internet. For this, we will download the following validator and work with it in the case.
!guardrails hub install hub://guardrails/secrets_present
Running this code produced the following output. We can see that in the first case, the validation_passed was set to True. Because in this User Prompt, there is no API Key or any such Secrets present. In the second User Prompt, the validation_passed is set to False. This is because, there is a secret key, i.e. the weather API key present in the User Prompt. Hence we see a validation failed error.
Guardrails-AI is an essential tool for building responsible and reliable AI applications with large language models (LLMs). It provides comprehensive protection against harmful content, personally identifiable information (PII), toxic language, and other sensitive data that could compromise the safety and security of users. Guardrails-AI offers an extensive range of validators that can be customized and tailored to suit the needs of different applications, ensuring data integrity and compliance with ethical standards. By leveraging the components available in the Guardrails Hub, developers can enhance the performance and safety of LLMs, ultimately creating a more positive user experience and mitigating risks associated with AI technology.
A. Guardrails-AI is an open-source library that enhances the safety and reliability of AI applications using large language models by validating both input prompts and LLM responses for toxic language, personally identifiable information (PII), secret keys, and other sensitive data.
A. Guardrails-AI can detect toxic language, PII (such as email addresses and phone numbers), secret keys, and other sensitive information in user prompts before they are sent to large language models.
A. The Guardrails Hub is an online repository of various validators and components created by the open-source community that can be used to customize and enhance the functionality of Guardrails-AI.
A. Guardrails-AI helps maintain ethical AI systems by validating input prompts and responses to ensure they do not contain harmful content, PII, or sensitive information, thereby upholding user privacy and safety standards.
A. Yes, Guardrails-AI offers various validators that can be customized and tailored to suit different applications, allowing developers to create robust guardrails for their AI projects.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.