|
| 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