Skip to content

Commit 2d2fb02

Browse files
authored
Merge pull request #656 from guardrails-ai/karan/readme-updates
Minor fixes to readme
2 parents 5ca7be2 + bfd4386 commit 2d2fb02

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

README.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,23 @@ pip install guardrails-ai
6464
3. Create a Guard from the installed guardrail.
6565

6666
```python
67-
# Import Guard and Validator
68-
from guardrails.hub import RegexMatch
6967
from guardrails import Guard
68+
from guardrails.hub import RegexMatch
7069
71-
# Initialize the Guard with
72-
val = Guard().use(
73-
RegexMatch(regex="^[A-Z][a-z]*$")
70+
guard = Guard().use(
71+
RegexMatch, regex="\(?\d{3}\)?-? *\d{3}-? *-?\d{4}", on_fail="exception"
7472
)
7573
76-
guard.parse("Caesar") # Guardrail Passes
77-
guard.parse("Caesar is a great leader") # Guardrail Fails
74+
guard.validate("123-456-7890") # Guardrail passes
75+
76+
try:
77+
guard.validate("1234-789-0000") # Guardrail fails
78+
except Exception as e:
79+
print(e)
80+
```
81+
Output:
82+
```console
83+
Validation failed for field with errors: Result must match \(?\d{3}\)?-? *\d{3}-? *-?\d{4}
7884
```
7985
4. Run multiple guardrails within a Guard.
8086
First, install the necessary guardrails from Guardrails Hub.
@@ -87,18 +93,32 @@ pip install guardrails-ai
8793
Then, create a Guard from the installed guardrails.
8894

8995
```python
90-
from guardrails.hub import RegexMatch, ValidLength
9196
from guardrails import Guard
97+
from guardrails.hub import CompetitorCheck, ToxicLanguage
9298
93-
guard = Guard().use(
94-
RegexMatch(regex="^[A-Z][a-z]*$"),
95-
ValidLength(min=1, max=32)
99+
guard = Guard().use_many(
100+
CompetitorCheck(["Apple", "Microsoft", "Google"], on_fail="exception"),
101+
ToxicLanguage(threshold=0.5, validation_method="sentence", on_fail="exception"),
96102
)
97103
98-
guard.parse("Caesar") # Guardrail Passes
99-
guard.parse("Caesar is a great leader") # Guardrail Fails
104+
guard.validate(
105+
"""An apple a day keeps a doctor away.
106+
This is good advice for keeping your health."""
107+
) # Both the guardrails pass
108+
109+
try:
110+
guard.validate(
111+
"""Shut the hell up! Apple just released a new iPhone."""
112+
) # Both the guardrails fail
113+
except Exception as e:
114+
print(e)
100115
```
116+
Output:
117+
```console
118+
Validation failed for field with errors: Found the following competitors: [['Apple']]. Please avoid naming those competitors next time, The following sentences in your response were found to be toxic:
101119
120+
- Shut the hell up!
121+
```
102122
103123
### Use Guardrails to generate structured data from LLMs
104124
@@ -133,7 +153,7 @@ validated_output, *rest = guard(
133153
engine="gpt-3.5-turbo-instruct"
134154
)
135155
136-
print(f"{validated_output}")
156+
print(validated_output)
137157
```
138158
139159
This prints:

0 commit comments

Comments
 (0)