
Introduction
AI agent improvement is without doubt one of the hottest frontiers of Software program innovation. As the standard of Giant Language Fashions evolves, we are going to witness a surge in AI agent integration with current software program techniques. With AI brokers, will probably be potential to perform duties with voice or gesture instructions as an alternative of manually navigating by means of purposes. However proper now, agent improvement is in its nascent stage. We’re nonetheless going by means of the preliminary section of infrastructure, instruments, and framework improvement, just like the Web of the Nineties. So, on this article, we are going to talk about one other framework for agent improvement referred to as CrewAI.
Studying Aims
- Find out about AI brokers.
- Discover CrewAI – an open-source instrument for constructing brokers.
- Construct a collaborative AI crew for writing content material.
- Discover real-life use instances of AI brokers.
This text was printed as part of the Information Science Blogathon.
What are AI Brokers?
The language fashions excel at translation, summarizing, and reasoning. Nonetheless, you are able to do a lot with them. One of many methods to totally notice the reasoning potential is to make LLMs agentic. The AI brokers are LLMs augmented with the proper instruments and prompts. These brokers can automate looking, internet scrapping, SQL question execution, file operations, and extra. The brokers use the reasoning capability of LLMs to pick out a instrument primarily based on present necessities. However as an alternative of utilizing a single agent for a activity, we are able to ensemble lots of them to perform complicated duties.
Langchain is the default instrument that involves thoughts when discussing AI brokers. Nonetheless, manually orchestrating AI brokers to carry out collaborative duties could be difficult with Langchain. That is the place CrewAI comes into the image.
What’s CrewAI?
CrewAI is an open-source framework for orchestrating role-playing and autonomous AI brokers. It helps create collaborative AI brokers to perform complicated objectives with ease. The framework is designed to allow AI brokers to imagine roles, delegate duties, and share objectives, very similar to a real-world crew. These are a number of the distinctive options of the CrewAI:
- Position-based Brokers: We will outline brokers with particular roles, objectives, and backstories to offer extra context to LLMs earlier than reply era.
- Activity administration: Outline duties with instruments and dynamically assign them to brokers.
- Inter-agent delegation: The brokers can delegate duties to different brokers to collaborate successfully.
Under is a illustration of the CrewAI thoughts map.

The CrewAI seamlessly integrates with the Langchain ecosystem. This implies we are able to use the Langchain instruments and LLM integrations with CrewAI.
Constructing a Collaborative AI Crew
To grasp CrewAI higher, let’s construct collaborative AI brokers for artistic content material writing. For this, we are going to outline brokers, instruments, and the respective duties for every agent. As it’s a group for content material writing, we are going to outline three separate brokers, like an thought analyst, a author, and an editor. Every agent will probably be assigned a activity.
The analyst agent will probably be liable for analyzing the thought and getting ready a complete blueprint for writing the content material. The Author agent will put together the draft for the article, and at last, the editor will probably be liable for formatting, modifying, and correcting the draft. As we all know, CrewAI lets us increase brokers with customized instruments. We are going to increase the editor with a instrument to reserve it to the native disk. However to perform all this stuff, we want an LLM. Right here, we are going to use Google’s Gemini mannequin.
Let’s delve into the coding
As with all Python undertaking, create a digital setting and set up the dependencies. We are going to want the Crewai library and Langchain’s implementation of Google GenAI. You should use different LLMs, like open-access fashions from Collectively, Any scale, or OpenAI fashions.
pip set up crewai langchain-google-genai
The subsequent step is to outline our LLM and collaborative Brokers. Create a separate file named brokers.py to outline brokers.
import os
from crewai import Agent
from langchain.instruments import instrument
from langchain_google_genai import GoogleGenerativeAI
GOOGLE_API_KEY = "Your Key"
llm = GoogleGenerativeAI(
mannequin="gemini-pro",
google_api_key=GOOGLE_API_KEY
)
Let’s outline the file-saving instrument.
class FileTools:
@instrument("Write File with content material")
def write_file(information: str):
"""Helpful to write down a file to a given path with a given content material.
The enter to this instrument needs to be a pipe (|) separated textual content
of size two, representing the total path of the file,
together with the ./lore/, and the written content material you wish to write to it.
"""
strive:
path, content material = information.break up("|")
path = path.substitute("n", "").substitute(" ", "").substitute("`", "")
if not path.startswith("./lore"):
path = f"./lore/path"
with open(path, "w") as f:
f.write(content material)
return f"File written to path."
besides Exception:
return "Error with the enter format for the instrument."
The above write_file methodology is adorned with Langchain’s instrument operate. Because the CrewAI makes use of Langchain underneath the hood, the instruments should adjust to Langchain’s conventions. The operate expects a single string with two elements, a file path, and content material separated by a pipe (|). The strategy doc strings are additionally used as added context for the operate. So, be sure to give detailed details about the strategy.
Let’s outline the brokers
idea_analyst = Agent(
position = "Thought Analyst",
purpose = "Comprehensively analyse an thought to organize blueprints for the article to be written",
backstory="""You're an skilled content material analyst, nicely versed in analyzing
an thought and getting ready a blueprint for it.""",
llm = llm,
verbose=True
)
author = Agent(
position = "Fiction Author",
purpose = "Write compelling fantasy and sci-fi fictions from the concepts given by the analyst",
backstory="""A famend fiction-writer with 2 instances NYT
a best-selling creator within the fiction and sci-fi class.""",
llm=llm,
verbose=True
)
editor = Agent(
position= "Content material Editor",
purpose = "Edit contents written by author",
backstory="""You're an skilled editor with years of
expertise in modifying books and tales.""",
llm = llm,
instruments=[FileTools.write_file],
verbose=True
)
We’ve got three brokers, every with a distinct position, purpose, and backstory. This info is used as a immediate for the LLM to offer extra context. The editor agent has a writing instrument related to it.
The subsequent factor is to outline duties. For this, create a distinct file duties.py.
from textwrap import dedent
class CreateTasks:
def expand_idea():
return dedent(""" Analyse the given activity thought. Put together complete pin-points
for undertaking the given activity.
Ensure that the concepts are to the purpose, coherent, and compelling.
Be sure you abide by the foundations. Do not use any instruments.
RULES:
- Write concepts in bullet factors.
- Keep away from grownup concepts.
""")
def write():
return dedent("""Write a compelling story in 1200 phrases primarily based on the blueprint
concepts given by the Thought
analyst.
Ensure that the contents are coherent, simply communicable, and charming.
Do not use any instruments.
Be sure you abide by the foundations.
RULES:
- Writing have to be grammatically right.
- Use as little jargon as potential
""")
def edit():
return dedent("""
Search for any grammatical errors, edit, and format if wanted.
Add title and subtitles to the textual content when wanted.
Don't shorten the content material or add feedback.
Create an acceptable filename for the content material with the .txt extension.
You MUST use the instrument to reserve it to the trail ./lore/(your title.txt).
""")
The duties listed here are detailed motion plans you anticipate the brokers to carry out.
Lastly, create the primary.py file the place we assemble the Brokers and Duties to create a useful crew.
from textwrap import dedent
from crewai import Crew, Activity
from brokers import editor, idea_analyst, author
from duties import CreateTasks
class ContentWritingCrew():
def __init__(self, thought):
self.thought = thought
def __call__(self):
duties = self._create_tasks()
crew = Crew(
duties=duties,
brokers=[idea_analyst, writer, editor],
verbose=True
)
consequence = crew.kickoff()
return consequence
def _create_tasks(self):
thought = CreateTasks.expand_idea().format(thought=self.thought)
expand_idea_task = Activity(
description=thought,
agent = idea_analyst
)
write_task = Activity(
description=CreateTasks.write(),
agent=author
)
edit_task = Activity(
description=CreateTasks.edit(),
agent=editor
)
return [expand_idea_task, write_task, edit_task]
if __name__ == "__main__":
dir = "./lore"
if not os.path.exists(dir):
os.mkdir(dir)
thought = enter("thought: ")
my_crew = ContentWritingCrew(thought=thought)
consequence = my_crew()
print(dedent(consequence))
Within the above code, we outlined a ContentWritingCrew class that accepts an thought string from the consumer. The _create_tasks methodology creates duties. The __call__ methodology initializes and kicks off the crew. When you run the script, you’ll be able to observe the chain of actions on the terminal or pocket book. The duties will probably be executed within the order they’re outlined by the crew. Here’s a snapshot of the execution log.

That is the execution log for the ultimate agent. i.e. Editor. It edits the draft obtained from the author’s agent and makes use of the file-writing instrument to save lots of the file with an acceptable filename.
That is the overall workflow for creating collaborative AI brokers with CrewAI. You may pair different Langchain instruments or create customized instruments with environment friendly prompting to perform extra complicated duties.
Right here is the GitHub repository for the codes: sunilkumardash9/ContentWritingAgents.
Replit repository: Sunil-KumarKu17/CollborativeAIAgent
Actual-world Use Circumstances
Autonomous AI brokers can have a whole lot of use instances. From private assistants to digital instructors. Listed here are a couple of use instances of AI brokers.
- Private AI Assistant: Private Assistants will probably be an integral a part of us quickly. A Jarvis-like assistant that processes all of your information gives perception as you go and handles trivial duties by itself.
- Code interpreters: OpenAI’s code interpreter is an excellent instance of an AI agent. The interpreter can run any Python script and output the leads to response to a textual content immediate. That is arguably probably the most profitable agent up to now.
- Digital Instructors: Because the AI tech evolves, we are able to anticipate digital instructors in lots of fields like schooling, coaching, and many others.
- Agent First Software program: An enormous potential use case of AI brokers is in agent first software program improvement. As a substitute of manually looking and clicking buttons to get issues finished, AI brokers will routinely accomplish them primarily based on voice instructions.
- Spatial Computing: Because the AR/VR tech evolves, AI brokers will play an important position in bridging the hole between the digital and actual world.
Conclusion
We’re nonetheless within the early levels of AI agent improvement. At present, for the very best final result from AI brokers, we have to depend on GPT-4, and it’s costly. However because the open-source fashions catch as much as GPT-4, we are going to get higher choices for operating AI brokers effectively at an affordable price. Alternatively, the frameworks for agent improvement are progressing quickly. As we transfer ahead, the frameworks will allow brokers to carry out much more complicated duties.
Key Takeaways
- AI brokers leverage the reasoning capability of LLMs to pick out acceptable instruments to perform complicated duties.
- CrewAI is an open-source framework for constructing collaborative AI brokers.
- The distinctive characteristic of CrewAI contains role-based Brokers, autonomous inter-agent delegation, and versatile activity administration.
- CrewAI seamlessly integrates with the present Langchain ecosystem. We will use Langchain instruments and LLM integrations with CrewAI.
Regularly Requested Questions
A. AI brokers are software program packages that work together with their setting, make choices, and act to realize an finish purpose.
A. This depends upon your use instances and price range. GPT 4 is probably the most succesful however costly, whereas GPT 3.5, Mixtral, and Gemini Professional fashions are much less certified however quick and low cost.
A. CrewAI is an open-source framework for orchestrating role-playing and autonomous AI brokers. It helps create collaborative AI brokers to perform complicated objectives with ease.
A. CrewAI gives a high-level abstraction for constructing collaborative AI brokers for complicated workflows.
A. In Autogen, orchestrating brokers’ interactions requires further programming, which might grow to be complicated and cumbersome as the dimensions of duties grows.
The media proven on this article shouldn’t be owned by Analytics Vidhya and is used on the Writer’s discretion.