Understanding Agentic Frameworks | Crew AI

Mahimai Raja J
6 min readMay 18, 2024

--

Discover Crew AI, a framework for creating, deploying, and managing powerful multi-agent AI systems effortlessly.

Hey folks! In this blog, let’s discuss the more agentic way of thinking and how we could implement one using the popular framework CrewAI. Let’s dive into the topic!

AI agent is a system that combines Large Language Models (LLMs), interaction capabilities, and tools to reasonably react to inputs and perform tasks. This integration enables AI agents to simulate human-like responses and execute complex operations, making them versatile in various applications.

The future of AI is in creating machines that can reason and learn as humans do — Yann LeCun (Meta)

Multi-Agent Systems

Multi-agent systems [4] consist of multiple AI agents working together to solve problems, coordinate actions, and achieve goals that are difficult for a single agent to accomplish. This collaborative approach enhances efficiency and performance in tasks ranging from simple automations to complex decision-making processes.

Building Blocks of Crew AI

credits: CrewAI

Agents: The core entities in Crew AI, agents are designed to perform specific roles and tasks. Each agent has defined capabilities and focuses on particular aspects of a project.

Tasks: These are the individual units of work assigned to agents. Tasks come with clear descriptions and expectations, ensuring that agents know precisely what to accomplish.

Crew: A collection of agents working together towards a common goal. Crews are structured to optimize collaboration and efficiency, enabling complex multi-agent interactions and workflows.

Key Elements of an Agent

  1. Role Playing: Each agent is assigned a specific role, such as a researcher or analyst, to focus their efforts.
  2. Focus: Agents maintain attention on their assigned tasks, ensuring efficient execution.
  3. Tools: Equipped with various tools for data retrieval, processing, and interaction.
  4. Cooperation: Agents collaborate with each other to complete tasks.
  5. Guardrails: Safety measures and protocols to ensure reliable and ethical operations.
  6. Memory: Ability to store and recall past interactions and data, enhancing decision-making.

Framework to Design Own Agents

Think of yourself as a manager defining [3] goals and processes. Determine the tasks, roles, and tools needed, then “hire” agents (assign roles) to build a cohesive crew. For instance, to conduct market research, you might need a data analyst, a researcher, and a report generator.

Key Elements of Tools

  1. Versatile: Tools must be adaptable to different tasks and environments.
  2. Fault Tolerance: Resilience to errors and failures, ensuring consistent performance.
  3. Caching: Efficient storage and retrieval of frequently used data to speed up processes.

Key Elements of Tasks

  1. Clear Description: Precisely define what needs to be done.
  2. Expectations: Outline the expected outcomes and standards for completion.

Multi-Agent Collaboration

Multi-agent collaboration [2] in Crew AI involves multiple agents working together to achieve complex goals. This collaboration can take various forms depending on the nature and requirements of the tasks. Effective collaboration ensures that tasks are completed efficiently and accurately. Here are the key types:

  1. Sequential: Agents perform tasks in a predetermined order, where each step depends on the completion of the previous one.
  2. Hierarchical: Agents follow a structured hierarchy, with higher-level agents overseeing and coordinating the activities of lower-level agents.
  3. Asynchronous: Agents operate independently, handling tasks as they arise without adhering to a fixed sequence, allowing for flexibility and parallel processing.

Writing an Article using Crew AI — Demo

Let’s create a crew and understand the working in Practical.

  1. Initial setup and load the api keys
import os
from crewai import Agent, Task, Crew

from dotenv import load_env
load_env()

os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["OPENAI_MODEL_NAME"] = 'gpt-3.5-turbo'

Assuming you have already stored the Open AI api key in the .env file. if not execute the below line in the terminal (this is for unix systems only, windows may vary).

export OPENAI_API_KEY=sk-your_api_key

2. Define the Agents — Planner, Writer, Editor.

planner = Agent(
role="Content Planner",
goal="Plan engaging and factually accurate content on {topic}",
backstory="You're working on planning a blog article "
"about the topic: {topic}."
"You collect information that helps the "
"audience learn something "
"and make informed decisions. "
"Your work is the basis for "
"the Content Writer to write an article on this topic.",
allow_delegation=False,
verbose=True
)

writer = Agent(
role="Content Writer",
goal="Write insightful and factually accurate "
"opinion piece about the topic: {topic}",
backstory="You're working on a writing "
"a new opinion piece about the topic: {topic}. "
"You base your writing on the work of "
"the Content Planner, who provides an outline "
"and relevant context about the topic. "
"You follow the main objectives and "
"direction of the outline, "
"as provide by the Content Planner. "
"You also provide objective and impartial insights "
"and back them up with information "
"provide by the Content Planner. "
"You acknowledge in your opinion piece "
"when your statements are opinions "
"as opposed to objective statements.",
allow_delegation=False,
verbose=True
)

editor = Agent(
role="Editor",
goal="Edit a given blog post to align with "
"the writing style of the organization. ",
backstory="You are an editor who receives a blog post "
"from the Content Writer. "
"Your goal is to review the blog post "
"to ensure that it follows journalistic best practices,"
"provides balanced viewpoints "
"when providing opinions or assertions, "
"and also avoids major controversial topics "
"or opinions when possible.",
allow_delegation=False,
verbose=True
)

3. Define the tasks (plan, write and edit) for the agents we created above.

plan = Task(
description=(
"1. Prioritize the latest trends, key players, "
"and noteworthy news on {topic}.\n"
"2. Identify the target audience, considering "
"their interests and pain points.\n"
"3. Develop a detailed content outline including "
"an introduction, key points, and a call to action.\n"
"4. Include SEO keywords and relevant data or sources."
),
expected_output="A comprehensive content plan document "
"with an outline, audience analysis, "
"SEO keywords, and resources.",
agent=planner,
)

write = Task(
description=(
"1. Use the content plan to craft a compelling "
"blog post on {topic}.\n"
"2. Incorporate SEO keywords naturally.\n"
"3. Sections/Subtitles are properly named "
"in an engaging manner.\n"
"4. Ensure the post is structured with an "
"engaging introduction, insightful body, "
"and a summarizing conclusion.\n"
"5. Proofread for grammatical errors and "
"alignment with the brand's voice.\n"
),
expected_output="A well-written blog post "
"in markdown format, ready for publication, "
"each section should have 2 or 3 paragraphs.",
agent=writer,
)

edit = Task(
description=("Proofread the given blog post for "
"grammatical errors and "
"alignment with the brand's voice."),
expected_output="A well-written blog post in markdown format, "
"ready for publication, "
"each section should have 2 or 3 paragraphs.",
agent=editor
)

4. It’s time to assemble the crew. Combine the agents into our awesome crew.

crew = Crew(
agents=[planner, writer, editor],
tasks=[plan, write, edit],
verbose=2
)

5. It’s time to kick-off! Execute the Crew.

topic = "AI Agents and Conversational AI"
result = crew.kickoff(inputs={"topic": topic})

from IPython.display import Markdown
Markdown(result)

Hurray, now we have an awesome article hand crafted by multiple agents. Find the link [5] to complete source code in the reference section.

Conclusion

Crew AI revolutionizes the way we build and manage multi-agent systems, offering a robust framework for creating sophisticated AI solutions. By leveraging agents, tasks, and tools, users can design efficient and collaborative AI systems tailored to specific needs.

With Crew AI, the future of multi-agent systems is here. Start building today and unlock the full potential of AI collaboration!

Feel free to write to me on LinkedIn 📩

References:

[ 1 ] Repository

[ 2 ] Official Documentation

[ 3 ] Short Course by DeepLearning AI

[ 4 ] Article by Newsbene

[ 5 ] Source Code

--

--