ai_agent

How To Build First AI Agents using CrewAI

Have you ever dreamed of having an army of AI assistants? Each one with unique skills, working together to solve complex tasks? Well, dream no more! CrewAI lets you build just that. This guide will show you how to create your own CrewAI agents step by step.

What is CrewAI?

CrewAI lets you design AI agents with special skills. These agents work together to solve big problems. Imagine a team with a stock analyst, a trip planner, and a game developer. Each one adds their skills to reach a common goal.

Why Use CrewAI?

CrewAI makes complex tasks easier. For example, researching a company and making a report used to take hours. With CrewAI, it’s much faster. This lets you focus on big decisions, not just the details.

Let’s Build Your Crew using CrewAI!

Here are the three main parts of CrewAI:

  1. Agents: The AI specialists in your crew, each with a specific role.
  2. Tasks: The steps each agent must complete.
  3. Crew: The framework that manages your agents and tasks for the goal.

Step 1: Define Your Agents

The first step is to design your agents. These are the AI specialists for your problem. Think about these points:

  • Roles: Define each agent’s role based on its skills. For example, a “Web Researcher” agent might gather info, and a “Report Compiler” agent might analyze it.
  • Backstories (Optional): Giving your agents backstories can make them more interesting. Backstory helps generate output keeping the context in mind.
  • Capabilities or Goals: Decide how each agent will interact with the world. This might include accessing APIs or using LLMs like GPT-3.

Python Code Example: Defining an Agent

from crewai import Agent 

# Create a Web Researcher 
agent web_researcher_agent = Agent( role="Web Researcher", 
# Optional backstory 
backstory="You are an Expert in searching the web for relevant information", 
# Define capabilities (replace with your specific actions) 
capabilities={ "search_web": lambda query: f"Search results for {query}" } )

Step 2: Outline Your Agents’ Tasks

Next, design the tasks for each agent. Tasks tell the agent what to do to fulfill its role. Consider these factors:

  • Descriptions: Clearly state what each task aims to do.
  • Inputs: Define the info the agent gets before starting the task.
  • Outputs: Specify what the task should produce. This becomes the input for the next agent or the final Crew output.
  • agent: Specify which agent will handle this particular task.

Python Code Example: Defining a Task

from crewai import Task 

# Create a "Search Company News" task for the Web Researcher
search_company_news = Task( description="Searches the web for news articles about a company", inputs={"company_name": str}, outputs={"news_articles": list}, agent=web_researcher_agent )

Step 3: Assemble Your Crew!

Now it’s time to put your agents and tasks together into a Crew. Here’s what you need to consider:

  • Crew Definition: This is where you define the Crew itself, specifying the agents and tasks involved.
  • Processing Order: Determine the order in which tasks are executed. CrewAI offers both sequential (one task after another) and hierarchical (more complex workflows) processing options.

Python Code Example: Defining a Crew

from crewai import Crew 

# Create a Crew for the Stock Analyst 
stock_analyst_crew = Crew( agents=[web_researcher, # Add other agents here], tasks=[search_company_news, # Add other tasks here], 
# Define processing order (replace with your desired processing logic) process=Crew.Process.sequential )

Step 4: Kickoff Your Crew!

Once your Crew is ready, it’s time to set it in motion. Here’s how:

  • Provide Input: Give your Crew the initial information it needs to start working. This could be a company name for the stock analyst or a travel destination for the trip planner.
  • Run the Crew: Execute the Crew’s run method to see the magic unfold.

Python Code Example: Kick-off Crew

result=stock_analyst_crew.kickoff()
print(result)
CrewAI
Pexels.com

Real-World Use Case: Incident Response Crew

An IT engineer can use CrewAI to build an incident response crew:

Agents:

  • Incident Detector: Scans logs, monitors systems for anomalies.
  • Incident Classifier: Categorizes incidents based on severity and type.
  • Solution Finder: Searches knowledge base, suggests potential solutions.
  • Automation Executor: Executes predefined remediation steps.

Tasks:

  • Detect incidents.
  • Classify incident severity.
  • Find potential solutions.
  • Attempt automated resolution.
  • Escalate if necessary.

By following these steps, you can use CrewAI to create intelligent agents that work together to solve complex problems. Start by defining your agents and their roles, then outline their tasks. Assemble your agents into a Crew, specifying the processing order. Finally, provide input and run your Crew to see the magic happen. With CrewAI, you’re not just building AI; you’re creating an AI workforce tailored to your needs.

The possibilities are endless. From stock analysis to travel planning, the uses of CrewAI are only limited by your imagination. So, dive in, experiment, and create your own army of AI agents!

Back To Top