Skip to content

Commit def2a88

Browse files
committed
Merge branch 'develop' of https://github.com/MervinPraison/PraisonAI into develop
2 parents 39316fa + 5f02656 commit def2a88

4 files changed

Lines changed: 397 additions & 10 deletions

File tree

src/praisonai-agents/CLAUDE.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ agent = Agent(
115115
self_reflect=True, # Enable self-reflection
116116
min_reflect=1, # Minimum reflection iterations
117117
max_reflect=3, # Maximum reflection iterations
118-
tools=[tool1, tool2] # Optional tools
118+
tools=[tool1, tool2], # Optional tools
119+
guardrail=validate_function, # Agent-level guardrail (function or string)
120+
max_guardrail_retries=3 # Retry limit for guardrail failures
119121
)
120122
```
121123

@@ -133,6 +135,8 @@ task = Task(
133135
```
134136

135137
### Guardrails Usage
138+
139+
#### Task-Level Guardrails
136140
```python
137141
from typing import Tuple, Any
138142

@@ -163,6 +167,35 @@ task = Task(
163167
)
164168
```
165169

170+
#### Agent-Level Guardrails
171+
```python
172+
# Agent guardrails apply to ALL outputs from that agent
173+
def validate_professional_tone(task_output: TaskOutput) -> Tuple[bool, Any]:
174+
"""Ensure professional tone in all agent responses."""
175+
content = task_output.raw.lower()
176+
casual_words = ['yo', 'dude', 'awesome', 'cool']
177+
for word in casual_words:
178+
if word in content:
179+
return False, f"Unprofessional language detected: {word}"
180+
return True, task_output
181+
182+
# Agent with function-based guardrail
183+
agent = Agent(
184+
name="BusinessWriter",
185+
instructions="You are a professional business writer",
186+
guardrail=validate_professional_tone, # Function guardrail
187+
max_guardrail_retries=3
188+
)
189+
190+
# Agent with LLM-based guardrail
191+
agent = Agent(
192+
name="ContentWriter",
193+
instructions="You are a content writer",
194+
guardrail="Ensure all responses are professional, accurate, and appropriate for business use", # String guardrail
195+
max_guardrail_retries=2
196+
)
197+
```
198+
166199
### Multi-Agent Workflow
167200
```python
168201
workflow = PraisonAIAgents(
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Agent-level guardrails example.
4+
5+
This example demonstrates how to use guardrails at the Agent level,
6+
which will apply to all tasks executed by that agent.
7+
"""
8+
9+
from typing import Tuple, Any
10+
from praisonaiagents import Agent, TaskOutput
11+
12+
def validate_content_length(task_output: TaskOutput) -> Tuple[bool, Any]:
13+
"""
14+
Validate that task output content meets minimum length requirement.
15+
16+
Args:
17+
task_output: The task output to validate
18+
19+
Returns:
20+
Tuple of (success, result_or_error_message)
21+
"""
22+
if len(task_output.raw) < 50:
23+
return False, "Content too short - must be at least 50 characters"
24+
return True, task_output
25+
26+
def validate_professional_tone(task_output: TaskOutput) -> Tuple[bool, Any]:
27+
"""
28+
Validate that the content has a professional tone.
29+
30+
Args:
31+
task_output: The task output to validate
32+
33+
Returns:
34+
Tuple of (success, result_or_error_message)
35+
"""
36+
content = task_output.raw.lower()
37+
unprofessional_words = ['yo', 'dude', 'awesome', 'cool', 'lol']
38+
39+
for word in unprofessional_words:
40+
if word in content:
41+
return False, f"Content contains unprofessional word: '{word}'"
42+
43+
return True, task_output
44+
45+
def main():
46+
"""Demonstrate Agent-level guardrails with function-based and LLM-based validation."""
47+
48+
print("=== Agent Guardrail Examples ===\n")
49+
50+
# Example 1: Function-based guardrail
51+
print("1. Function-based guardrail (content length validation):")
52+
agent1 = Agent(
53+
name="ContentWriter",
54+
instructions="You are a professional content writer who creates detailed responses",
55+
guardrail=validate_content_length,
56+
max_guardrail_retries=2
57+
)
58+
59+
try:
60+
result1 = agent1.start("Write a brief welcome message")
61+
print(f"Result: {result1}\n")
62+
except Exception as e:
63+
print(f"Error: {e}\n")
64+
65+
# Example 2: LLM-based guardrail (string description)
66+
print("2. LLM-based guardrail (professional tone validation):")
67+
agent2 = Agent(
68+
name="BusinessWriter",
69+
instructions="You are a business communication expert",
70+
guardrail="Ensure the content is professional, formal, and suitable for business communication. No casual language or slang.",
71+
max_guardrail_retries=3
72+
)
73+
74+
try:
75+
result2 = agent2.start("Write a welcome message for new employees")
76+
print(f"Result: {result2}\n")
77+
except Exception as e:
78+
print(f"Error: {e}\n")
79+
80+
# Example 3: Multiple agents with different guardrails
81+
print("3. Professional tone function-based guardrail:")
82+
agent3 = Agent(
83+
name="ProfessionalWriter",
84+
instructions="Write professional business content",
85+
guardrail=validate_professional_tone,
86+
max_guardrail_retries=2
87+
)
88+
89+
try:
90+
result3 = agent3.start("Write a casual greeting message")
91+
print(f"Result: {result3}\n")
92+
except Exception as e:
93+
print(f"Error: {e}\n")
94+
95+
print("=== Agent Guardrails Demonstration Complete ===")
96+
97+
if __name__ == "__main__":
98+
main()

0 commit comments

Comments
 (0)