Skip to content

Commit e5e1113

Browse files
committed
2 parents c15c8a6 + 6603124 commit e5e1113

File tree

7 files changed

+474
-0
lines changed

7 files changed

+474
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
AutoAgents Hierarchical Generation Example
3+
4+
This example demonstrates hierarchical agent patterns using PraisonAI
5+
for structured task delegation and coordinated workflows.
6+
"""
7+
8+
from praisonaiagents import Agent, Task, PraisonAIAgents
9+
from praisonaiagents.tools import internet_search
10+
11+
print("=== AutoAgents Hierarchical Generation Example ===\n")
12+
13+
# Create manager agent for hierarchical coordination
14+
manager_agent = Agent(
15+
name="Project Manager",
16+
role="Project Coordinator",
17+
goal="Coordinate hierarchical task execution and manage team workflow",
18+
backstory="Experienced manager who excels at breaking down complex projects and coordinating teams",
19+
tools=[internet_search],
20+
allow_delegation=True,
21+
verbose=True
22+
)
23+
24+
# Create specialist agent for execution
25+
research_agent = Agent(
26+
name="Research Specialist",
27+
role="Market Research Analyst",
28+
goal="Conduct thorough market research and analysis",
29+
backstory="Expert researcher skilled in market analysis and competitive intelligence",
30+
tools=[internet_search],
31+
verbose=True
32+
)
33+
34+
# Hierarchical task structure
35+
planning_task = Task(
36+
description="""Plan a comprehensive market research project for electric vehicles:
37+
1. Define research scope and objectives
38+
2. Identify key research areas and methodologies
39+
3. Create task breakdown structure
40+
4. Set deliverable requirements for execution team
41+
42+
Prepare clear instructions for the research specialist.""",
43+
expected_output="Detailed project plan with clear research instructions",
44+
agent=manager_agent
45+
)
46+
47+
execution_task = Task(
48+
description="""Execute the market research plan:
49+
1. Follow the research plan from the project manager
50+
2. Conduct comprehensive market analysis
51+
3. Gather competitive intelligence and market data
52+
4. Prepare detailed findings report
53+
54+
Use the planning guidance to ensure complete coverage.""",
55+
expected_output="Comprehensive market research report with findings",
56+
agent=research_agent,
57+
context=[planning_task]
58+
)
59+
60+
# Run hierarchical workflow
61+
agents_system = PraisonAIAgents(
62+
agents=[manager_agent, research_agent],
63+
tasks=[planning_task, execution_task],
64+
process="sequential",
65+
verbose=True
66+
)
67+
68+
print("Starting hierarchical agent generation...")
69+
result = agents_system.start()
70+
71+
print(f"\nHierarchical Result: {result[:200]}...")
72+
print("\n✅ AutoAgents hierarchical generation complete!")
73+
print("Demonstrated coordinated hierarchical workflow between manager and specialist agents.")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Self-Reflection Optimization Patterns Example
3+
4+
This example demonstrates self-reflection capabilities using PraisonAI's
5+
built-in reflection features for iterative improvement and quality optimization.
6+
"""
7+
8+
from praisonaiagents import Agent, Task, PraisonAIAgents
9+
from praisonaiagents.tools import internet_search
10+
11+
print("=== Self-Reflection Optimization Patterns Example ===\n")
12+
13+
# Create self-reflecting agent with built-in reflection capabilities
14+
reflection_agent = Agent(
15+
name="Self-Reflecting Agent",
16+
role="Self-Improving Researcher",
17+
goal="Demonstrate self-reflection and iterative improvement patterns",
18+
backstory="Expert researcher with strong self-reflection and continuous improvement capabilities",
19+
tools=[internet_search],
20+
self_reflect=True,
21+
min_reflect=2,
22+
max_reflect=4,
23+
reflect_llm="gpt-4o-mini",
24+
verbose=True
25+
)
26+
27+
# Create task that benefits from self-reflection
28+
reflection_task = Task(
29+
description="""Research and analyze the future of artificial intelligence:
30+
1. Investigate current AI trends and breakthrough technologies
31+
2. Analyze potential impacts on society and industry
32+
3. Provide well-reasoned predictions for the next 5 years
33+
4. Ensure analysis is comprehensive, accurate, and well-structured
34+
35+
Use self-reflection to improve the quality and completeness of your analysis.""",
36+
expected_output="Comprehensive AI future analysis with high quality through self-reflection",
37+
agent=reflection_agent
38+
)
39+
40+
# Run with self-reflection optimization
41+
print("Starting self-reflection optimization demonstration...")
42+
result = reflection_agent.execute_task(reflection_task)
43+
44+
print(f"\nSelf-Reflection Result: {result[:200]}...")
45+
print("\n✅ Self-reflection optimization complete!")
46+
print("Agent demonstrated iterative improvement through built-in self-reflection capabilities.")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Advanced Callback Systems Example
3+
4+
This example demonstrates callback systems using PraisonAI's built-in
5+
callback functionality for monitoring agent interactions and tool usage.
6+
"""
7+
8+
from praisonaiagents import Agent, Task, PraisonAIAgents
9+
from praisonaiagents.display_callback import register_display_callback
10+
from praisonaiagents.tools import internet_search
11+
12+
print("=== Advanced Callback Systems Example ===\n")
13+
14+
# Simple callback function to monitor interactions
15+
def interaction_callback(data):
16+
"""Monitor agent interactions"""
17+
print(f"📋 Interaction: {data.get('agent_name', 'Unknown')} - {data.get('type', 'unknown')}")
18+
19+
# Simple callback function to monitor tool usage
20+
def tool_callback(data):
21+
"""Monitor tool usage"""
22+
print(f"🔧 Tool Used: {data.get('agent_name', 'Unknown')} used {data.get('tool_name', 'unknown')}")
23+
24+
# Simple callback function to monitor generation
25+
def generation_callback(data):
26+
"""Monitor content generation"""
27+
print(f"✍️ Generation: {data.get('agent_name', 'Unknown')} generating content")
28+
29+
# Register callbacks with PraisonAI's callback system
30+
print("Registering callbacks...")
31+
register_display_callback('interaction', interaction_callback)
32+
register_display_callback('tool_call', tool_callback)
33+
register_display_callback('generating', generation_callback)
34+
print("✅ Callbacks registered\n")
35+
36+
# Create a research agent with callback monitoring
37+
research_agent = Agent(
38+
name="Research Agent",
39+
role="Information Researcher",
40+
goal="Research topics and demonstrate callback monitoring",
41+
backstory="Expert researcher that demonstrates callback functionality",
42+
tools=[internet_search],
43+
verbose=True
44+
)
45+
46+
# Create a simple task
47+
research_task = Task(
48+
description="Research the latest developments in renewable energy technology",
49+
expected_output="Brief summary of renewable energy developments",
50+
agent=research_agent
51+
)
52+
53+
# Execute with callback monitoring
54+
print("Starting research with callback monitoring...")
55+
result = research_agent.execute_task(research_task)
56+
57+
print(f"\nResearch Result: {result[:200]}...")
58+
print("\n✅ Callback monitoring demonstration complete!")
59+
print("Callbacks successfully tracked agent interactions, tool usage, and content generation.")
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Production Guardrails Patterns Example
3+
4+
This example demonstrates production guardrail patterns using PraisonAI's
5+
built-in validation and safety mechanisms for secure agent operations.
6+
"""
7+
8+
from praisonaiagents import Agent, Task, PraisonAIAgents
9+
from praisonaiagents.tools import internet_search
10+
11+
print("=== Production Guardrails Patterns Example ===\n")
12+
13+
# Simple guardrail function for content validation
14+
def content_guardrail(response, context):
15+
"""Simple content validation guardrail"""
16+
if not response or len(response.strip()) < 50:
17+
return {
18+
"valid": False,
19+
"reason": "Response too short for production use",
20+
"action": "regenerate"
21+
}
22+
23+
# Check for sensitive patterns (simplified)
24+
sensitive_keywords = ["password", "secret", "private_key", "api_key"]
25+
if any(keyword in response.lower() for keyword in sensitive_keywords):
26+
return {
27+
"valid": False,
28+
"reason": "Potential sensitive information detected",
29+
"action": "review_and_redact"
30+
}
31+
32+
return {"valid": True}
33+
34+
# Create production-ready agent with guardrails
35+
production_agent = Agent(
36+
name="Production Agent",
37+
role="Production Content Generator",
38+
goal="Generate safe, compliant content for production use",
39+
backstory="Production-ready agent with built-in safety and compliance checks",
40+
tools=[internet_search],
41+
guardrail=content_guardrail,
42+
max_retries=3,
43+
verbose=True
44+
)
45+
46+
# Create secure task with validation
47+
secure_task = Task(
48+
description="""Create a professional report about renewable energy trends:
49+
1. Research current renewable energy market developments
50+
2. Include factual data and reliable sources
51+
3. Ensure content is production-ready and compliant
52+
4. Avoid any sensitive or inappropriate information
53+
54+
Focus on creating safe, accurate, professional content.""",
55+
expected_output="Professional renewable energy report meeting production standards",
56+
agent=production_agent
57+
)
58+
59+
# Run with guardrail validation
60+
print("Starting production guardrails demonstration...")
61+
result = production_agent.execute_task(secure_task)
62+
63+
print(f"\nProduction Result: {result[:200]}...")
64+
print("\n✅ Production guardrails validation complete!")
65+
print("Agent demonstrated safe content generation with built-in guardrail validation.")
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Advanced Graph Memory Integration Example
3+
4+
This example demonstrates graph memory capabilities with PraisonAI agents
5+
for knowledge graph construction and relationship-aware memory retrieval.
6+
"""
7+
8+
from praisonaiagents import Agent, Task, PraisonAIAgents
9+
from praisonaiagents.tools import internet_search
10+
11+
print("=== Advanced Graph Memory Integration Example ===\n")
12+
13+
# Configure simple graph memory
14+
memory_config = {
15+
"provider": "mem0",
16+
"config": {
17+
"vector_store": {
18+
"provider": "chroma",
19+
"config": {
20+
"path": ".praison/graph_memory"
21+
}
22+
}
23+
}
24+
}
25+
26+
# Create knowledge builder agent with graph memory
27+
knowledge_agent = Agent(
28+
name="Knowledge Agent",
29+
role="Knowledge Graph Builder",
30+
goal="Build and retrieve from knowledge graphs",
31+
backstory="Expert at creating structured knowledge and finding relationships",
32+
tools=[internet_search],
33+
memory=True,
34+
verbose=True
35+
)
36+
37+
# Task to build knowledge graph about AI companies
38+
build_task = Task(
39+
description="""Research and store information about major AI companies and their relationships:
40+
1. OpenAI - products like GPT-4, ChatGPT, leadership, partnerships
41+
2. Anthropic - Claude AI, safety focus, team
42+
3. Relationships between these companies and their technologies
43+
44+
Focus on capturing connections and relationships between entities.""",
45+
expected_output="Knowledge graph with AI company ecosystem relationships",
46+
agent=knowledge_agent
47+
)
48+
49+
# Task to query the knowledge graph
50+
query_task = Task(
51+
description="""Using the stored knowledge graph, answer:
52+
1. What are the main products of each company?
53+
2. Who are the key leaders in these companies?
54+
3. How do these companies relate to each other?
55+
56+
Use relationship-aware memory retrieval.""",
57+
expected_output="Analysis using graph memory relationships",
58+
agent=knowledge_agent
59+
)
60+
61+
# Run with graph memory integration
62+
agents_system = PraisonAIAgents(
63+
agents=[knowledge_agent],
64+
tasks=[build_task, query_task],
65+
memory=True,
66+
memory_config=memory_config,
67+
verbose=True
68+
)
69+
70+
print("Starting graph memory demonstration...")
71+
result = agents_system.start()
72+
73+
print(f"\nGraph Memory Result: {result[:200]}...")
74+
print("\n✅ Graph memory integration complete!")
75+
print("Agent built knowledge graph and performed relationship-aware queries.")

0 commit comments

Comments
 (0)