Skip to main content
In this example we will use Guardrails to summarize text in terms of length, quality and output read time.
To download this example as a Jupyter notebook, click here.
In this example, we will use Guardrails in the summarization of a text document. We will check whether the summarized document has a high semantic similarity with the original document. We will ensure the output meets a certain range of length and read time.

Setup

In order to check semantic similarity we will need the numpy package via the command below. We will also install the validators we intend to use.

Step 1: Load data and create Pydantic model

Load our text with the code below:
Next we can define our return output with a pydantic model:

Step 2: Create Guard from pydantic

The guard we create will:
  1. Enforce reading time
  2. Enforce length
  3. Enforce similarity

Step 3: Call LLM via guard()

We use the tools API to ensure our data is returned in a structured form.
We can see the step-wise history of the Guard object below:
The guard wrapper returns the raw_llm_response (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. Now let’s try a model that’s not as proficient at summarization and we can see the output is filtered and validation has failed. The final validated output is None due to the failed validation.
We can see the step wise history of the guard execution below: