Crewai is an open source frame for orchestration of autonomous AI agents in a team. It allows you to create an AI “crew” where each agent has a specific role and goals and works together to perform complex tasks. In a herd system, several agents can collaborate, share information and coordinate their actions against a common goal. This makes it possible to divide a problem into sub -tasks and have specialized agents that tackle each part, like a team of people with different expertise.
In this tutorial, we demonstrate a use case for several AI agents working together using Crewai. Our example -Scenario will involve the summary of an article using three agents with different roles:
- Research Assistant Agent – Reads the article and draws the main points or facts.
- Summarizer Agent – takes the key points and briefly summarizes the article.
- Author Agent – reviews the summary and formats it to a structured final output (for example, the addition of a title or conclusion).
This collaborative workflow mimics how a team can work: one member collects information, another condenses it and a third polishes the presentation. We implement this workflow with crewai abstractions (agents, tasks and herds).
Code implementation
!pip install crewai crewai-tools transformers
First, we install all the necessary packages for the project at once. The Crewai and Crewai-Tools packages provide the framework and additional tools for orchestrating AI Agents, while the Transformers Package from embracing facial offerings for pre-formed models for word processing tasks such as summary.
from crewai import Agent, Task, Crew, Process
Here we import core classes from the Crewai frame. The agent class allows you to define an AI agent with a particular role and behavior, assignment represents a unit of work assigned to an agent, crew orchestrates the collaboration between these agents, and the process sets the execution work (as sequential or parallel).
# Define the agents' roles, goals, and backstories to satisfy the model requirements.
research_agent = Agent(
role="Research Assistant",
goal="Extract the main points and important facts from the article.",
backstory="You are a meticulous research assistant who carefully reads texts and extracts key points."
)
summarizer_agent = Agent(
role="Summarizer",
goal="Summarize the key points into a concise paragraph.",
backstory="You are an expert summarizer who can condense information into a clear and brief summary."
)
writer_agent = Agent(
role="Writer",
goal="Organize the summary into a final report with a title and conclusion.",
backstory="You are a creative writer with an eye for structure and clarity, ensuring the final output is polished."
)
We define three specialized AI agents using the crewai frame through the above code. Each agent is configured with a particular role, goals and back story that instructs them on how to contribute to the overall task: The research assistant draws key points from an article, summarizes these points to a concise section, and the author formats the final output of a polished report.
# Example: Create tasks for each agent
research_task = Task(
description="Read the article and identify the main points and important facts.",
expected_output="A list of bullet points summarizing the key information from the article.",
agent=research_agent
)
summarization_task = Task(
description="Take the above bullet points and summarize them into a concise paragraph that captures the article's essence.",
expected_output="A brief paragraph summarizing the article.",
agent=summarizer_agent
)
writing_task = Task(
description="Review the summary paragraph and format it with a clear title and a concluding sentence.",
expected_output="A structured summary of the article with a title and conclusion.",
agent=writer_agent
)
The above three task definitions assign specific responsibilities to the respective agents. Research_Task instructs the research assistant to extract key points from the article, Summarization_Task instructs the summary to transform these points into a brief section, and Write_Task asks the author to format the summary to a structured final report with a title and conclusion.
# Create a crew with a sequential process
crew = Crew(
agents=[research_agent, summarizer_agent, writer_agent],
tasks=[research_task, summarization_task, writing_task],
process=Process.sequential,
verbose=True
)
print("Agents defined successfully!")
Now we create a herd object that orchestrates the collaboration between the three defined agents and their corresponding tasks in a sequential workflow. With process. Seseciential, each task is performed one after the other, and setting Verbose = TRUE enables detailed logging of the process.
# Sample article text (as a multi-paragraph string)
article_text = """Artificial intelligence (AI) has made significant inroads in various sectors, transforming how we work and live.
One of the most notable impacts of AI is seen in healthcare, where machine learning algorithms assist doctors in diagnosing diseases faster and more accurately.
In the automotive industry, AI powers self-driving cars, analyzing traffic patterns in real-time to ensure passenger safety.
This technology also plays a crucial role in finance, with AI-driven algorithms detecting fraudulent transactions and enabling personalized banking services.
Education is another field being revolutionized by AI. Intelligent tutoring systems and personalized learning platforms adapt to individual student needs, making education more accessible and effective.
Despite these advancements, AI adoption also raises important questions.
Concerns about job displacement, ethical use of AI, and ensuring data privacy are at the forefront of public discourse as AI continues to evolve.
Overall, AI's growing influence across industries showcases its potential to tackle complex problems, but it also underscores the need for thoughtful integration to address the challenges that accompany these innovations.
"""
print("Article length (characters):", len(article_text))
In this part, we define a string with several parts named Centicle_Text that simulates an article discussing AI’s influence across different industries, such as healthcare, automotive, finance and education. It then prints the character length of the article and helps verify that the text is loaded correctly for further processing.
import re
def extract_key_points(text):
paragraphs = [p.strip() for p in text.split("\n\n") if p.strip()]
key_points = []
for para in paragraphs:
sentences = re.split(r'(?<=\.)\s+', para.strip())
if not sentences:
continue
main_sentence = max(sentences, key=len)
point = main_sentence.strip()
if point and point[-1] not in ".!?":
point += "."
key_points.append(point)
return key_points
# Use the function on the article_text
key_points = extract_key_points(article_text)
print("Research Assistant Agent - Key Points:\n")
for i, point in enumerate(key_points, 1):
print(f"{i}. {point}")
Here we define a feature extract_key_points that processes an article’s text by dividing it into paragraphs and longer in sentences. For each section, it chooses the longest sentence as the central point (as a heuristic) and ensures that it ends with proper punctuation. Finally, it prints each key point as a numbered list that simulates the research assistant’s output.
from transformers import pipeline
# Initialize a summarization pipeline
summarizer_pipeline = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
# Prepare the input for summarization by joining the key points
input_text = " ".join(key_points)
summary_output = summarizer_pipeline(input_text, max_length=100, min_length=30, do_sample=False)
summary_text = summary_output[0]['summary_text']
print("Summarizer Agent - Summary:\n")
print(summary_text)
We initialize an embracing facial summary pipeline using “SSHLEIFER/DISTILART-CNN-12-6” model. It then joins the extracted key points in a single string and leads it to the pipeline that generates a brief summary. Finally, it prints the resulting summary and simulates output from Summarizer agent.
# Writer agent formatting
title = "AI's Impact Across Industries: A Summary"
conclusion = "In conclusion, while AI offers tremendous benefits across sectors, addressing its challenges is crucial for its responsible adoption."
# Combine title, summary, and conclusion
final_report = f"# {title}\n\n{summary_text}\n\n{conclusion}"
print("Writer Agent - Final Output:\n")
print(final_report)
Finally, we simulate the author’s work by formatting the final output. It defines a title and a conclusion and then combines these with the previously generated summary (stored in Summary_text) using an F-string. The final report is formatted in Markdown, with the title of a headline, followed by the summary and the final sentence. It is then printed to show the complete, structured summary.
Finally, this tutorial showed how to create a team of AI agents using Crewai and makes them work together on a task. We structured a problem (summarized an article) in sub-tasks handled by specialized agents and demonstrated the end-to-end stream with examples of outputs. Crewai provides a flexible framework to manage these multi-agent collaborations, while we as users as users define the roles and processes that control the agents’ teamwork.
Here it is Colab notebook For the above project. Nor do not forget to follow us on Twitter and join in our Telegram Channel and LinkedIn GrOUP. Don’t forget to take part in our 80k+ ml subbreddit.
🚨 Recommended Reading AI Research Release Nexus: An Advanced System Integrating Agent AI system and Data Processing Standards To Tackle Legal Concerns In Ai Data Set
Asif Razzaq is CEO of Marketchpost Media Inc. His latest endeavor is the launch of an artificial intelligence media platform, market post that stands out for its in -depth coverage of machine learning and deep learning news that is both technically sound and easily understandable by a wide audience. The platform boasts over 2 million monthly views and illustrates its popularity among the audience.
🚨 Recommended Open Source AI platform: ‘Intellagent is an open source multi-agent framework for evaluating complex conversation-ai system’ (promoted)