Learn more about the need for remote validators in our concepts doc.
Step 1: Pick a validator
First, choose a validator that you want to use. You can browse a list of all the Guardrails validators on Guardrails Hub.We suggest picking the “ML” filter on the left side of the page in the “Infrastructure Requirements” section to find validators that use models.
Step 2: Find and download the validator model
The validator model is referenced in the validator implementation, as well as in the README. As we can see, the ToxicLanguage validator currently uses the Detoxify model. In order to use this, we need to install the necessary libraries:Step 3: Wrap the model with FastAPI and run the server
Before implementing the API, we first need to define what the API expects as input and what it will return as output so that the API and server can effectively communicate with each other.The Guardrails standard inference endpoint schema
Guardrails AI recommends using a similar schema for inputs and outputs across all APIs fronting machine learning models. This approach standardizes the implementation for extracting model inference results out of different models in validators. For the input, let’s make the model take in:"text" contains the text we want to check for toxic language and "threshold" contains the confidence threshold for determining toxicity. If the model predicts a toxic level higher than the threshold, then the text is considered toxic.
For the output, let’s make it look like:
Implementing the model
Now it’s time to write ourapp.py to set up the FastAPI server. You can find the FastAPI setup for ToxicLanguage here.
In general, our code for the server is pretty simple. The server setup is quite similar for any validator we choose to use.
We first load in the Detoxify model and create a validate endpoint where we do two main things:
- Extract the text that we want to validate as well as the threshold that we need to figure out the toxicity of our text
- Call the detoxify model on the text and return the output in the format described above
uvicorn app:app --reload and now our server is running on https://127.0.0.1:8000:
Step 4: Configure our Guard
Now that we have our server up and running, we have to write a guard that calls the ToxicLanguage validator using our new API. With Guardrails, this is really simple:Guard and pass in the ToxicLanguage validator with the following parameters:
use_local=False: This tells Guardrails to use remote inference instead of running the ML model locally.on_fail="exception": If toxic language is detected, the guard will throw aValidationErrorexplaining which parts of the text are toxic.validation_endpoint="http://127.0.0.1:8000/validate": This passes in our FastAPI URL as the validation endpoint to the validator, which then makes a request to this URL to run the Detoxify model on the given text.
app.py in the repository, you can simply write the FastAPI or Flask code using one of the given app.py’s as reference.
Conclusion
You’ve learned how to host the ToxicLanguage model using FastAPI and integrate it directly with Guardrails. This setup can be used for various types of validators and ML models.Production considerations
- Make sure to wrap the application in a WSGI or ASGI server (like uvicorn or Gunicorn).
- Deploy on GPUs to accelerate model inference and increase performance.