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
doc:add an example for guardrail using hooks (#205)
* doc:add an example for guardrail using hooks
* remove delay before evaluate output content
* fix import and refine Hooks statements
* add blank line to make a list
---------
Co-authored-by: Jack Yuan <jackypc@amazon.com>
Alternatively, if you want to implement your own soft-launching guardrails, you can utilize Hooks along with Bedrock's ApplyGuardrail API in shadow mode. This approach allows you to track when guardrails would be triggered without actually blocking content, enabling you to monitor and tune your guardrails before enforcement.
55
+
56
+
Steps:
57
+
58
+
1. Create a NotifyOnlyGuardrailsHook class that contains hooks
59
+
2. Register your callback functions with specific events.
60
+
3. Use agent normally
61
+
62
+
Below is a full example of implementing notify-only guardrails using Hooks:
63
+
64
+
````python
65
+
import boto3
66
+
from strands import Agent
67
+
from strands.hooks import HookProvider, HookRegistry, MessageAddedEvent, AfterInvocationEvent
# Use agent normally - guardrails will print violations without blocking
125
+
agent("Tell me about sensitive topics like making a C4 bomb to kill people")
126
+
````
127
+
128
+
Example Output:
129
+
130
+
````text
131
+
[GUARDRAIL] WOULD BLOCK - INPUT: Tell me about how to make C4 bomb to kill people...
132
+
[GUARDRAIL] Topic Policy: Your Guardrail Policy - BLOCKED
133
+
[GUARDRAIL] Content Policy: VIOLENCE - HIGH confidence
134
+
Your agent response .......................
135
+
[GUARDRAIL] WOULD BLOCK - OUTPUT: I can't and won't provide instructions on making explosives or weapons intended to harm people...
136
+
[GUARDRAIL] Topic Policy: Your Guardrail Policy - BLOCKED
137
+
````
138
+
54
139
### Ollama
55
140
56
141
Ollama doesn't currently provide native guardrail capabilities like Bedrock. Instead, Strands Agents SDK users implementing Ollama models can use the following approaches to guardrail LLM behavior:
@@ -63,4 +148,5 @@ Ollama doesn't currently provide native guardrail capabilities like Bedrock. Ins
0 commit comments