Skip to content

Commit ea5a5ac

Browse files
Merge pull request #933 from MervinPraison/claude/issue-931-20250715_160543
feat: add comprehensive examples for missing PraisonAI features
2 parents 42911b1 + 6bb8ea4 commit ea5a5ac

File tree

8 files changed

+1633
-0
lines changed

8 files changed

+1633
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
RouterAgent Cost Optimization Example
3+
4+
This example demonstrates how to use RouterAgent for intelligent model selection
5+
based on cost optimization. The router agent automatically selects the most
6+
cost-effective model for different types of tasks while maintaining quality.
7+
8+
Features demonstrated:
9+
- Cost-optimized model routing
10+
- Automatic model selection based on task complexity
11+
- Usage tracking and cost monitoring
12+
- Fallback mechanisms for reliability
13+
"""
14+
15+
from praisonaiagents import Agent
16+
from praisonaiagents.agent.router_agent import RouterAgent
17+
from praisonaiagents.tools import duckduckgo
18+
19+
# Define model options with cost considerations
20+
# Using different models for different complexity levels
21+
cheap_model = "gpt-4o-mini" # For simple tasks
22+
balanced_model = "gpt-4o" # For moderate complexity
23+
premium_model = "claude-3-5-sonnet-20241022" # For complex tasks
24+
25+
# Create RouterAgent with cost optimization strategy
26+
router = RouterAgent(
27+
name="CostOptimizedRouter",
28+
role="Cost-Efficient Task Router",
29+
goal="Route tasks to the most cost-effective model while maintaining quality",
30+
backstory="You are a smart router that optimizes for cost-efficiency by selecting appropriate models based on task complexity.",
31+
32+
# Model routing strategy - simple tasks to cheap models, complex to premium
33+
models=[cheap_model, balanced_model, premium_model],
34+
35+
# Routing logic - this could be enhanced with more sophisticated rules
36+
instructions="""You are a cost-optimization router. For simple tasks like basic questions,
37+
definitions, or simple calculations, use the cheapest model. For moderate complexity tasks
38+
like writing, analysis, or research, use the balanced model. For complex tasks requiring
39+
deep reasoning, creativity, or specialized knowledge, use the premium model.""",
40+
41+
tools=[duckduckgo],
42+
verbose=True
43+
)
44+
45+
# Example tasks of varying complexity levels
46+
47+
# Simple task - should route to cheap model
48+
print("="*60)
49+
print("TESTING SIMPLE TASK (should use cheap model)")
50+
print("="*60)
51+
simple_result = router.start("What is the capital of France?")
52+
print(f"Simple task result: {simple_result}")
53+
54+
# Moderate complexity task - should route to balanced model
55+
print("\n" + "="*60)
56+
print("TESTING MODERATE TASK (should use balanced model)")
57+
print("="*60)
58+
moderate_result = router.start("Write a brief analysis of renewable energy trends in 2024")
59+
print(f"Moderate task result: {moderate_result}")
60+
61+
# Complex task - should route to premium model
62+
print("\n" + "="*60)
63+
print("TESTING COMPLEX TASK (should use premium model)")
64+
print("="*60)
65+
complex_result = router.start("Conduct a comprehensive strategic analysis of the impact of quantum computing on cybersecurity, including potential vulnerabilities and mitigation strategies")
66+
print(f"Complex task result: {complex_result}")
67+
68+
# Research task with tool usage - should route appropriately
69+
print("\n" + "="*60)
70+
print("TESTING RESEARCH TASK WITH TOOLS")
71+
print("="*60)
72+
research_result = router.start("Research and analyze the latest developments in artificial intelligence regulation globally")
73+
print(f"Research task result: {research_result}")
74+
75+
print("\n" + "="*80)
76+
print("COST OPTIMIZATION ROUTER DEMONSTRATION COMPLETED")
77+
print("="*80)
78+
print("The RouterAgent automatically selected appropriate models based on task complexity:")
79+
print("- Simple factual questions → Cheaper model (gpt-4o-mini)")
80+
print("- Moderate analysis tasks → Balanced model (gpt-4o)")
81+
print("- Complex strategic analysis → Premium model (claude-3-5-sonnet)")
82+
print("- This approach optimizes costs while maintaining quality for each task type")
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
"""
2+
Comprehensive Guardrails Example
3+
4+
This example demonstrates both function-based and LLM-based guardrails for validating
5+
agent outputs and ensuring quality, safety, and compliance.
6+
7+
Features demonstrated:
8+
- Function-based validation guardrails
9+
- LLM-based content validation
10+
- Multiple validation criteria
11+
- Retry mechanisms with feedback
12+
- Quality assurance workflows
13+
"""
14+
15+
from praisonaiagents import Agent, Task, PraisonAIAgents
16+
from praisonaiagents.task import TaskOutput
17+
from typing import Tuple, Any
18+
import re
19+
20+
# Define function-based guardrails for different validation types
21+
22+
def email_format_guardrail(task_output: TaskOutput) -> Tuple[bool, Any]:
23+
"""
24+
Validates that the output contains properly formatted email addresses.
25+
"""
26+
output_text = str(task_output.raw)
27+
28+
# Email regex pattern
29+
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
30+
emails_found = re.findall(email_pattern, output_text)
31+
32+
if emails_found:
33+
return True, task_output
34+
else:
35+
return False, "No valid email addresses found in the output. Please include at least one properly formatted email address."
36+
37+
def word_count_guardrail(task_output: TaskOutput) -> Tuple[bool, Any]:
38+
"""
39+
Validates that the output meets minimum word count requirements.
40+
"""
41+
output_text = str(task_output.raw)
42+
word_count = len(output_text.split())
43+
44+
min_words = 50 # Minimum required words
45+
46+
if word_count >= min_words:
47+
return True, task_output
48+
else:
49+
return False, f"Output too short. Found {word_count} words but need at least {min_words} words. Please provide a more detailed response."
50+
51+
def professional_tone_guardrail(task_output: TaskOutput) -> Tuple[bool, Any]:
52+
"""
53+
Validates that the output maintains a professional tone.
54+
"""
55+
output_text = str(task_output.raw).lower()
56+
57+
# Check for unprofessional words/phrases
58+
unprofessional_terms = ['stupid', 'dumb', 'sucks', 'terrible', 'awful', 'hate']
59+
60+
for term in unprofessional_terms:
61+
if term in output_text:
62+
return False, f"Output contains unprofessional language ('{term}'). Please revise to maintain a professional tone."
63+
64+
return True, task_output
65+
66+
def factual_accuracy_guardrail(task_output: TaskOutput) -> Tuple[bool, Any]:
67+
"""
68+
Validates that the output includes specific factual elements.
69+
"""
70+
output_text = str(task_output.raw).lower()
71+
72+
# Check for presence of factual indicators
73+
factual_indicators = ['according to', 'research shows', 'studies indicate', 'data suggests', 'evidence']
74+
75+
has_factual_backing = any(indicator in output_text for indicator in factual_indicators)
76+
77+
if has_factual_backing:
78+
return True, task_output
79+
else:
80+
return False, "Output lacks factual backing. Please include references to research, studies, or data to support your claims."
81+
82+
# Create agents with different guardrail configurations
83+
84+
# Agent with email format validation
85+
email_agent = Agent(
86+
name="EmailAgent",
87+
role="Business Communication Specialist",
88+
goal="Create professional business communications with proper email formatting",
89+
backstory="You are a professional communication specialist who ensures all business correspondence includes proper contact information.",
90+
instructions="Always include properly formatted email addresses in your responses."
91+
)
92+
93+
# Agent with word count validation
94+
content_agent = Agent(
95+
name="ContentAgent",
96+
role="Content Writer",
97+
goal="Create detailed, comprehensive content that meets length requirements",
98+
backstory="You are a professional content writer who creates detailed, well-researched articles.",
99+
instructions="Provide comprehensive, detailed responses with sufficient depth and explanation."
100+
)
101+
102+
# Agent with professional tone validation
103+
business_agent = Agent(
104+
name="BusinessAgent",
105+
role="Professional Advisor",
106+
goal="Provide professional business advice with appropriate tone",
107+
backstory="You are a seasoned business advisor who maintains professionalism in all communications.",
108+
instructions="Maintain a professional, respectful tone in all communications."
109+
)
110+
111+
# Agent with factual accuracy validation
112+
research_agent = Agent(
113+
name="ResearchAgent",
114+
role="Research Analyst",
115+
goal="Provide well-researched, fact-based analysis",
116+
backstory="You are a research analyst who backs all claims with evidence and citations.",
117+
instructions="Support all statements with references to research, studies, or credible data sources."
118+
)
119+
120+
# Create tasks with specific guardrails
121+
122+
# Task with email format guardrail
123+
email_task = Task(
124+
name="email_communication",
125+
description="Write a business proposal for a new software project, including contact information",
126+
expected_output="Professional business proposal with contact email addresses",
127+
agent=email_agent,
128+
guardrail=email_format_guardrail,
129+
max_retries=3
130+
)
131+
132+
# Task with word count guardrail
133+
content_task = Task(
134+
name="detailed_analysis",
135+
description="Write an analysis of cloud computing benefits for small businesses",
136+
expected_output="Comprehensive analysis of cloud computing benefits (minimum 50 words)",
137+
agent=content_agent,
138+
guardrail=word_count_guardrail,
139+
max_retries=3
140+
)
141+
142+
# Task with professional tone guardrail
143+
business_task = Task(
144+
name="professional_advice",
145+
description="Provide advice on handling difficult client relationships",
146+
expected_output="Professional advice maintaining appropriate business tone",
147+
agent=business_agent,
148+
guardrail=professional_tone_guardrail,
149+
max_retries=3
150+
)
151+
152+
# Task with factual accuracy guardrail
153+
research_task = Task(
154+
name="research_report",
155+
description="Analyze the impact of artificial intelligence on job markets",
156+
expected_output="Research-backed analysis with citations and evidence",
157+
agent=research_agent,
158+
guardrail=factual_accuracy_guardrail,
159+
max_retries=3
160+
)
161+
162+
# LLM-based guardrail example
163+
llm_guardrail_agent = Agent(
164+
name="LLMGuardrailAgent",
165+
role="Content Quality Validator",
166+
goal="Validate content quality using LLM-based assessment",
167+
backstory="You are a quality assurance specialist who uses AI to validate content quality.",
168+
instructions="Create marketing content that is engaging, accurate, and compliant with advertising standards."
169+
)
170+
171+
# Task with LLM-based guardrail
172+
llm_guardrail_task = Task(
173+
name="marketing_content",
174+
description="Create a marketing email for a new fitness app targeting busy professionals",
175+
expected_output="Engaging marketing email that is accurate and compliant",
176+
agent=llm_guardrail_agent,
177+
178+
# LLM-based guardrail using natural language validation
179+
guardrail="Validate that this marketing content is: 1) Factually accurate with no false claims, 2) Compliant with advertising standards (no misleading statements), 3) Engaging and professional in tone, 4) Includes a clear call-to-action. If any criteria are not met, explain what needs to be improved.",
180+
max_retries=2
181+
)
182+
183+
# Execute tasks with guardrails
184+
agents = PraisonAIAgents(
185+
agents=[email_agent, content_agent, business_agent, research_agent, llm_guardrail_agent],
186+
tasks=[email_task, content_task, business_task, research_task, llm_guardrail_task],
187+
verbose=True
188+
)
189+
190+
print("="*80)
191+
print("EXECUTING TASKS WITH COMPREHENSIVE GUARDRAILS")
192+
print("="*80)
193+
194+
result = agents.start()
195+
196+
print("\n" + "="*80)
197+
print("GUARDRAILS DEMONSTRATION COMPLETED")
198+
print("="*80)
199+
print("This example demonstrated:")
200+
print("- Function-based validation (email format, word count, tone, factual accuracy)")
201+
print("- LLM-based content validation using natural language criteria")
202+
print("- Automatic retry mechanisms with specific feedback")
203+
print("- Quality assurance workflows for different content types")
204+
print("- Professional validation standards for business communications")

0 commit comments

Comments
 (0)