Skip to content

Commit cb64f8f

Browse files
committed
added guardrails demo
1 parent 3daad46 commit cb64f8f

36 files changed

+8921
-3
lines changed

examples/approval/bot/agent.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from "@botpress/runtime";
22

33
export default defineConfig({
4-
name: "demo-brand-extractor",
4+
name: "approval",
55
description: "An AI agent built with Botpress ADK",
66

77
defaultModels: {
@@ -12,7 +12,6 @@ export default defineConfig({
1212
dependencies: {
1313
integrations: {
1414
webchat: { version: "webchat@0.3.0", enabled: true },
15-
delegate: { version: "agi/delegate@0.1.0", enabled: true },
1615
},
1716
},
1817
});

examples/approval/bot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "demo-brand-extractor",
2+
"name": "approval-bot",
33
"version": "1.0.0",
44
"description": "A Botpress Agent built with the ADK",
55
"type": "module",

examples/guardrails/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

examples/guardrails/bot/.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Dependencies
2+
node_modules/
3+
.pnpm-store/
4+
5+
# Build outputs
6+
dist/
7+
.adk/
8+
9+
# Environment files
10+
.env
11+
.env.local
12+
.env.production
13+
14+
# IDE files
15+
.vscode/
16+
.idea/
17+
*.swp
18+
*.swo
19+
20+
# OS files
21+
.DS_Store
22+
Thumbs.db
23+
24+
# Logs
25+
*.log
26+
logs/
27+
28+
# Runtime files
29+
*.pid
30+
*.seed
31+
*.pid.lock

examples/guardrails/bot/.mcp.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"mcpServers": {
3+
"adk": {
4+
"command": "adk",
5+
"args": [
6+
"mcp"
7+
]
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)