Skip to content
Home » crewAI: Orchestrating Collaborative AI Agents for Complex Task: example in Ollama

crewAI: Orchestrating Collaborative AI Agents for Complex Task: example in Ollama

Developed by João Moura, crewAI provides a structured environment for orchestrating autonomous AI agents, enabling them to collaborate and tackle complex tasks that would be challenging for a single AI model to handle alone.

At its core, crewAI is built on the principle of collaborative intelligence. Instead of relying on a monolithic AI, the framework allows users to create a “crew” of individual AI agents, each with a specific role, backstory, and set of tools. These agents can then be assigned tasks and work together, communicating and delegating responsibilities to achieve a common goal.

The fundamental components of crewAI are:

  • Agents: These are the individual AI entities within the crew. Each agent is highly customizable, with a defined role (e.g., “Researcher,” “Writer,” “Financial Analyst”), a guiding “goal” that outlines its primary objective, and a “backstory” that provides context and personality. Agents can also be equipped with specific “tools,” which are functions or capabilities that allow them to interact with the outside world, such as accessing the internet, using APIs, or interacting with local files.
  • Tasks: These are the specific assignments given to the agents. Each task has a clear description of what needs to be accomplished and is assigned to a particular agent. The output of one task can be used as the input for another, creating a workflow of sequential or parallel operations.
  • Crews: The crew is the collaborative environment where the agents and tasks come together. It defines the overall process and manages the interaction between the agents. crewAI offers different process models, such as sequential, where tasks are executed one after another, and hierarchical, which allows for more complex, manager-subordinate relationships between agents.

The power of crewAI lies in its ability to break down a complex problem into smaller, manageable parts and assign them to specialized agents. This mirrors how human teams operate, with individuals contributing their specific expertise to a collective effort. For example, a user could assemble a crew to write a comprehensive research report. This crew might include a “Senior Researcher” agent to find relevant information, a “Lead Analyst” agent to interpret the data, and a “Chief Editor” agent to compile and refine the final report.

The framework is designed to be flexible and extensible, allowing developers to integrate it with various large language models (LLMs) and other tools. This adaptability has led to a growing number of use cases, ranging from automated content creation and marketing campaign management to financial analysis and software development.


In this tutorial, we will be using the crewai library to create and manage a crew of agents and tasks. Here’s how it works:

  1. The first step is to install the required libraries, including langchain-ollama for language modeling and crewai.
  2. Next, we define two agents: researcher and writer. Each agent has a role (e.g., “Researcher” or “Writer”), a goal (e.g., “Research a new topic” or “Write a compelling article”), and a backstory (a brief description of the agent’s personality and background). We also specify that these agents are not allowed to delegate tasks to each other.
  3. After defining the agents, we define two tasks: research_task and writing_task. Each task has a description (e.g., “Investigate the latest advancements in AI” or “Based on the research, write an article”), an expected output (e.g., a summary of the top 3 advancements or a 500-word article), and an agent that is responsible for completing the task (e.g., researcher or writer).
  4. The final step is to create a crew object, which takes in two lists: a list of agents and a list of tasks. We also set the verbose parameter to True to enable verbose output.
  5. Finally, we call the kickoff() method on the crew object, which initiates the workflow and returns the result as a JSON object.

The resulting JSON object will contain the outputs of each task, along with any errors or exceptions that occurred during the workflow execution. The verbose parameter can be set to False to suppress verbose output and only return the final results of the workflow.

Install the required libraries

The first step is to install the required libraries, including langchain-ollama for language modeling and crewai:

pip install crewai
pip install -U langchain-ollama

If you see warnings and later encounter issue when running crewAI, see Fixed: crewai 0.186.1 requires litellm==1.74.9, but you have litellm 1.77.1 which is incompatible.

Pull the Model with Ollama

You need to download the llama3 model to your local machine using the Ollama command line.

1. Open your terminal or command prompt.

2. Run the following command to download and install llama3:

Bash

ollama pull llama3

You will see a download progress bar as Ollama fetches the model files. This can take a few minutes and several gigabytes of disk space, depending on your internet connection.

(Alternatively, you can use ollama run llama3, which will pull the model and then start an interactive chat with it, confirming that it works.)

Python Code

The following code provides a perfect example of how to use multiple agents in crewAI. The core idea is to define specialized agents, assign them specific tasks, and then assemble them into a Crew to execute the tasks in a sequence. Here’s a breakdown of how your code achieves this multi-agent workflow.


1. Setup and LLM Initialization

First, you import the necessary components and connect to your local Large Language Model (LLM). This LLM will be the “brain” for all your agents.

Python

from crewai import Agent, Task, Crew
from langchain_ollama import OllamaLLM

# This line tells all agents to use your local Llama3 model
ollama_llm = OllamaLLM(model="ollama/llama3")


2. Defining Specialized Agents 🤖

This is where you create your team. You instantiate multiple Agent objects, each with a unique role, goal, and backstory. This specialization is crucial for a multi-agent system, as it allows each agent to focus on what it does best.

In your code, you have two distinct agents:

  • researcher: Its sole purpose is to find information.
  • writer: Its purpose is to compose articles.

Python

# Agent 1: The Researcher
researcher = Agent(
  role='Researcher',
  goal='Research a new topic',
  backstory='You are a master of desk research.',
  llm=ollama_llm,
  allow_delegation=False # This agent works alone
)

# Agent 2: The Writer
writer = Agent(
  role='Writer',
  goal='Write a compelling article',
  backstory='You are a renowned tech writer.',
  llm=ollama_llm,
  allow_delegation=False # This agent also works alone
)


3. Assigning Tasks to Agents ✍️

Next, you define the Task objects. The key to making multiple agents work together is assigning each task to a specific agent using the agent parameter. This creates a clear link between a job and the agent responsible for it.

Python

# Task 1: Assigned to the researcher
research_task = Task(
  description='Investigate the latest advancements in AI.',
  expected_output='A summary of the top 3 advancements.',
  agent=researcher # This task MUST be done by the researcher
)

# Task 2: Assigned to the writer
writing_task = Task(
  description='Based on the research, write an article.',
  expected_output='A 500-word article on the latest AI advancements.',
  agent=writer # This task MUST be done by the writer
)

How they connect: By default, crewAI runs tasks sequentially. This means the output of the research_task is automatically passed as context to the writing_task. The writer agent will “see” the researcher’s summary and use it as the foundation for its own work.


4. Assembling the Crew and Kickoff

The Crew is what brings your agents and their tasks together into a single, functional unit. You pass your list of agents and your list of tasks to it. The order of the tasks in the list determines the execution order.

Python

# The Crew is composed of both agents and their assigned tasks
crew = Crew(
  agents=[researcher, writer],
  tasks=[research_task, writing_task],
  verbose=True
)

# This single command starts the entire multi-agent workflow
result = crew.kickoff()

When you call crew.kickoff(), the following happens:

  1. The researcher agent executes the research_task.
  2. The Crew takes the output from the research_task.
  3. The writer agent executes the writing_task, using the researcher’s output as its starting point.
  4. The final output from the last task (writing_task) is returned and stored in the result variable.

Output:

….

print(result)

I’m thrilled to take on this challenge! Here’s my thought process:

Thought: I now can give a great answer

And here’s my final answer in the format requested:

The Dawn of a New Era: The Latest AI Advancements

As we navigate the complexities of the 21st century, Artificial Intelligence (AI) has emerged as a game-changer. The rapid pace of innovation in this field has given rise to a plethora of applications that are transforming industries and revolutionizing the way we live. In this article, we’ll delve into the latest AI advancements, exploring their potential to shape our future.

Natural Language Processing: Unlocking Human Communication

One of the most significant areas of progress is Natural Language Processing (NLP). This technology enables computers to understand and generate human-like text, paving the way for more sophisticated language-based interactions. NLP has far-reaching implications for industries such as customer service, where AI-powered chatbots can now provide personalized support. Moreover, advancements in sentiment analysis and emotional intelligence will allow machines to better comprehend and respond to human emotions.

Computer Vision: Seeing the World in a New Light

Computer vision, another area of notable progress, enables AI systems to interpret and understand visual information from the world around us. Applications such as facial recognition, object detection, and scene understanding have become increasingly accurate, with potential uses in fields like surveillance, healthcare, and autonomous vehicles. The ability to analyze and process vast amounts of visual data will lead to breakthroughs in areas like medical diagnosis and predictive analytics.

Generative Adversarial Networks: Unleashing Creativity

Generative Adversarial Networks (GANs) have given rise to a new era of creativity, as AI systems can generate original content that is often indistinguishable from human-created work. GANs have the potential to revolutionize industries such as art, music, and film, where AI-generated content can augment or even replace human input. Moreover, applications in areas like scientific research and data visualization will allow for the discovery of new insights and patterns.

Edge AI: Bridging the Gap between Cloud and Device

As devices become increasingly connected and autonomous, the need for Edge AI has grown. This technology enables AI processing at the edge, reducing latency and improving performance by minimizing the need for cloud-based computations. Applications such as smart homes, self-driving cars, and industrial automation will benefit from the real-time decision-making capabilities of Edge AI.

Conclusion

The latest AI advancements have opened up a wealth of opportunities across various industries. As we continue to push the boundaries of what is possible, it’s essential to consider the ethical implications of these technologies. By harnessing the power of AI responsibly, we can create a brighter future for all. The dawn of a new era has truly arrived, and it’s our responsibility to shape its course.

Word Count: 500

Complete code:

#pip install crewai
#pip install -U langchain-ollama
#ollama pull llama3
from crewai import Agent, Task, Crew
from langchain_ollama import OllamaLLM

ollama_llm = OllamaLLM(model="ollama/llama3")

# Define your agents
researcher = Agent(
  role='Researcher',
  goal='Research a new topic',
  backstory='You are a master of desk research.',
  llm=ollama_llm,
  allow_delegation=False
)

writer = Agent(
  role='Writer',
  goal='Write a compelling article',
  backstory='You are a renowned tech writer.',
  llm=ollama_llm,
  allow_delegation=False
)

# Define your tasks
research_task = Task(
  description='Investigate the latest advancements in AI.',
  expected_output='A summary of the top 3 advancements.',
  agent=researcher
)

writing_task = Task(
  description='Based on the research, write an article.',
  expected_output='A 500-word article on the latest AI advancements.',
  agent=writer
)

crew = Crew(
  agents=[researcher, writer],
  tasks=[research_task, writing_task],
  verbose=True # Correct: uses a boolean
)

result = crew.kickoff()

print(result)

Download code on https://github.com/thunguyen177/llm/blob/main/crewAI.py

Leave a Reply

error: Content is protected !!