ADaSci Banner 2024

Integrating CrewAI and Ollama for Building Intelligent Agents

Discover how CrewAI and Ollama collaborate to create intelligent, efficient AI agents for complex task management.
CrewAI

In the dynamic world of artificial intelligence, efficiency, and specialization are crucial for managing and executing complex tasks. Enter CrewAI, a groundbreaking platform designed to enhance collaboration among AI agents. By assigning distinct roles and integrating powerful tools, CrewAI enables multiple AI agents to work together seamlessly, resembling a well-coordinated human team. This innovative framework not only optimizes task execution but also elevates the potential of AI-driven solutions in various fields, from content creation and market research to customer service and beyond. In this article, we will understand what is CrewAI and also see its implementation with Ollama to build a multi-agent.

Table of Contents

  1. What is CrewAI?
  2. Key Features of CrewAI
  3. The Integration with Ollama
  4. Implementation of CrewAI and Ollama to build a Blog Writing Agent

Let us deep dive into understanding what CrewAI is and also understand how to implement CrewAI and Ollama together to build a multi-agent.

What is CrewAI?

In the rapidly evolving landscape of artificial intelligence, collaboration and specialization are key to tackling complex tasks efficiently. CrewAI is an innovative framework designed to orchestrate role-playing, autonomous AI agents, fostering collaborative intelligence. By enabling agents to assume distinct roles and work together seamlessly, CrewAI is transforming how tasks are managed and executed in various domains, from research and content creation to customer service and beyond.

Key Features of CrewAI

Role-Based Agent Design

CrewAI allows for the creation of agents with specific roles, goals, and backstories. This enables each agent to specialize in certain tasks, enhancing overall efficiency and effectiveness.

Multi-Agent Collaboration

Agents can delegate tasks, share information, and assist one another, ensuring a cohesive and productive workflow. This feature is essential for handling complex, multi-faceted tasks that require input from various specialized agents.

Tool Integration

The platform supports a wide range of tools, from web search and data analysis to content generation. These tools can be seamlessly integrated into the agents’ workflows, boosting their capabilities and efficiency​​.

Flexible Task Management

CrewAI offers robust task management processes, including sequential and hierarchical task execution. This flexibility allows developers to design workflows that best suit their needs​​.

Multiagent Collaboration in Action

Unlike traditional AI systems that rely on a single, powerful agent, CrewAi embraces the power of teamwork. Developers can create “crews” consisting of diverse agents, including:

Large Language Models (LLMs)

These powerhouses excel at text generation, translation, and question-answering.

User-defined Agents

These custom-built agents handle specific tasks, such as data analysis or knowledge base integration.

Assistant Agents

These support the team by managing conversation flow, retrieving information, or triggering actions within CrewAi.

This collaborative approach offers significant advantages:

  • Efficiency: By dividing tasks and leveraging specialized agents, CrewAi tackles complex workflows more efficiently.
  • Flexibility: The open-source nature allows developers to tailor agents to specific needs, ensuring a perfect fit for the project.
  • Structured Approach: CrewAi provides a framework for communication and collaboration, keeping the team on track and achieving optimal results.

The Integration with Ollama

LLMs are a central component of many CrewAi projects. They bring the power of language processing and generation to the table. However, integrating LLMs with CrewAi requires careful consideration. This is where Ollama steps in.

Ollama is a user-friendly interface that simplifies LLM access and management. It allows developers to:

Run Local LLMs

Instead of relying on cloud-based services, Ollama empowers developers to utilize powerful LLMs on their machines.

Simplified Configuration

Ollama streamlines the configuration process, making it easier to connect LLMs to CrewAi.

Enhanced Control

Developers gain more control over LLM behavior. This allows them to fine-tune parameters and ensure optimal performance within CrewAi.

Implementation of CrewAI and Ollama to build a Blog Writing Agent

We will now see how to use CrewAI with LLM and Ollama. We will be creating a content writing agent using CrewAI and LLM. We will use CrewAI as a multi-agent controller. Ollama will help us easily access LLM and utilize it to the full extent in the local environment. 

To begin with we have to have Ollama installed in our system. Then, in VS code, we will create a main.py file and a setup folder. In the setup folder, we will create two files – Model.sh and Modelfile. 

After setting up Ollama, we have to pull llama2, for that, we have to give the command ollama pull llama2 in the command prompt or terminal. 

Modelfile will consist of the following commands:

FROM llama3

# Set parameters

PARAMETER temperature 0.8
PARAMETER stop Result

# Sets a custom system message to specify the behavior of the chat assistant

# Leaving it blank for now.

SYSTEM """"""

Create a script to get the base model, which is llama2, and create a model on top of that with ModelFile above. The Model.sh file will consist of

# variables
model_name="llama2"
custom_model_name="crewai-llama2"

#get the base model
ollama pull $model_name

#create the model file
ollama create $custom_model_name -f ./ModelFile

Now go to the terminal and give the command: chmod +x ./Model.sh. This will pull the custom model we have created.

We now have everything set up to build agents and execute the task. We will now install all the required packages for the project.

%pip install crewai==0.28.8 crewai_tools==0.1.6 langchain_community==0.0.29

Import all the libraries:

from crewai import Agent, Task, Crew
from langchain_openai import ChatOpenAI
import os

We will call our OpenAI API Key here:

os.environ["OPENAI_API_KEY"] = "sk-****"

Let us now create agents one by one. Here we will build three different agents. The agents all together will create content based on the given topic. 

The first agent we will build is “Content Planner”.

planner = Agent(
   role="Content Planner",
   goal="Plan engaging and factually accurate content on {topic}",
   backstory="You're working on planning a blog article "
             "about the topic: {topic} in 'https://adasci.org/'."
             "You collect information that helps the "
             "audience learn something "
             "and make informed decisions. "
             "You have to prepare a detailed "
             "outline and the relevant topics and sub-topics that has to be a part of the"
             "blogpost."
             "Your work is the basis for "
             "the Content Writer to write an article on this topic.",
   llm=llm,
   allow_delegation=False,
verbose=True
)

The second agent is “Content Writer”.

writer = Agent(
   role="Content Writer",
   goal="Write insightful and factually accurate "
        "opinion piece about the topic: {topic}",
   backstory="You're working on a writing "
             "a new opinion piece about the topic: {topic} in 'https://adasci.org/'. "
             "You base your writing on the work of "
             "the Content Planner, who provides an outline "
             "and relevant context about the topic. "
             "You follow the main objectives and "
             "direction of the outline, "
             "as provide by the Content Planner. "
             "You also provide objective and impartial insights "
             "and back them up with information "
             "provide by the Content Planner. "
             "You acknowledge in your opinion piece "
             "when your statements are opinions "
             "as opposed to objective statements.",
   allow_delegation=False,
   llm=llm,
   verbose=True
)

The third agent is the “Editor”.

editor = Agent(
   role="Editor",
   goal="Edit a given blog post to align with "
        "the writing style of the organization 'https://medium.com/'. ",
   backstory="You are an editor who receives a blog post "
             "from the Content Writer. "
             "Your goal is to review the blog post "
             "to ensure that it follows journalistic best practices,"
             "provides balanced viewpoints "
             "when providing opinions or assertions, "
             "and also avoids major controversial topics "
             "or opinions when possible.",
   llm=llm,
   allow_delegation=False,
   verbose=True
)

Now, we will define the tasks to be achieved by the agents.

This is the task of “Content Planner”:

plan = Task(
   description=(
       "1. Prioritize the latest trends, key players, "
           "and noteworthy news on {topic}.\n"
       "2. Identify the target audience, considering "
           "their interests and pain points.\n"
       "3. Develop a detailed content outline including "
           "an introduction, key points, and a call to action.\n"
       "4. Include SEO keywords and relevant data or sources."
   ),
   expected_output="A comprehensive content plan document "
       "with an outline, audience analysis, "
       "SEO keywords, and resources.",
   agent=planner,
)

This is the task of “Content Writer”:

write = Task(
   description=(
       "1. Use the content plan to craft a compelling "
           "blog post on {topic}.\n"
       "2. Incorporate SEO keywords naturally.\n"
 "3. Sections/Subtitles are properly named "
           "in an engaging manner.\n"
       "4. Ensure the post is structured with an "
           "engaging introduction, insightful body, "
           "and a summarizing conclusion.\n"
       "5. Proofread for grammatical errors and "
           "alignment with the brand's voice.\n"
   ),
   expected_output="A well-written blog post "
       "in markdown format, ready for publication, "
       "each section should have 2 or 3 paragraphs.",
   agent=writer,
)

This is the task of the “Editor”:

edit = Task(
   description=("Proofread the given blog post for "
                "grammatical errors and "
                "alignment with the brand's voice."),
   expected_output="A well-written blog post in markdown format, "
                   "ready for publication, "
                   "each section should have 2 or 3 paragraphs.",
   agent=editor
)

We will now build our CrewAI multi-agent consisting of all these agents:

crew = Crew(
   agents=[planner, writer, editor],
   tasks=[plan, write, edit],
   verbose=2
)

We can initiate the content writing process now that all our agents are ready.

inputs = {"topic":"Write about any of the articles written by author Shreepradha Hegde."}
result = crew.kickoff(inputs=inputs)

We will now display the overall output.

from IPython.display import Markdown,display
display(Markdown(result))

Thus, we can integrate CrewAI and Ollama to build a multi-agent that will work effectively based on the defined task. It is important to remember that the working of each agent depends on the way we define it and how we give the task to it. 

Conclusion

CrewAI, when combined with advanced language models and platforms like Ollama, represents a significant step forward in the field of AI collaboration. By enabling specialized agents to work together seamlessly, CrewAI enhances productivity and efficiency in handling complex tasks. This innovative approach not only simplifies workflows but also opens up new possibilities for AI-driven automation across various industries.

References

  1. Link to Code
  2. CrewAI Documentation
  3. LangChain – CrewAI Documentation

Learn more about Multi-Agent LLMs and Applications of LLMs by joining following courses.

Picture of Shreepradha Hegde

Shreepradha Hegde

Shreepradha is an accomplished Associate Lead Consultant at AIM, showcasing expertise in AI and data science, specifically Generative AI. With a wealth of experience, she has consistently demonstrated exceptional skills in leveraging advanced technologies to drive innovation and insightful solutions. Shreepradha's dedication and strategic mindset have made her a valuable asset in the ever-evolving landscape of artificial intelligence and data science.

The Chartered Data Scientist Designation

Achieve the highest distinction in the data science profession.

Elevate Your Team's AI Skills with our Proven Training Programs

Strengthen Critical AI Skills with Trusted Generative AI Training by Association of Data Scientists.

Our Accreditations

Get global recognition for AI skills

Chartered Data Scientist (CDS™)

The highest distinction in the data science profession. Not just earn a charter, but use it as a designation.

Certified Data Scientist - Associate Level

Global recognition of data science skills at the beginner level.

Certified Generative AI Engineer

An upskilling-linked certification initiative designed to recognize talent in generative AI and large language models

Join thousands of members and receive all benefits.

Become Our Member

We offer both Individual & Institutional Membership.