Skip to content

Commit 394a515

Browse files
regex chapter
1 parent 48ad5af commit 394a515

File tree

2 files changed

+155
-6
lines changed

2 files changed

+155
-6
lines changed

docs/assets/invariant.css

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ span.parser-badge::before {
513513
}
514514

515515
.builtin-badge:hover::after {
516-
content: 'BUILTIN DESCRIPTION';
516+
content: 'Built-in functions are pre-defined functions that are available for use in your code without requiring any additional imports.';
517517
}
518518

519519
.parser-badge:hover::after,
@@ -859,6 +859,10 @@ ul.md-nav__list {
859859
font-size: 12pt;
860860
}
861861

862+
.admonition ul li, .admonition ol li {
863+
font-size: 12pt !important;
864+
}
865+
862866
.admonition p {
863867
font-size: 12pt !important;
864868
}
@@ -1140,19 +1144,20 @@ strong .twemoji {
11401144
padding: 10pt;
11411145
background-color: #f0f0f0;
11421146
border-radius: 10pt;
1143-
padding-bottom: 20pt;
1147+
padding-bottom: 40pt;
11441148
position: relative;
11451149
}
11461150

1147-
.format-explainer figcaption {
1151+
.md-typeset .format-explainer figcaption {
11481152
position: absolute;
1149-
bottom: 0pt;
1150-
left: 50%;
1151-
transform: translateX(-50%);
1153+
bottom: 5pt;
1154+
left: 0pt;
11521155
font-size: 10pt;
11531156
color: #666;
11541157
z-index: 10;
1158+
display: block;
11551159
text-align: center;
1160+
max-width: 100%;
11561161
width: 100%;
11571162
}
11581163

docs/guardrails/regex-filters.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Regex Filters
2+
3+
<div class='subtitle'>Use regular expressions to filter messages</div>
4+
5+
One simple, yet effective method to constrain your agent is to apply regular expressions to match undesired content and substrings.
6+
7+
This is a powerful tool, specifically to fight plain text risks, e.g. to prevent certain URLs, names or other patterns from being included in the agent's context.
8+
9+
10+
!!! danger "Plain Text Content Risks"
11+
Agents that operate on plain text content are suceptible to generating harmful, or misleading content, which you as the operator may be liable for. An insecure agent could:
12+
13+
- Generate phishing URLs that are advertised under your brand authority
14+
- Reference competitors or their websites in responses and internal reasoning
15+
- Produce content in unsupported output formats, leading to visual defects in your application
16+
- Use URL smuggling to bypass security measures (e.g. to leak information via URLs)
17+
18+
19+
20+
21+
## match <span class="builtin-badge"/>
22+
```python
23+
def match(
24+
pattern: str,
25+
content: str
26+
) -> bool
27+
```
28+
Detector to match a regular expression pattern in a message.
29+
30+
**Parameters**
31+
32+
| Name | Type | Description |
33+
|-------------|--------|----------------------------------------|
34+
| `pattern` | `str` | The regular expression pattern to match. |
35+
| `content` | `str` | The content to match the pattern against. |
36+
37+
**Returns**
38+
39+
Returns `True` if the pattern matches the content, `False` otherwise.
40+
41+
Wraps `re.match` from Python's standard library.
42+
43+
By default only matches content at the beginning of the string. To match anywhere in the string, use `.*` at the beginning of the pattern.
44+
45+
### Examples
46+
47+
**Example:** Checking if a message contains a URL.
48+
49+
```guardrail
50+
raise "Must not link to example.com" if:
51+
(msg: Message)
52+
match("https?://[^\s]+", msg.content)
53+
```
54+
```example-trace
55+
[
56+
{
57+
"role": "user",
58+
"content": "Respond with http://example.com"
59+
},
60+
{
61+
"role": "assistant",
62+
"content": "http://example.com"
63+
}
64+
]
65+
```
66+
67+
**Example:** Checking if a message contains a competitor's name.
68+
69+
```guardrail
70+
raise "Must not mention competitor" if:
71+
(msg: Message)
72+
match(".*[Cc]ompetitor.*", msg.content)
73+
```
74+
```example-trace
75+
[
76+
{
77+
"role": "user",
78+
"content": "What do you think about competitor?"
79+
},
80+
{
81+
"role": "assistant",
82+
"content": "I dont' know what you are talking about"
83+
}
84+
]
85+
```
86+
87+
88+
## find <span class="builtin-badge"/>
89+
```python
90+
def find(
91+
pattern: str,
92+
content: str
93+
) -> List[str]
94+
```
95+
96+
Detector to find all occurrences of a regular expression pattern in a message.
97+
98+
**Parameters**
99+
100+
| Name | Type | Description |
101+
|--------------|--------|----------------------------------------|
102+
| `pattern` | `str` | The regular expression pattern to find.|
103+
| `content` | `str` | The content to find the pattern in. |
104+
105+
**Returns**
106+
107+
The list of all occurrences of the pattern in the content.
108+
109+
### Examples
110+
111+
**Example:** Iterating over all capitalized words and checking if they are in a list of names.
112+
113+
```guardrail
114+
raise "must not send emails to anyone but 'Peter' after seeing the inbox" if:
115+
(msg: Message)
116+
(name: str) in find("[A-Z][a-z]*", msg.content)
117+
name in ["Peter", "Alice", "John"]
118+
```
119+
```example-trace
120+
[
121+
{
122+
"role": "user",
123+
"content": "Reply to Peter's message and then Alice's"
124+
}
125+
]
126+
```
127+
128+
**Example:** Checking all URLs in a message
129+
```guardrail
130+
raise "Must not link to example.com" if:
131+
(msg: Message)
132+
(url: str) in find("https?://[^\s]+", msg.content)
133+
url in ["http://example.com", "https://example.com"]
134+
```
135+
```example-trace
136+
[
137+
{
138+
"role": "user",
139+
"content": "Go to http://example.com and then https://secure-example.com"
140+
}
141+
]
142+
```
143+
144+
Here, we quantify over all matches returned by `find`. This means, if any of the matches satisfies the extra condition, the guardrail will raise.

0 commit comments

Comments
 (0)