How to Build Multi-Agent System with CrewAI and Ollama?
Introduction
Don’t want to spend money on APIs, or are you concerned about privacy? Or do you just want to run LLMs locally? Don’t worry; this guide will help you build agents and multi-agent frameworks with local LLMs that are completely free. We’ll explore how to build a multi-agent system with CrewAI and Ollama and look at the multiple LLMs available from Ollama.
Overview
- This guide focuses on building agentic frameworks and multi-agent systems using local LLMs with CrewAI and Ollama, providing a cost-free and privacy-preserving solution.
- It introduces key concepts of agents and Multi-Agentic Framework with CrewAI and Ollama, emphasizing their role in autonomous, collaborative problem-solving across various industries.
- CrewAI is highlighted as an advanced framework for orchestrating tasks between agents. It uses structured roles, goals, and memory management to improve task execution.
- Ollama enables running language models like Llama2, Llama3, and LLaVA locally, allowing users to bypass cloud services for AI tasks.
- The article includes a practical example of building a Multi-Agent System for image classification, description, and information retrieval using CrewAI and Ollama.
- The conclusion underscores the benefits of using different LLMs for specialized tasks and showcases the flexibility of combining CrewAI and Ollama in local environments.
Table of contents
- Introduction
- Agents, Agentic Frameworks, and CrewAI
- Agentic Frameworks
- CrewAI Framework
- Key Strengths of CrewAI
- Ollama
- Building a Multi-Agent System
- Objectives
- Components
- Let’s Build our Multi-Agent System
- Import Required Libraries
- Define the Agents
- Define Tasks for Each Agent
- Managing Agents and Tasks with a Crew
- Conclusion
- Frequently Asked Questions
Agents, Agentic Frameworks, and CrewAI
Generative AI has transitioned from basic large language models (LLM) to advanced multi-agent systems. In theory, Agents are autonomous systems capable of planning, reasoning, and acting without human input. These agents aim to reduce human involvement while expanding functionality.
Also Read: Top 5 Frameworks for Building AI Agents in 2024
Agentic Frameworks
These frameworks utilize multiple agents working in concert, allowing for collaboration, communication, and problem-solving that exceed the capabilities of single-use agents. In these frameworks, agents have distinct roles and goals and can perform complex tasks. Multi-Agentic Framework such as CrewAI and Ollama are essential for large-scale, dynamic, and distributed problem-solving, making them adaptable across industries like robotics, finance, healthcare, and beyond.
Key Components of Agentic Frameworks
- Agent Architecture: Defines the internal structure of agents, including planning, reasoning, and communication protocols.
- Communication Protocols: Methods for agent collaboration through messaging and data exchange.
- Agent Interaction Design: Mechanisms for agent collaboration, including task allocation and conflict resolution.
- Environment: The setting where agents interact, often including external tools and resources.
These frameworks enable modular and scalable systems, making modifying or adding agents to adapt to evolving requirements easy.
CrewAI Framework
crewAI is an advanced multi-agentic framework, enabling multiple agents (called a “crew”) to collaborate through task orchestration. The framework divides agents into three attributes—role, goal, and backstory—ensuring a thorough understanding of each agent’s function. This structured approach mitigates under-specification risk, improving task definition and execution.
Key Strengths of CrewAI
- Explicit Task Definition: Tasks are well-defined, ensuring clarity in what each agent does.
- Tool Use: Task-specific tools precede agent-level tools, creating a more granular and controlled toolset.
- Agent Interaction Processes: crewAI supports sequential and hierarchical agent collaboration processes.
- Advanced Memory Management: The framework provides short-term, long-term, entity, and contextual memory, facilitating sophisticated reasoning and learning.
Also Read: Building Collaborative AI Agents With CrewAI
Ollama
Ollama is a framework for building and running language models on local machines. It’s easy to use, as we can run models directly on devices without needing cloud-based services. There’s no concern about privacy.
To interact with Ollama:
We can run the pip install ollama command to integrate Ollama with Python.
Now, we can download models with the ollama pull command to download the models.
Let’s run these:
1 2 3 4 5 |
|
Now, we have 3 of the Large Language Models (LLMs) locally:
- Llama 2: An open-source large language model from Meta.
- Llama 3: The latest iteration of Meta’s Llama series, further refining capabilities for complex language generation tasks with increased parameter size and efficiency.
- LLaVA: A vision-language model designed for image and text understanding tasks.
We can use these models locally by running ollama run model-name, here’s an example:
You can press ctrl d to exit.
Also read: How to Run LLM Models Locally with Ollama?
Building a Multi-Agent System
Let’s work on building an Agentic system that takes an image as an input and gives a few interesting facts about the animal in the system.
Objectives
- Build a multi-agent system for image classification, description, and information retrieval using CrewAI.
- Automate decision-making: Agents perform specific tasks like identifying animals in images, describing them, and fetching relevant facts.
- Task sequencing: Coordinate agents through tasks in a stepwise, agentic system.
Components
- Classifier Agent: Identifies whether the input image contains an animal using the llava:7b model.
- Description Agent: Describes the animal in the image, also powered by llava:7b.
- Information Retrieval Agent: Fetches additional facts about the animal using llama2.
- Task Definitions: Each task is tied to a specific agent, guiding its action.
- Crew Management: The Crew coordinates agent actions, executes tasks, and aggregates results based on the input image
By default, tasks are executed sequentially in CrewAI. You can add a task manager to control the order of execution. Additionally, the allow_delegation feature allows an agent to ask its preceding agent to regenerate a response if needed. Setting memory to True enables agents to learn from past interactions, and you can optionally configure tasks to ask for human feedback about the output.
Also read: Building Collaborative AI Agents With CrewAI
Let’s Build our Multi-Agent System
Before we start, let’s install all the necessary packages:
1 2 3 4 5 |
|
Import Required Libraries
1 2 3 4 5 6 7 8 9 10 |
|
Define the Agents
Here, we define three agents with specific roles and goals. Each agent is responsible for a task related to image classification and description.
- Classifier Agent: Checks if the image contains an animal, uses llava:7b model to classify the animal.
- Description Agent: Describes the animal in the image. This also uses the same llava:7b model like the preceding agent.
- Information Retrieval Agent: This agent retrieves additional information or interesting facts about the animal. It uses llama2 to provide this information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
Also Read: Agentic Frameworks for Generative AI Applications
Define Tasks for Each Agent
Each task is tied to one of the agents. Tasks describe the input, the expected output, and which agent should handle it.
- Task 1: Classify whether the image contains an animal.
- Task 2: If the image is classified as an animal, describe it.
- Task 3: Provide additional information about the animal based on the description.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Managing Agents and Tasks with a Crew
A Crew is set up to manage the agents and tasks. It coordinates the tasks sequentially and provides the results based on the agents’ chains of thought.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
I’ve given an image of a racoon to the crewAI framework, and this is the output that I got:
Note: Ensure the image is in the working directory, or you can give the full path.
OUTPUT
1 |
|
The classifier confirmed that it was an animal, and then the agent with the llava:7b model described the animal and image and sequentially passed it to the information agent. Despite the information agent using llama2, a text-based model, it could use the context from the previous agent and give information about a raccoon.
Also read: Building a Responsive Chatbot with Llama 3.1, Ollama and LangChain
Conclusion
Using multiple LLMs according to their strengths is good because different models excel at different tasks. We have used CrewAI and Ollama to showcase multi-agent collaboration and also used LLMs locally from Ollama. Yes, the Ollama models might be slower than cloud-based models for obvious reasons, but both have pros and cons. The effectiveness of the agentic framework depends on the workflows and the use of the right tools and LLMs to optimize the results.
Frequently Asked Questions
Q1. What’s allow_delegation in CrewAI?Ans. When set to True, this crewAI parameter lets agents assign tasks to others, enabling complex task flows and collaboration.
Q2. How does crewAI use Pydantic objects?Ans. crewAI uses Pydantic objects to define and validate task input/output data structures, ensuring agents receive and produce data in the expected format.
Q3. How does crewAI manage task flow and agent collaboration?Ans. crewAI manages this by organizing agents and tasks into a ‘Crew’ object, coordinating tasks sequentially based on user-defined dependencies.
Q4. Can I use custom LLMs with crewAI and Ollama?Ans. Yes, both support custom LLMs. For crewAI, specify the model path/name when creating an Agent. For Ollama, follow their docs to build and run custom models.
The above is the detailed content of How to Build Multi-Agent System with CrewAI and Ollama?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Meta's Llama 3.2: A Leap Forward in Multimodal and Mobile AI Meta recently unveiled Llama 3.2, a significant advancement in AI featuring powerful vision capabilities and lightweight text models optimized for mobile devices. Building on the success o

Hey there, Coding ninja! What coding-related tasks do you have planned for the day? Before you dive further into this blog, I want you to think about all your coding-related woes—better list those down. Done? – Let’

This week's AI landscape: A whirlwind of advancements, ethical considerations, and regulatory debates. Major players like OpenAI, Google, Meta, and Microsoft have unleashed a torrent of updates, from groundbreaking new models to crucial shifts in le

Shopify CEO Tobi Lütke's recent memo boldly declares AI proficiency a fundamental expectation for every employee, marking a significant cultural shift within the company. This isn't a fleeting trend; it's a new operational paradigm integrated into p

Introduction Imagine walking through an art gallery, surrounded by vivid paintings and sculptures. Now, what if you could ask each piece a question and get a meaningful answer? You might ask, “What story are you telling?

Introduction OpenAI has released its new model based on the much-anticipated “strawberry” architecture. This innovative model, known as o1, enhances reasoning capabilities, allowing it to think through problems mor

SQL's ALTER TABLE Statement: Dynamically Adding Columns to Your Database In data management, SQL's adaptability is crucial. Need to adjust your database structure on the fly? The ALTER TABLE statement is your solution. This guide details adding colu

For those of you who might be new to my column, I broadly explore the latest advances in AI across the board, including topics such as embodied AI, AI reasoning, high-tech breakthroughs in AI, prompt engineering, training of AI, fielding of AI, AI re
