Learn how to create AI agents that can autonomously monitor, act, and adapt based on environment feedback.
An agent-based workflow where LLMs act autonomously within a loop, interacting with their environment and receiving feedback to refine their actions and decisions.
Set your OpenAI API key as an environment variable in your terminal:
Copy
export OPENAI_API_KEY=your_api_key_here
3
Create a file
Create a new file app.py with the basic setup:
Copy
from praisonaiagents import Agent, Workflowfrom praisonaiagents.workflows import route, repeat# Create monitor agent that analyzes environment statemonitor_agent = Agent( name="Monitor", role="Environment Monitor", goal="Monitor and analyze system state", instructions="Analyze the current state. Respond with ONLY 'normal', 'critical', or 'optimal'.")# Create action agents for different statesmaintenance_agent = Agent( name="Maintenance", role="System Maintainer", goal="Maintain normal system operations", instructions="Perform routine maintenance tasks. Report what was done.")emergency_agent = Agent( name="Emergency", role="Emergency Responder", goal="Handle critical situations", instructions="Address the critical issue immediately. Provide resolution steps.")optimization_agent = Agent( name="Optimizer", role="System Optimizer", goal="Optimize system performance", instructions="The system is optimal. Suggest further improvements.")# Create feedback agentfeedback_agent = Agent( name="Feedback", role="Feedback Processor", goal="Process and learn from feedback", instructions="Analyze the action taken and provide feedback for improvement.")# Check if optimal state reacheddef is_optimal(ctx) -> bool: return "optimal" in ctx.previous_result.lower()# Create autonomous workflowworkflow = Workflow( steps=[ repeat( monitor_agent, # Monitor keeps checking state until=is_optimal, max_iterations=3 ), route({ "normal": [maintenance_agent], "critical": [emergency_agent], "optimal": [optimization_agent] }), feedback_agent ])result = workflow.start("Monitor the AI system health")print(f"Result: {result['output'][:500]}...")
4
Start Workflow
Type this in your terminal to run your workflow:
Copy
python app.py
Requirements
Python 3.10 or higher
OpenAI API key. Generate OpenAI API key here. Use Other models using this guide.
For optimal results, ensure your environment monitoring is reliable and your feedback processing logic is properly configured for effective adaptation.