CrewAI, Empower your AI Agent

NUTHDANAI WANGPRATHAM
6 min readJun 2, 2024

--

In my first article in the AI Agent series, I’m introducing How to Develop your first AI Agent. I am Quant, and I work at an asset management company. Therefore, I am interested in both GEN AI and Investment. I came across the article “Financial Statement Analysis with
Large Language Models
” This article concludes AI agents can beat Professional Financial Analysts. I doubted how this was possible until I discovered the article and code on GitHub. They use CrewAI to improve LLM performance.

What is CrewAI?

CrewAI is a framework designed to orchestrate autonomous AI agents to operate cohesively and collaboratively, much like a well-oiled crew. Here’s a detailed overview of what CrewAI offers and how it works:

Key Features of CrewAI

  1. Role-Based Agent Design: Agents in CrewAI are designed with specific roles, goals, and tools. This allows for specialization and a clearer division of labor among agents.
  2. Autonomous Inter-Agent Delegation: Agents can autonomously delegate tasks and communicate among themselves, enhancing problem-solving efficiency and allowing for more complex operations.
  3. Processes Driven: CrewAI supports different process-driven execution models, allowing tasks to be executed sequentially, hierarchically, or through other complex processes.
  4. Integration with Tools: CrewAI integrates with various internal to CrewAI and those available through the LangChain framework. This allows agents to perform multiple tasks, from web scraping to API interactions.

The CrewAI framework comprises several key components that facilitate the seamless orchestration of autonomous AI agents. The agents are Central to this framework, designed with specific roles, goals, and backstories that guide their actions and interactions. These agents can be equipped with various tools and have memory capabilities, allowing them to remember past interactions and perform more efficiently. Tasks are assigned to these agents, each with a clear description and expected output, ensuring that the agents understand their objectives and have the necessary tools to accomplish them.

The framework includes the concept of a Crew, which is a collection of agents working together to achieve a set of tasks. The crew configuration defines how tasks are distributed among agents and how they collaborate. The execution model for the crew is defined by processes, which currently support sequential execution, with more complex models like hierarchical execution in development. These components work together to enable the creation of sophisticated, multi-agent systems that can perform complex operations and achieve shared goals.

Demo with python

Basic Example of Using CrewAI

This example creates two agents: a researcher and a writer, assigns them specific tasks, and then runs the crew to complete these tasks.

First, ensure you have CrewAI and its necessary tools installed. If not, you might need to install it via pip.

pip install crewai

You need to set up API keys for any tools you plan to use. For example, here we use a search tool from serper.dev.

import os 
os.environ["SERPER_API_KEY"] = "Your Serper.dev API Key"
os.environ["OPENAI_API_KEY"] = "Your OpenAI API Key"

Importing Required Classes and Tools

from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool

# Initialize the search tool
search_tool = SerperDevTool()

Define the agents with their roles, goals, and backstories.

# Senior Researcher Agent
researcher = Agent(
role='Senior Researcher',
goal='Uncover groundbreaking technologies in {topic}',
verbose=True,
memory=True,
backstory=(
"Driven by curiosity, you're at the forefront of"
"innovation, eager to explore and share knowledge that could change"
"the world."
),
tools=[search_tool],
allow_delegation=True
)

# Writer Agent
writer = Agent(
role='Writer',
goal='Narrate compelling tech stories about {topic}',
verbose=True,
memory=True,
backstory=(
"With a flair for simplifying complex topics, you craft"
"engaging narratives that captivate and educate, bringing new"
"discoveries to light in an accessible manner."
),
tools=[search_tool],
allow_delegation=False
)

Define the tasks to be assigned to the agents.

# Research Task
research_task = Task(
description=(
"Identify the next big trend in {topic}."
"Focus on identifying pros and cons and the overall narrative."
"Your final report should clearly articulate the key points,"
"its market opportunities, and potential risks."
),
expected_output='A comprehensive 3 paragraphs long report on the latest AI trends.',
tools=[search_tool],
agent=researcher,
)

# Writing Task
write_task = Task(
description=(
"Compose an insightful article on {topic}."
"Focus on the latest trends and how it's impacting the industry."
"This article should be easy to understand, engaging, and positive."
),
expected_output='A 4 paragraph article on {topic} advancements formatted as markdown.',
tools=[search_tool],
agent=writer,
async_execution=False,
output_file='new-blog-post.md' # Example of output customization
)

Combine agents and tasks into a crew and define the execution process.

# Forming the tech-focused crew with enhanced configurations
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
process=Process.sequential # Optional: Sequential task execution is default
)

# Running the crew with input topic
result = crew.kickoff(inputs={'topic': 'AI in healthcare'})
print(result)

Finished chain. Artificial intelligence (AI) is rapidly becoming an integral part of healthcare, bringing transformative changes that promise a more efficient, cost-effective, and personalized health system. Among the latest trends in AI-enabled healthcare are data summarization and exploration, AI-powered cost-effective treatments, Emotion AI for mental health disorders, and Generative AI for personalized medicine. Data summarization and exploration provide clinicians with accessible and comprehensive patient insights, enhancing decision-making and improving the quality of care. AI-powered treatments are heralding a new era of precision medicine, where treatments are tailored to individual patient profiles, significantly improving outcomes. Emotion AI is a breakthrough in mental health care, offering nuanced understanding and treatment of disorders such as autism. Generative AI is propelling personalized medicine to new heights by creating unique health solutions based on individual genetic, environmental, and lifestyle factors. The benefits of AI in healthcare are significant. Improved patient engagement, increased surgical precision, and automation of tedious tasks are just a few of the advantages. AI can analyze large amounts of data to improve diagnoses and treatment plans, enhancing patient care and potentially reducing the cost of care. Patient engagement is improved through applications offering personalized care recommendations and educational content. Tedious tasks are automated, freeing up clinicians’ schedules for more patient interaction. However, the integration of AI in healthcare is not without challenges. Data privacy and ethical concerns are at the forefront. As more patient data is collected and analyzed, questions about who has access to this information and how it’s used arise. The potential for job displacement is another concern, as AI could replace certain roles in healthcare. Effective human-AI collaboration is crucial, requiring training and understanding from healthcare professionals. Despite these challenges, the future of AI in healthcare is promising. As we continue to innovate and refine these technologies, we can look forward to a healthcare system that is more efficient, personalized, and patient-centric. With AI, the potential to revolutionize healthcare is immense, promising a healthier future for all.

Conclusion

CrewAI is a robust framework designed to orchestrate the collaborative efforts of autonomous AI agents. By assigning specific roles, goals, and backstories to agents, and providing them with the necessary tools, CrewAI facilitates the execution of complex tasks through efficient inter-agent communication and delegation. The basic example provided illustrates how easily one can set up and run a crew of agents to perform specialized tasks, highlighting the power and flexibility of the framework.

This modular and process-driven approach allows for scalable and adaptable AI solutions, making CrewAI an excellent choice for projects requiring sophisticated multi-agent interactions. Whether for research, content creation, or other applications, CrewAI provides a structured and efficient way to leverage the capabilities of multiple AI agents working in conce

Ref:

https://docs.crewai.com/

--

--