


Click the blue text above to follow us

1. Introduction
-
Understanding AI agents. -
Exploring CrewAI – an open-source tool for building agents. -
Learning how to build a collaborative content AI team. -
Understanding real-life application cases of AI agents.

2. What are AI Agents?
When we talk about how to build AI agents, the first tool that comes to mind is LangChain. However, manually coordinating AI agents to perform collaborative tasks using LangChain can be quite challenging. CrewAI was born to solve this problem.

3. What is CrewAI?
-
Role-based AI agent design: Define agents with specific roles, goals, and backstories to provide richer context for LLMs before generating responses. -
Flexible task management: Define tasks through customizable tools and dynamically assign them to different agents. -
Autonomous delegation mechanism between agents: Agents can autonomously decide on task assignments and collaborate, significantly improving problem-solving efficiency. -
Process-driven strategies: Currently, the system only supports sequential task execution and hierarchical process organization. CrewAI is developing more complex process management methods, such as consensus-based and autonomous decision-making processes. -
Saving task outputs: Allows saving task-generated data as files for later use. -
Output parsing functionality: Task outputs can be parsed into Pydantic models or JSON format as needed. -
Support for open-source model integration: You can leverage OpenAI or other open-source models to run your team.

4. How CrewAI Works

5. Building Collaborative AI Teams
If you want to learn more about CrewAI, we can try to build a collaborative AI team for creative writing. First, we need to set the roles of the agents, the tools, and the specific tasks for each agent. In this content creation team, we envision three roles: creative analyst, writer, and editor. Each role will take on specific tasks.
The creative analyst is responsible for analyzing the topic and developing a detailed writing outline. The writer will draft the article based on the outline. Finally, the editor will format, edit, and proofread the draft. As we know, CrewAI allows us to add custom tools for agents. For example, we can add a tool for the editor agent to save documents to the local hard drive. To achieve this functionality, we need a Large Language Model (LLM). In this example, we chose Google’s Gemini model.
Next, let’s start coding.
As with any Python project, first create a virtual environment and install the necessary dependencies. We need the CrewAI library and the Google GenAI implementation provided by LangChain. Of course, you can also choose other Large Language Models, such as Anthropic, Ollama, Tongyi Qwen, or the open models provided by OpenAI.
pip install crewai langchain-google-genai
import os
from crewai import Agent
from langchain.tools import tool
from langchain_google_genai import GoogleGenerativeAI
GOOGLE_API_KEY = "Your Key"
llm = GoogleGenerativeAI(
model="gemini-pro",
google_api_key=GOOGLE_API_KEY
)
class FileTools:
@tool("Write File with content")
def write_file(data: str):
"""This tool is used to write specified content to a file at a specific path.
The input format should be a string separated by a pipe (|) containing two parts: the full path of the file (e.g., ./lore/...) and the specific content you want to write to the file.
"""
try:
path, content = data.split("|")
path = path.replace("\n", "").replace(" ", "").replace("`", "")
if not path.startswith("./lore"):
path = f"./lore/{path}"
with open(path, "w") as f:
f.write(content)
return f"File written to {path}."
except Exception:
return "Error with the input format for the tool."
idea_analyst = Agent(
role = "Creative Analyst",
goal = "Deeply analyze the idea and create a detailed outline for writing.",
backstory="""As a seasoned content analyst, you excel at delving into ideas and formulating a complete writing plan.""",
llm = llm,
verbose=True
)
writer = Agent(
role = "Novelist",
goal = "Create engaging fantasy and science fiction works based on the ideas provided by the analyst.",
backstory="""As a renowned novelist, you have topped the "People's Literature" bestseller list twice, specializing in fiction and science fiction.""",
llm=llm,
verbose=True
)
editor = Agent(
role= "Content Editor",
goal = "Carefully edit the content written by the writer.",
backstory="""As an experienced editor, you have years of professional experience editing books and stories, ensuring quality in the works.
Try sharing again""",
llm = llm,
tools=[FileTools.write_file],
verbose=True
)
from textwrap import dedent
class CreateTasks:
def expand_idea():
return dedent("""Analyze the given task {idea}. Prepare a comprehensive outline for the given task.
Ensure that the ideas are relevant, coherent, and engaging.
Ensure compliance with the rules. Do not use any tools.
Rules:
- List ideas in bullet points.
- Avoid adult content ideas.
""")
def write():
return dedent("""Write an engaging 1200-word story based on the blueprint idea provided by the creative analyst.
Ensure the content is coherent, easy to convey, and engaging.
Do not use any tools.
Ensure compliance with the rules.
Rules:
- Writing must be grammatically correct.
- Use terminology as little as possible
""")
def edit():
return dedent("""
Look for any grammatical errors, edit and format (if needed).
Add titles and subtitles to the text where necessary.
Do not shorten the content or add comments.
Create a suitable filename for the content, using the .txt extension.
You must use the tool to save it to the path ./lore/(your title.txt) .
""")
Here, the tasks are the specific action plans you want the agents to execute.
Finally, create a main.py file where we will combine the agents and tasks to build a fully functional team.
from textwrap import dedent
from crewai import Crew, Task
from agents import editor, idea_analyst, writer
from tasks import CreateTasks
class ContentWritingCrew():
def __init__(self, idea):
self.idea = idea
def __call__(self):
tasks = self._create_tasks()
crew = Crew(
tasks=tasks,
agents=[idea_analyst, writer, editor],
verbose=True
)
result = crew.kickoff()
return result
def _create_tasks(self):
idea = CreateTasks.expand_idea().format(idea=self.idea)
expand_idea_task = Task(
description=idea,
agent = idea_analyst
)
write_task = Task(
description=CreateTasks.write(),
agent=writer
)
edit_task = Task(
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)
idea = input("idea: ")
my_crew = ContentWritingCrew(idea=idea)
result = my_crew()
print(dedent(result))

6. Application Cases of AI Agents

7. Conclusion
7.1 Key Points

7.1 Key Points

-
AI agents utilize the reasoning capabilities of Large Language Models (LLMs) to select appropriate tools for completing complex tasks. -
CrewAI is an open-source framework for building collaborative AI agents. -
CrewAI’s unique features include role-based agent design, autonomous delegation between agents, and flexible task management. -
CrewAI can seamlessly integrate with existing LangChain ecosystems, allowing us to use LangChain tools and Large Language Models (LLMs) together with CrewAI.

8. References
[1]. CrewAI GitHub:
https://github.com/joaomdmoura/crewAI
[2]. Google GenAI:
https://python.langchain.com/docs/integrations/llms/google_ai
[3]. Ollama:
https://python.langchain.com/docs/integrations/llms/ollama
[4]. Tongyi Qwen:
https://python.langchain.com/docs/integrations/llms/tongyi
[5]. Building AI Agents:
https://www.analyticsvidhya.com/blog/2024/01/building-collaborative-ai-agents-with-crewai/
[6]. CrewAI Docs:
https://docs.crewai.com/
[7]. CrewAI Examples:
https://github.com/joaomdmoura/crewAI-examples
