|
| 1 | +# Guardrails |
| 2 | + |
| 3 | +A pattern for implementing topic guardrails that keep your AI agent focused on specific subjects using the Botpress ADK. |
| 4 | + |
| 5 | +## Use Case |
| 6 | + |
| 7 | +When building AI agents, you often need to ensure the conversation stays on topic. This example demonstrates how to implement **topic guardrails** that: |
| 8 | + |
| 9 | +- Monitor conversation content in real-time |
| 10 | +- Detect when users drift off-topic |
| 11 | +- Gracefully redirect users back to the intended subject |
| 12 | +- Display visual feedback when guardrails are triggered |
| 13 | + |
| 14 | +## How It Works |
| 15 | + |
| 16 | +1. **Before each agent execution**, the conversation transcript is analyzed using `zai.check()` |
| 17 | +2. The check runs **asynchronously** to avoid blocking the response |
| 18 | +3. If the topic drifts off-topic, a **custom guardrail message** is sent to the UI |
| 19 | +4. The agent receives an error with instructions to **recover gracefully** |
| 20 | +5. The agent redirects the user back to the intended topic |
| 21 | + |
| 22 | +## Key Components |
| 23 | + |
| 24 | +### Topic Check with `zai.check()` |
| 25 | + |
| 26 | +Uses the Zai library to verify if the conversation stays on topic: |
| 27 | + |
| 28 | +```typescript |
| 29 | +const guardAsync = adk.zai.check( |
| 30 | + transcript, |
| 31 | + `Is the transcript topic specifically about "Botpress"?`, |
| 32 | + { |
| 33 | + examples: [ |
| 34 | + { input: "Tell me about Botpress features.", check: true }, |
| 35 | + { input: "Tell me about cooking recipes.", check: false }, |
| 36 | + ], |
| 37 | + } |
| 38 | +); |
| 39 | +``` |
| 40 | + |
| 41 | +### `onBeforeExecution` Hook |
| 42 | + |
| 43 | +The guardrail runs before each agent execution cycle: |
| 44 | + |
| 45 | +```typescript |
| 46 | +await execute({ |
| 47 | + instructions: "You are a helpful assistant that only talks about Botpress.", |
| 48 | + hooks: { |
| 49 | + onBeforeExecution: async () => { |
| 50 | + const guard = await guardAsync; |
| 51 | + if (!guard) { |
| 52 | + throw new Error("Conversation stopped by guardrail..."); |
| 53 | + } |
| 54 | + }, |
| 55 | + }, |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +### Custom Guardrail Messages |
| 60 | + |
| 61 | +When triggered, a custom message is sent to display in the UI: |
| 62 | + |
| 63 | +```typescript |
| 64 | +await conversation.send({ |
| 65 | + type: "custom", |
| 66 | + payload: { |
| 67 | + url: "custom://guardrail", |
| 68 | + name: "TopicError", |
| 69 | + data: { |
| 70 | + name: "Out of Topic", |
| 71 | + message: "Topic is not about Botpress", |
| 72 | + }, |
| 73 | + }, |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +## Example Usage |
| 78 | + |
| 79 | +Try these prompts to see the guardrail in action: |
| 80 | + |
| 81 | +- ✅ "What is Botpress?" - On topic, agent responds normally |
| 82 | +- ✅ "How do I build a chatbot?" - Related to Botpress, agent responds |
| 83 | +- ❌ "Tell me a recipe for pizza" - Off topic, guardrail triggers |
| 84 | +- ❌ "What's the weather like?" - Off topic, guardrail triggers |
0 commit comments