AImy.blog Logo
← Back to Latest Intelligence
·AI Tutorials

Build Your First AI Agent: Automate Market Research with CrewAI

Learn to build a practical AI agent system using CrewAI, a powerful framework for orchestrating multiple AI agents to automate complex workflows like market research, step-by-step.

Christina
Christina
AImy Editor
Build Your First AI Agent: Automate Market Research with CrewAI

Build Your First AI Agent: Automate Market Research with CrewAI

Unlock AI Automation: Why Agents Matter

AI agents are transforming how we interact with technology, moving beyond simple prompts to autonomous execution of complex workflows. Imagine an AI that doesn't just answer a question but actively researches, analyzes, and synthesizes information for you. This tutorial will guide you through building a practical AI agent system using CrewAI, a powerful framework for orchestrating multiple AI agents.

Prerequisites: What You'll Need

Before we dive in, ensure you have:

  • Python 3.9+: Installed on your system.
  • pip: Python's package installer.
  • OpenAI API Key: For accessing powerful LLMs (e.g., GPT-4). Set it as an environment variable OPENAI_API_KEY.
  • Serper API Key: For web search capabilities. Set it as an environment variable SERPER_API_KEY. (You can substitute with another search API like Brave Search API or Google Custom Search API, adjusting the tool accordingly).

Understanding the Core Components of CrewAI

CrewAI simplifies the creation of multi-agent systems by providing clear abstractions for:

  • Agents: Autonomous entities with defined roles, goals, and backstories. They decide on actions.
  • Tools: Functions agents can use to interact with the external world (e.g., search engines, code interpreters, file readers).
  • Tasks: Specific units of work assigned to agents, with a clear description and expected output.
  • Process: How tasks are executed (sequential, hierarchical, etc.).

Step-by-Step: Building a Market Research Crew

Our goal is to build a crew of AI agents that can research the latest trends in sustainable packaging and generate a concise report.

Step 1: Set Up Your Environment

First, create a new directory for your project and install CrewAI:

mkdir ai_research_crew
cd ai_research_crew
python -m venv venv
source venv/bin/activate # On Windows: .\venv\Scripts\activate
pip install crewai 'crewai[tools]' python-dotenv

Create a .env file in your project root and add your API keys:

OPENAI_API_KEY="your_openai_api_key_here"
SERPER_API_KEY="your_serper_api_key_here"

Step 2: Define Your Tools

Our agents will need a way to access current information from the web. We'll use the SerperDevTool from crewai_tools.

Create a file named tools.py:

from crewai_tools import SerperDevTool

# Initialize the SerperDevTool
search_tool = SerperDevTool()

Step 3: Define Your Agents

Now, let's create two agents: a Researcher and an Analyst. Each will have a distinct role, goal, and a backstory to guide its behavior.

Create a file named agents.py:

from crewai import Agent
from langchain_openai import ChatOpenAI
from tools import search_tool
import os

# Initialize the LLM
llm = ChatOpenAI(model="gpt-4o", temperature=0.7) # You can use other models like "gpt-3.5-turbo"

# Define the Researcher Agent
researcher = Agent(
    role='Senior Research Analyst',
    goal='Discover and summarize the latest trends in sustainable packaging solutions.',
    backstory=(
        "You are a seasoned research analyst with a knack for identifying emerging trends "
        "and synthesizing complex information into actionable insights. Your expertise "
        "lies in navigating vast amounts of data to pinpoint critical developments."
    ),
    verbose=True,
    allow_delegation=False,
    tools=[search_tool],
    llm=llm
)

# Define the Analyst Agent
analyst = Agent(
    role='Report Compiler and Synthesizer',
    goal='Compile a comprehensive report on sustainable packaging trends based on research findings.',
    backstory=(
        "You are an expert report writer, skilled at taking raw data and research "
        "summaries and transforming them into clear, concise, and insightful reports. "
        "Your reports are known for their clarity and strategic value."
    ),
    verbose=True,
    allow_delegation=False,
    llm=llm
)

Step 4: Define Your Tasks

Next, we define the tasks these agents need to perform. Each task specifies the agent responsible, a description, and the expected output.

Create a file named tasks.py:

from crewai import Task

# Define the Research Task
research_task = Task(
    description=(
        "Identify the top 5-7 most significant and emerging trends in sustainable packaging "
        "over the last 12-18 months. Focus on innovations, market adoption, and environmental impact."
        "Provide specific examples of companies or technologies driving these trends."
    ),
    expected_output='A detailed list of 5-7 sustainable packaging trends with brief descriptions and examples.',
    agent=researcher,
)

# Define the Analysis and Reporting Task
analysis_task = Task(
    description=(
        "Synthesize the research findings into a comprehensive, well-structured report. "
        "The report should include an introduction, a section for each identified trend "
        "with detailed explanations and examples, and a concluding summary of key takeaways."
        "Ensure the language is professional and easy to understand for a business audience."
    ),
    expected_output='A professional, 500-800 word report detailing the latest sustainable packaging trends.',
    agent=analyst,
)

Step 5: Orchestrate with a Crew

Finally, we bring everything together in a Crew. We define the agents involved, the tasks they need to complete, and the process flow. For this example, a sequential process is sufficient.

Create a file named main.py:

import os
from dotenv import load_dotenv
from crewai import Crew, Process

# Load environment variables from .env file
load_dotenv()

# Import agents and tasks
from agents import researcher, analyst
from tasks import research_task, analysis_task

# Instantiate your crew
market_research_crew = Crew(
    agents=[researcher, analyst],
    tasks=[research_task, analysis_task],
    process=Process.sequential, # Tasks will be executed in the order they are provided
    verbose=True,
)

# Kick off the crew's work
print("### Starting the Market Research Crew ###")
result = market_research_crew.kickoff()

print("\n\n### Market Research Report Completed ###")
print(result)

Step 6: Run Your AI Agent System

Now, execute your main.py script:

python main.py

Watch as your agents communicate, use tools, and collaboratively work towards the defined goal. The verbose=True setting will show you the agents' thought processes.

Expected Output

The script will print the final market research report generated by your analyst agent, based on the findings gathered by your researcher agent. This report will detail current sustainable packaging trends.

Next Steps & Advanced Concepts

This tutorial provided a basic introduction. You can expand on this by:

  • Adding more agents: Introduce a "Quality Assurance Agent" to review the report.
  • Creating custom tools: Integrate with internal databases, specific APIs, or file systems.
  • Implementing different processes: Explore hierarchical processes for more complex workflows.
  • Integrating with other LLMs: Experiment with different models or local LLMs.
  • Error Handling: Implement robust error handling for tool failures or unexpected agent behavior.

Conclusion

Building AI agents empowers you to automate complex, multi-step workflows that go far beyond simple scripting. By leveraging frameworks like CrewAI, you can design intelligent systems that perform research, analysis, content creation, and much more, freeing up valuable human time for higher-level strategic thinking. Start experimenting and unlock the true potential of AI automation!

Tags & Entities

#AI Agents#CrewAI#Workflow Automation#Python#Tutorial#Market Research