Translate text without profanities
Note
To download this example as a Jupyter notebook, click here.
In this example, we will use Guardrails during the translation of a statement from another language to english. We will check whether the translated statement passes the profanity check or not.
Objective
We want to translate a statement from another languages to English and ensure the translated statement is profanity free.
Step 0: Setup
In order to run this example, you will need to install alt-profanity-check
package. You can do so by running the following commands:
Step 1: Create the RAIL Spec
Ordinarily, we would create an RAIL spec in a separate file. For the purposes of this example, we will create the spec in this notebook as a string following the RAIL syntax. For more information on RAIL, see the RAIL documentation. We will also show the same RAIL spec in a code-first format using a Pydantic model.
In this RAIL spec, we:
- Create an
output
schema that returns a single key-value pair. The key should be 'translated_statement', and the value should be the English translation of the given statement. The translated statement should not have any profanity.
First we create out custom Validator:
from profanity_check import predict
from guardrails.validators import Validator, register_validator, ValidationResult, PassResult, FailResult
from typing import Dict, Any
@register_validator(name="is-profanity-free", data_type="string")
class IsProfanityFree(Validator):
def validate(self, value: Any, metadata: Dict) -> Dict:
prediction = predict([value])
if prediction[0] == 1:
return FailResult(
error_message=f"Value {value} contains profanity language",
fix_value="",
)
return PassResult()
Next we define our RAIL spec either as XML:
rail_str = """
<rail version="0.1">
<output>
<string description="Translate the given statement into english language" format="is-profanity-free" name="translated_statement" on-fail-is-profanity-free="fix"></string>
</output>
<prompt>
Translate the given statement into english language:
${statement_to_be_translated}
${gr.complete_json_suffix}
</prompt>
</rail>
"""
Or as a Pydantic model:
from pydantic import BaseModel, Field
prompt = """
Translate the given statement into english language:
${statement_to_be_translated}
${gr.complete_json_suffix}
"""
class Translation(BaseModel):
translated_statement: str = Field(
description="Translate the given statement into english language",
validators=[IsProfanityFree(on_fail="fix")]
)
Note
In order to ensure the translated statement is profanity free, we use is-profanity-free
as the validator. This validator uses profanity_check
package.
Step 2: Create a Guard
object with the RAIL Spec
We create a gd.Guard
object that will check, validate and correct the output of the LLM. This object:
- Enforces the quality criteria specified in the RAIL spec.
- Takes corrective action when the quality criteria are not met.
- Compiles the schema and type info from the RAIL spec and adds it to the prompt.
From XML:
Or from our Pydantic model:
We see the prompt that will be sent to the LLM:
Here, statement_to_be_translated
is the the statement and will be provided by the user at runtime.
Step 3: Wrap the LLM API call with Guard
First, let's try translating a statement that doesn't have any profanity in it.
We can take a look at the output of the LLM and the validated output using the Guard's internal logs:
The guard
wrapper returns the raw_llm_respose (which is a simple string), and the validated and corrected output (which is a dictionary). We can see that the output is a dictionary with the correct schema and types.
Next, let's try translating a statement that has profanity in it. We see that the translated statement has been corrected to return an empty string instead of the translated statement.
This time around, when we look at the logs, we can see that the output of the LLM was filtered out because it did not pass the profanity check.