Building Efficient Collaborative AI Agents with CrewAI

Building Efficient Collaborative AI Agents with CrewAI
Building Efficient Collaborative AI Agents with CrewAI
Building Efficient Collaborative AI Agents with CrewAI

Click the blue text above to follow us

Building Efficient Collaborative AI Agents with CrewAI

1. Introduction

AI Agents development is a hot topic in the current software innovation field. With the continuous advancement of Large Language Models (LLMs), the integration of AI agents with existing software systems is expected to see explosive growth. With AI agents, we can accomplish tasks that previously required manual operation of applications through simple voice or gesture commands. However, the development of AI agents is still in its early stages, similar to the internet in the 1990s, where we are exploring the basic stages of infrastructure, tools, and framework development. This article will introduce a new agent development framework called CrewAI.
This article mainly includes the following content
  • 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.
Building Efficient Collaborative AI Agents with CrewAI

2. What are AI Agents?

Language models excel in translation, summarization, and reasoning. However, their potential extends far beyond that. Giving Large Language Models (LLMs) agency is a way to fully tap into their reasoning potential.AI agents are LLMs endowed with the appropriate tools and instructions that can automatically perform tasks such as web browsing, web scraping, executing SQL queries, and file operations. Utilizing the reasoning capabilities of LLMs, these agents can choose the appropriate tools based on current needs. Furthermore, we can combine multiple agents to accomplish more complex tasks.

Building Efficient Collaborative AI Agents with CrewAI

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.

Building Efficient Collaborative AI Agents with CrewAI

3. What is CrewAI?

CrewAI is an open-source framework specifically designed to coordinate AI agents with role-playing and autonomous operation. It enables us to easily create AI agents that can collaboratively achieve complex goals.The framework is designed to allow AI agents to play different roles, assign tasks, and share goals, just like team members in the real world. Some key features of CrewAI include:
  • 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.

Building Efficient Collaborative AI Agents with CrewAI

CrewAI can seamlessly integrate with the LangChain ecosystem. This means we can leverage the tools provided by LangChain and the integration capabilities of Large Language Models (LLMs) to work together with CrewAI.
CrewAI is a state-of-the-art framework designed to enhance collaboration among AI agents. This innovative approach addresses the limitations of existing solutions like Autogen and ChatDev by creating an environment where AI agents can operate more effectively as a unified team.
AutoGen has played a significant role in facilitating the creation of collaborative dialogue agents. However, it encounters difficulties when it comes to coordinating interactions between agents, especially for larger tasks. On the other hand, ChatDev introduces the concept of processes, but lacks flexibility and scalability, making it less suitable for practical applications. CrewAI overcomes these challenges and sets a new standard for AI collaboration.

Building Efficient Collaborative AI Agents with CrewAI

4. How CrewAI Works

Behind CrewAI, each agent is fundamentally based on LangChain agents, but they are endowed with special capabilities through ReActSingleInputOutputParser. This specially designed parser not only optimizes role-playing functions but also adds binding stop words for enhancing contextual focus, achieving task continuity through LangChain’s conversation summary memory mechanism.
Since these agents are built on LangChain, they inherently bring about the so-called “flywheel effect,” with the most direct benefit being seamless access to all tools and toolkits provided by LangChain, significantly broadening application scenarios.
The current version of agents can operate autonomously, selecting suitable tools through internal dialogue. However, CrewAI plans to introduce more diverse process types in future versions, supporting collaboration in different team configurations and enabling the ability to dynamically assign tasks at runtime.
Tasks are assigned to agents from the start and can adjust the tools available to agents as needed, allowing for flexible guidance to complete different tasks while avoiding the burden of assigning too many tools to them.
Crew is essentially a framework that encompasses agents and tasks, facilitating their sequential completion of work. In practice, adopting a modular deployment approach with multiple independent Crews is often more efficient, with each Crew containing a small number of agents. This approach not only allows each Crew to achieve different outcomes but also avoids bottlenecks that may occur when a single large Crew handles numerous tasks and agents.

Building Efficient Collaborative AI Agents with CrewAI

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.

Note: CrewAI can run locally using Ollama (an open-source library for AI agent development). This feature enables seamless integration with existing systems and eliminates reliance on external APIs, ensuring data privacy and security.
pip install crewai langchain-google-genai
First, we need to define our LLM and collaborative agents. To do this, create a file named agents.py to define these agents.
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
           )
Next, we define a file-saving tool.
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."
The above write_file method uses LangChain’s tool function for decoration. Since CrewAI uses LangChain in the background, the tool must comply with LangChain’s specifications. This method expects to receive a string containing the file path and content, separated by a pipe (|). The method’s docstring also serves as additional context for the function, so be sure to provide detailed method information.
Next, let’s define the agents.
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
)
We have three agents, each with different roles, goals, and backstories. This information will serve as prompts to help the LLM better understand the context. The editor agent is also associated with a writing tool.
Next, we need to define the tasks. To do this, create a file named tasks.py.
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))
In the above code, we define a class called ContentWritingCrew that accepts a user-inputted creative topic. The _create_tasks method is used to create tasks, while the __call__ method is used to initialize and start the team. When you run the script, you can observe a series of actions being executed in the terminal or notebook. Tasks will be executed in the order defined by the team. Below is an example of the execution log.

Building Efficient Collaborative AI Agents with CrewAI

This is the execution log of the final agent, the editor. It edited the draft provided by the writer agent and saved the file with an appropriate filename using the file writing tool.
This is the basic process of creating collaborative AI agents using CrewAI. You can also combine other LangChain tools or create custom tools to accomplish more complex tasks through effective prompting.

Building Efficient Collaborative AI Agents with CrewAI

6. Application Cases of AI Agents

Autonomous AI agents have a wide range of application prospects in real life. From personal assistants to virtual coaches, here are some practical application cases for AI agents.
Personal AI Assistants: In the near future, personal assistants will become a part of our daily lives. Imagine having a smart assistant like Jarvis that can handle all your data, provide useful information in real time, and automatically complete some daily chores.
Code Interpreters: OpenAI’s code interpreter demonstrates the powerful potential of AI agents. This interpreter can execute any Python script and return results based on text prompts. This may be one of the most successful applications of AI agents to date.
Virtual Coaches: With the continuous advancement of AI technology, we can foresee a large number of virtual coaches emerging in education, training, and other fields.
Agent-first Software Development: AI agents have immense application potential in software development. Compared to traditional manual operations, AI agents can automatically complete various tasks based on voice commands.
Spatial Computing: With the continuous development of Augmented Reality (AR) and Virtual Reality (VR) technologies, AI agents will play a crucial role in bridging the virtual world and the real world.

Building Efficient Collaborative AI Agents with CrewAI

7. Conclusion

Although we are still in the early stages of AI agent development, achieving optimal performance for AI agents currently relies on GPT-4, which may be relatively expensive. However, as open-source models gradually catch up with GPT-4, we will have more options to run AI agents efficiently at reasonable costs. Meanwhile, the frameworks for agent development continue to evolve, enabling agents to perform more complex tasks in the future.

7.1 Key Points

Building Efficient Collaborative AI Agents with CrewAI

  • 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.
Building Efficient Collaborative AI Agents 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

Building Efficient Collaborative AI Agents with CrewAI
If you are interested in this article and want to learn more about AI practical skills, you canfollow the “Technical Wave AI” public account. Here, you can see the latest and hottest AIGC field articles and case practical tutorials.

Leave a Comment