|
1 | 1 | # Overview |
2 | 2 |
|
3 | | -| Developed by | Guardrails AI | |
| 3 | +| Developed by | Jonathan Bennion | |
4 | 4 | | --- | --- | |
5 | | -| Date of development | Feb 15, 2024 | |
| 5 | +| Date of development | Mar 29, 2024 | |
6 | 6 | | Validator type | Format | |
7 | | -| Blog | | |
8 | 7 | | License | Apache 2 | |
9 | 8 | | Input/Output | Output | |
10 | 9 |
|
11 | | -## Description |
12 | | - |
13 | | -### Intended Use |
14 | | -This validator is a template for creating other validators, but for demonstrative purposes it ensures that a generated output is the literal `pass`. |
| 10 | +# Description |
| 11 | +This bias check format validator ensures textual outputs do not contain biased language towards specific demographics, such as race, gender, sex, religion, ethnicity. |
| 12 | + |
| 13 | +## Intended Use |
| 14 | +This validator can be used to ensure fairness of model output across various demographic groups. |
15 | 15 |
|
16 | | -### Requirements |
| 16 | +## Requirements |
17 | 17 |
|
18 | 18 | * Dependencies: |
19 | | - - guardrails-ai>=0.4.0 |
| 19 | + - guardrails-ai>=0.4.0 |
| 20 | + - dbias>=0.1.0 |
| 21 | + |
| 22 | +* Dev Dependencies: |
| 23 | + - pytest |
| 24 | + - pyright |
| 25 | + - ruff |
20 | 26 |
|
21 | 27 | * Foundation model access keys: |
22 | | - - OPENAI_API_KEY |
| 28 | + - Dependent on the use case (rephrase if unclear) |
| 29 | + |
23 | 30 |
|
24 | | -## Installation |
| 31 | +# Installation |
25 | 32 |
|
26 | 33 | ```bash |
27 | | -$ guardrails hub install hub://guardrails/validator_template |
| 34 | +$ guardrails hub install hub://guardrails/bias_check |
28 | 35 | ``` |
29 | 36 |
|
30 | | -## Usage Examples |
| 37 | +# Usage Examples |
31 | 38 |
|
32 | | -### Validating string output via Python |
| 39 | +## Validating string output via Python |
33 | 40 |
|
34 | 41 | In this example, we apply the validator to a string output generated by an LLM. |
35 | 42 |
|
36 | 43 | ```python |
37 | 44 | # Import Guard and Validator |
38 | | -from guardrails.hub import ValidatorTemplate |
| 45 | +from guardrails.hub import BiasCheck |
39 | 46 | from guardrails import Guard |
40 | 47 |
|
41 | 48 | # Setup Guard |
42 | | -guard = Guard().use( |
43 | | - ValidatorTemplate |
| 49 | +guard = Guard.use( |
| 50 | + BiasCheck() |
44 | 51 | ) |
45 | 52 |
|
46 | | -guard.validate("pass") # Validator passes |
47 | | -guard.validate("fail") # Validator fails |
48 | | -``` |
49 | | - |
50 | | -### Validating JSON output via Python |
51 | | - |
52 | | -In this example, we apply the validator to a string field of a JSON output generated by an LLM. |
53 | | - |
54 | | -```python |
55 | | -# Import Guard and Validator |
56 | | -from pydantic import BaseModel, Field |
57 | | -from guardrails.hub import ValidatorTemplate |
58 | | -from guardrails import Guard |
59 | | - |
60 | | -# Initialize Validator |
61 | | -val = ValidatorTemplate() |
62 | | - |
63 | | -# Create Pydantic BaseModel |
64 | | -class Process(BaseModel): |
65 | | - process_name: str |
66 | | - status: str = Field(validators=[val]) |
67 | | - |
68 | | -# Create a Guard to check for valid Pydantic output |
69 | | -guard = Guard.from_pydantic(output_class=Process) |
70 | | - |
71 | | -# Run LLM output generating JSON through guard |
72 | | -guard.parse(""" |
73 | | -{ |
74 | | - "process_name": "templating", |
75 | | - "status": "pass" |
76 | | -} |
77 | | -""") |
78 | | -``` |
79 | | - |
80 | | -# API Reference |
81 | | - |
82 | | -**`__init__(self, on_fail="noop")`** |
83 | | -<ul> |
84 | | -Initializes a new instance of the ValidatorTemplate class. |
85 | | - |
86 | | -**Parameters** |
87 | | -- **`arg_1`** *(str)*: A placeholder argument to demonstrate how to use init arguments. |
88 | | -- **`arg_2`** *(str)*: Another placeholder argument to demonstrate how to use init arguments. |
89 | | -- **`on_fail`** *(str, Callable)*: The policy to enact when a validator fails. If `str`, must be one of `reask`, `fix`, `filter`, `refrain`, `noop`, `exception` or `fix_reask`. Otherwise, must be a function that is called when the validator fails. |
90 | | -</ul> |
91 | | -<br/> |
92 | | - |
93 | | -**`validate(self, value, metadata) -> ValidationResult`** |
94 | | -<ul> |
95 | | -Validates the given `value` using the rules defined in this validator, relying on the `metadata` provided to customize the validation process. This method is automatically invoked by `guard.parse(...)`, ensuring the validation logic is applied to the input data. |
96 | | - |
97 | | -Note: |
98 | | - |
99 | | -1. This method should not be called directly by the user. Instead, invoke `guard.parse(...)` where this method will be called internally for each associated Validator. |
100 | | -2. When invoking `guard.parse(...)`, ensure to pass the appropriate `metadata` dictionary that includes keys and values required by this validator. If `guard` is associated with multiple validators, combine all necessary metadata into a single dictionary. |
101 | | - |
102 | | -**Parameters** |
103 | | -- **`value`** *(Any)*: The input value to validate. |
104 | | -- **`metadata`** *(dict)*: A dictionary containing metadata required for validation. Keys and values must match the expectations of this validator. |
105 | | - |
106 | | - |
107 | | - | Key | Type | Description | Default | |
108 | | - | --- | --- | --- | --- | |
109 | | - | `key1` | String | Description of key1's role. | N/A | |
110 | | -</ul> |
| 53 | +guard.validate("The movie was great!") # Validator passes |
| 54 | +guard.validate("Why do men always think the movie was great?") # Validator fails |
0 commit comments