This video provides a comprehensive tutorial on CrewAI, an agentic AI framework. It covers the fundamental concepts of AI agents and agentic AI, installation steps, API key setup, and demonstrates how to build simple agents, agents with tools, and crews. The tutorial progresses to more advanced topics like using YAML for configuration and culminates in building a complete marketing strategy project using multiple agents.
Agent, Task, and Crew as fundamental building blocks in CrewAI.Here's a step-by-step guide to building a multi-agent system using CrewAI, based on the provided transcript:
uv init <your_project_name> (e.g., uv init marketing_crew)uv add crewai toolsyour_project_name/venv/Scripts/python.exe)..env File: In your project's root directory, create a file named .env..env file:
GEMINI_API_KEY="YOUR_GEMINI_API_KEY"from dotenv import load_dotenvload_dotenv()Agents are the core actors in your system. For a multi-agent system, you'll define several, each with a specific role and goal.
Import Necessary Classes:
from crewai import Agent, Crew, Task, Processfrom crewai_tools import ... (import any tools needed)from langchain_google_genai import ChatGoogleGenerativeAI (or your chosen LLM)Initialize LLM: Create an LLM object.
llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.7)Define Individual Agents: For each agent, specify:
role: A concise title for the agent (e.g., "Marketing Head", "Content Writer").goal: What the agent is ultimately trying to achieve.backstory: Context about the agent's expertise and persona.verbose: Set to True to see detailed execution logs.llm: Assign the initialized LLM.tools (Optional): Assign any relevant tools the agent can use.allow_delegation: Set to True if the agent can assign tasks to other agents.max_iter (Optional): Limit the number of iterations for an agent.Example Agent (Marketing Head):
marketing_head = Agent(
role='Head of Marketing',
goal='Lead the marketing team to achieve specific goals for product promotion.',
backstory="""An experienced marketing professional with a proven track record of leading successful marketing campaigns for SaaS products.
Skilled in market research, strategy development, and team coordination.""",
verbose=True,
llm=llm,
tools=[serp_tool, website_scraping_tool, directory_read_tool, file_write_tool, file_read_tool], # Example tools
allow_delegation=True,
max_iter=15 # Example iteration limit
)
Repeat this for all agents (e.g., content_writer_social_media, content_writer_blog, seo_specialist).
Tasks define specific actions agents need to perform. For a multi-agent system, tasks are often sequential or dependent.
Import Task class: from crewai import Task
Define Individual Tasks: For each task, specify:
description: A clear instruction for the agent.agent: Assign the agent responsible for this task.expected_output (Optional): Describe the desired outcome.context (Optional): Provide outputs from previous tasks that this task depends on.tools (Optional): Specify tools to be used for this task.output_json (Optional): Define a Pydantic model for JSON output.Example Tasks (Sequential):
market_research_task = Task(
description="Conduct market research to identify trends, opportunities, and challenges in the AI-powered Excel automation industry. Focus on customer needs, competitive strategies, and market dynamics to inform marketing decisions for Autoheet IQ.",
agent=marketing_head,
expected_output="A comprehensive market research report including key trends, competitor analysis, and target audience insights.",
tools=[serp_tool, website_scraping_tool] # Example tools for this task
)
marketing_strategy_task = Task(
description="Develop a detailed marketing strategy for Autoheet IQ, a new AI-powered Excel automation tool, targeting small and medium enterprises.",
agent=marketing_head,
context=[market_research_task], # Depends on the output of market_research_task
expected_output="A clear marketing strategy document outlining objectives, target audience, value proposition, and key channels.",
tools=[file_read_tool] # Example tool
)
# ... define other tasks like content_calendar_task, social_media_post_task, etc.
The Crew orchestrates the agents and tasks.
Import Crew class: from crewai import Crew, Process
Instantiate the Crew:
agents.tasks.process: Use Process.sequential for tasks executed in order, or Process.hierarchical for more complex workflows.verbose and max_rpm can also be set at the crew level.Example Crew:
crew = Crew(
agents=[marketing_head, content_writer_social_media, content_writer_blog, seo_specialist],
tasks=[market_research_task, marketing_strategy_task, content_calendar_task, social_media_post_task, blog_post_task, seo_optimization_task], # List all tasks
process=Process.sequential, # or Process.hierarchical
verbose=True,
max_rpm=10 # Example rate limiting
)
Provide the necessary inputs to kick off the crew's execution.
inputs = { 'product_name': 'Autoheet IQ', 'target_audience': 'Small and Medium Enterprises', 'current_date': '2024-07-26' }kickoff() method.
result = crew.kickoff(inputs=inputs)print("Crew execution finished!")print("Final Result:", result)This separates configuration from code, making it easier to manage prompts and agent settings.
config Directory: Make a directory named config in your project root.agents.yaml: Define your agents in this file.
- role: Head of Marketing
goal: Lead the marketing team...
backstory: An experienced marketing professional...
verbose: true
llm: gemini-pro # Specify the LLM model here
tools:
- serp_tool
- website_scraping_tool
allow_delegation: true
max_iter: 15
# ... define other agents
tasks.yaml: Define your tasks in this file.
- description: Conduct market research...
agent: Head of Marketing # Matches the role in agents.yaml
expected_output: A comprehensive market research report...
tools:
- serp_tool
- website_scraping_tool
# ... define other tasks
CrewBase and annotations to load configurations.
from crewai import Agent, Task, Crew, Process
from crewai_tools import ...
from crewai.project import CrewBase # Import CrewBase
# Define your LLM
llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.7)
# Create your crew class inheriting from CrewBase
class MarketingCrew(CrewBase):
agents_config = 'config/agents.yaml'
tasks_config = 'config/tasks.yaml'
def __init__(self):
super().__init__()
# Define agents and tasks using the configurations
@Agent(role='Head of Marketing', ...) # Reference the agent defined in YAML
def marketing_head(self):
pass
@Task(agent=marketing_head, ...) # Reference the task defined in YAML
def market_research_task(self):
pass
# ... define other agents and tasks here
@CrewCrew(agents=agents, tasks=tasks, ...) # Define the crew itself
def crew(self):
pass
# Instantiate and run
marketing_crew = MarketingCrew()
result = marketing_crew.kickoff(inputs={...})
print(result)
Note: The exact syntax for integrating YAML with CrewBase might involve specific decorators and class structures as shown in the video's later part, especially for agent and task decorators linking to the YAML configurations.verbose output to understand agent actions, tool usage, and reasoning processes.temperature, max_iter, and max_rpm to balance creativity, execution time, and cost.context parameter in Task definitions.