from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.guardrails import LLMGuardrail
# Define agents
researcher = Agent(
name="Researcher",
instructions="Research topics thoroughly and provide accurate information"
)
writer = Agent(
name="Blog Writer",
instructions="Write engaging, SEO-friendly blog posts"
)
editor = Agent(
name="Editor",
instructions="Polish and perfect blog posts"
)
# Define guardrails
def seo_validation(task_output):
"""Validate SEO requirements"""
content = task_output.raw
# Check title
lines = content.split('\n')
if not lines[0].startswith('#'):
return False, "Missing H1 title"
# Check meta description
if 'Meta description:' not in content:
return False, "Missing meta description"
# Check keyword density (example: "AI" keyword)
keyword_count = content.lower().count('ai')
word_count = len(content.split())
density = (keyword_count / word_count) * 100
if density < 1 or density > 3:
return False, f"Keyword density {density:.1f}% (target: 1-3%)"
return True, task_output
# LLM-based quality guardrail
quality_guardrail = LLMGuardrail(
description="""
Evaluate if the blog post:
1. Has a compelling introduction that hooks readers
2. Provides valuable, actionable information
3. Uses clear, concise language
4. Includes relevant examples or case studies
5. Has a strong conclusion with call-to-action
6. Maintains consistent tone throughout
7. Is free of factual errors
8. Flows logically from point to point
""",
llm="gpt-4"
)
# Create tasks with guardrails
tasks = [
Task(
description="Research the topic: 'Future of AI in Healthcare'",
agent=researcher,
expected_output="Comprehensive research notes"
),
Task(
description="Write a 1000-word blog post based on the research",
agent=writer,
guardrail=seo_validation,
expected_output="SEO-optimized blog post"
),
Task(
description="Edit and polish the blog post",
agent=editor,
guardrail=quality_guardrail,
expected_output="Publication-ready blog post",
context=[tasks[1]] # Use previous task output
)
]
# Run the blog generation pipeline
agents = PraisonAIAgents(
agents=[researcher, writer, editor],
tasks=tasks,
verbose=True
)
# Execute with guardrails
result = agents.start()
# The system will:
# 1. Research the topic
# 2. Write the blog post (retry if SEO validation fails)
# 3. Edit the post (retry if quality standards not met)
# 4. Return the final, validated blog post