-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathguardrails_example.py
More file actions
154 lines (118 loc) · 5.14 KB
/
guardrails_example.py
File metadata and controls
154 lines (118 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
"""
Example demonstrating guardrails functionality in PraisonAI Agents.
This example shows both function-based and LLM-based guardrails
for validating task outputs.
"""
import sys
import os
from typing import Tuple, Any
from praisonaiagents import Agent, Task, TaskOutput
def email_validator(task_output: TaskOutput) -> Tuple[bool, Any]:
"""
Function-based guardrail to validate email content.
Args:
task_output: The task output to validate
Returns:
Tuple of (success, result_or_error)
"""
content = task_output.raw.lower()
# Check for required email components
if "subject:" not in content:
return False, "Email must include a subject line"
if "dear" not in content and "hello" not in content:
return False, "Email must include a proper greeting"
if len(content) < 50:
return False, "Email content is too short"
if "error" in content or "problem" in content:
return False, "Email should not mention errors or problems"
return True, task_output
def main():
"""Run the guardrails example."""
print("PraisonAI Agents - Guardrails Example")
print("=====================================\n")
# Create an agent
agent = Agent(
name="Email Assistant",
role="Professional Email Writer",
goal="Write clear, professional emails",
backstory="I am an AI assistant specialized in writing professional emails"
)
print("1. Testing Function-based Guardrail")
print("------------------------------------")
# Create task with function-based guardrail
task_with_function_guardrail = Task(
description="Write a professional email to a client about project completion",
expected_output="A well-formatted professional email",
agent=agent,
guardrail=email_validator, # Function-based guardrail
max_retries=2
)
print(f"Task created with function guardrail: {email_validator.__name__}")
print(f"Max retries: {task_with_function_guardrail.max_retries}")
# Simulate a task output that should pass
good_output = TaskOutput(
description="Email task",
raw="""Subject: Project Completion Update
Dear Client,
I am pleased to inform you that your project has been completed successfully.
All deliverables have been reviewed and are ready for your review.
Please let me know if you have any questions.
Best regards,
Project Team""",
agent="Email Assistant"
)
result = task_with_function_guardrail._process_guardrail(good_output)
print(f"Good email result: {'PASSED' if result.success else 'FAILED'}")
if not result.success:
print(f"Error: {result.error}")
# Simulate a task output that should fail
bad_output = TaskOutput(
description="Email task",
raw="Hi there, there was an error with your project.",
agent="Email Assistant"
)
result = task_with_function_guardrail._process_guardrail(bad_output)
print(f"Bad email result: {'PASSED' if result.success else 'FAILED'}")
if not result.success:
print(f"Error: {result.error}")
print("\n2. Testing String-based LLM Guardrail")
print("-------------------------------------")
# Create task with string-based guardrail
task_with_llm_guardrail = Task(
description="Write a marketing email for a new product launch",
expected_output="Engaging marketing content",
agent=agent,
guardrail="Ensure the content is professional, engaging, includes a clear call-to-action, and is free of errors",
max_retries=3
)
print("Task created with LLM-based guardrail")
print("Guardrail description: 'Ensure the content is professional, engaging, includes a clear call-to-action, and is free of errors'")
print(f"Max retries: {task_with_llm_guardrail.max_retries}")
print("\n3. Backward Compatibility")
print("-------------------------")
# Create task without guardrail (backward compatible)
task_without_guardrail = Task(
description="Write a simple thank you note",
expected_output="A brief thank you message",
agent=agent
)
print("Task created without guardrail (backward compatible)")
print(f"Guardrail function: {task_without_guardrail._guardrail_fn}")
# Test that it doesn't break existing functionality
simple_output = TaskOutput(
description="Thank you task",
raw="Thank you for your business!",
agent="Email Assistant"
)
result = task_without_guardrail._process_guardrail(simple_output)
print(f"No guardrail result: {'PASSED' if result.success else 'FAILED'}")
print("\n✅ Guardrails example completed successfully!")
print("\nKey Features Demonstrated:")
print("- Function-based guardrails with custom validation logic")
print("- String-based LLM guardrails using natural language")
print("- Configurable retry mechanism")
print("- Backward compatibility with existing tasks")
print("- Integration with TaskOutput validation")
if __name__ == "__main__":
main()