Module 5 of 21 · Agentic AI Patterns — Planning, Reflection, Tool Use, CrewAI, AutoGen, Orchestration, Evaluation · Advanced

Overview of CrewAI Framework

Duration: 5 min

This module provides a comprehensive overview of the CrewAI framework, a powerful tool for orchestrating autonomous AI agents. Understanding CrewAI is crucial for leveraging advanced AI capabilities in complex, multi-agent environments. This module will cover key concepts such as planning, reflection, tool use, and orchestration, and will demonstrate how to implement these using Python.

Understanding CrewAI Agents

CrewAI agents are autonomous entities capable of performing tasks, making decisions, and interacting with tools and other agents. Each agent is designed with specific roles and capabilities, allowing them to collaborate effectively in a crew. The framework enables agents to plan their actions, reflect on their performance, and use tools to accomplish tasks.

from crewai import Agent, Task, Crew

# Define an agent
researcher = Agent(
  role='Senior Research Analyst',
  goal='Conduct market research',
  backstory='Experienced in market analysis with a focus on tech industries.',
  verbose=True
)

# Define a task
task = Task(
  description='Analyze the latest trends in AI technology',
  expected_output='A comprehensive report on AI trends',
  agent=researcher
)

# Create a crew with the agent and task
crew = Crew(agents=[researcher], tasks=[task])

# Run the crew
result = crew.kickoff()
print(result)

Try it in Google Colab: Open in Colab

{'status': 'completed', 'output': 'Comprehensive report on AI trends generated.'}

Orchestrating Multiple Agents

CrewAI allows for the orchestration of multiple agents to work together on complex tasks. This involves defining roles, setting goals, and specifying interactions between agents. The framework handles the coordination and ensures that agents can leverage each other’s outputs to achieve the overall objective.

from crewai import Agent, Task, Crew

# Define agents
researcher = Agent(
  role='Senior Research Analyst',
  goal='Conduct market research',
  backstory='Experienced in market analysis with a focus on tech industries.',
  verbose=True
)
writer = Agent(
  role='Content Writer',
  goal='Create engaging content',
  backstory='Skilled in writing compelling narratives and reports.',
  verbose=True
)

# Define tasks
research_task = Task(
  description='Analyze the latest trends in AI technology',
  expected_output='A detailed report on AI trends',
  agent=researcher
)
write_task = Task(
  description='Write an article based on the research report',
  expected_output='Engaging article on AI trends',
  agent=writer,
  depends_on=[research_task]
)

# Create a crew with the agents and tasks
crew = Crew(agents=[researcher, writer], tasks=[research_task, write_task])

# Run the crew
result = crew.kickoff()
print(result)

💡 Tip: Ensure that tasks are well-defined with clear dependencies to avoid conflicts and ensure smooth orchestration among agents.

❓ What is the primary role of an Agent in the CrewAI framework?

❓ How does CrewAI handle the coordination of multiple agents?

← Previous Continue interactively → Next →

Related Courses