|
| 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