You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`List[str]`| A list of extracted pieces of text from`data`. |
71
+
72
+
### Analyzing Text in Images
73
+
The `ocr` function is a <span class="parser-badge" size-mod="small"></span> so it returns the data found from parsing its content, in this case extracting text from an image. The extracted text can then be used for further detection, for example detecting a prompt injection in an image, like the example below.
74
+
75
+
**Example:** Image Prompt Injection Detection.
76
+
```python
77
+
from invariant.detectors import prompt_injection
78
+
from invariant.parsers import ocr
79
+
80
+
raise"Found Prompt Injection in Image"if:
81
+
(msg: Image)
82
+
ocr_results := ocr(msg)
83
+
prompt_injection(ocr_results)
84
+
```
85
+
<div class="code-caption"> The text extracted from the image can be checked using, for example, detectors.</div>
86
+
87
+
88
+
## image <span class="builtin-badge"/>
89
+
90
+
```python
91
+
def image(
92
+
content: Union[Content | List[Content]]
93
+
) -> List[Image]
94
+
```
95
+
Given some `Content`, this <span class="builtin-badge" size-mod="small"></span> extracts all images. This is useful when messages may contain mixed content.
|`List[Image]`| A list of extracted `Image`s from`content`. |
108
+
109
+
110
+
### Extracting Images
111
+
Some policies may wish to check images and text in specific ways. Using `image`and`text` we can create a policy that detects prompt injection attacks in user input, even when we allow users to submit images.
112
+
113
+
**Example:** Prompt Injection Detection in Both Images and Text
114
+
```python
115
+
from invariant.detectors import prompt_injection
116
+
from invariant.parsers import ocr
117
+
118
+
raise"Found Prompt Injection"if:
119
+
(msg: Message)
120
+
121
+
# Only check user messages
122
+
msg.role =='user'
123
+
124
+
# Use image function to get images
125
+
ocr_results := ocr(image(msg))
126
+
127
+
# Check both text and images
128
+
prompt_injection(text(msg))
129
+
prompt_injection(ocr_results)
130
+
```
131
+
<divclass="code-caption"> Extract specific content types from mixed-content messages.</div>
|`data`|`Union[str, List[str]]`| A single message or a list of messages to detect PIIin|
30
+
|`data`|`Union[str, List[str]]`| A single message or a list of messages to detect PIIin.|
31
31
|`entities`|`Optional[List[str]]`| A list of [PII entity types](https://microsoft.github.io/presidio/supported_entities/) to detect. Defaults to detecting all types. |
32
32
33
33
**Returns**
@@ -40,7 +40,7 @@ Detector to find personally indentifaible information in text.
40
40
The simplest usage of the `pii` function is to check against any message. The following example will raise an error ifany message in the trace contains PII.
41
41
42
42
**Example:** Detecting anyPIIinany message.
43
-
``` py
43
+
```python
44
44
from invariant.detectors import pii
45
45
46
46
raise"Found PII in message"if:
@@ -54,7 +54,7 @@ raise "Found PII in message" if:
54
54
You can also specify specific types of PII that you would like to detect, such as phone numbers, emails, or credit card information. The example below demonstrates how to detect credit card numbers in Messages.
55
55
56
56
**Example:** Detecting Credit Card Numbers.
57
-
```guardrail
57
+
```python
58
58
from invariant.detectors import pii
59
59
60
60
raise"Found PII in message"if:
@@ -64,7 +64,7 @@ raise "Found PII in message" if:
64
64
<divclass="code-caption"> Only messages containing credit card numbers will raise an error. </div>
65
65
66
66
67
-
### Preventing PII leakage
67
+
### Preventing PII Leakage
68
68
It is also possible to use the `pii` function in combination with other filters to get more complex behaviour. The example below shows how you can detect when an agent attempts to send emails outside of your organisation.
69
69
70
70
**Example:** Detecting PII Leakage in External Communications.
0 commit comments