Let AIs do the Jobs with CrewAI

MPyK
The Web Tub
Published in
11 min readApr 12, 2024

--

In this tutorial, we’ll demonstrate how to leverage CrewAI to create and deploy multiple AI agents that can perform a variety of tasks for us. While there are countless tutorials showcasing the capabilities of GPT-4 models, this time we’ll be utilizing a free, open-source model that you can download to your local machine. This will allow you to explore the concept without incurring any costs, and you’ll find that it works remarkably well.

The application we’re creating will task our AI agents with thinking like humans and generating a well-written blog post on any topic we provide. This approach allows us to harness the power of AI to streamline the content creation process, freeing up valuable time and resources that can be devoted to other priorities.

Setting the Stage: Tools and Technologies

Before we dive into the code, let’s take a moment to understand the key components that will power our AI-driven blog.

  1. Ollama: Ollama is a tool to run LLM model locally. It offers a range of pre-trained models, each with its own unique capabilities, which we’ll leverage in our project.
  2. LangChain: LangChain is a framework that simplifies the development of applications powered by large language models. It provides a set of abstractions and tools that make it easier to build complex AI systems.
  3. Chroma: Chroma is a high-performance vector database that integrates seamlessly with LangChain. It allows us to efficiently store and retrieve text embeddings, which are crucial for our question-answering functionality.
  4. crewai: The crewai library is a powerful tool that enables the creation of collaborative AI agents. In our project, we'll use it to build a team of AI "agents" that work together to generate engaging blog content.

The blog cover image picture is credit Forage.

Let’s dive into the step-by-step process of building our conversational AI-powered blog.

Setting up the Environment

  1. Ensure you have Python 3.10 installed on your system.
  2. Install the required Python packages: langchain, crewai, bs4, and chromadb.
pip install langchain
pip install crewai
pip install bs4
pip install chromadb

3. Pull the required Ollama models from the cloud

ollama pull gemma:7b
ollama pull mistral:7b
ollama pull nomic-embed-text
  • gemma: Gemma is a family of lightweight, state-of-the-art open models built by Google DeepMind.
  • mistral: Cost-efficient reasoning for low-latency workloads.
  • nomic-embed-text: A high-performing open embedding model with a large token context window. This model will be used later in the second part of the blog.

AI Agents

Now, it’s time for the coding part.

First, we’ll create a file called blog.py and add the following content:

from langchain_community.llms import Ollama
from crewai import Agent, Task, Crew
import sys

ollama_gemma = Ollama(model='gemma:7b')
ollama_mistral = Ollama(model='mistral:7b')

We’ve just imported all the required libraries. Then, we’ve initialized two AI models that we downloaded from Ollama.

def write_blog(topic):
# Create Researcher agent
researcher = Agent(
role='Senior Research Analyst',
goal=f"Uncover research in {topic}",
backstory="""You work at a leading research company.
Your expertise lies in identifying emerging trends, research and discover.
You have a knack for dissecting complex data and presenting actionable insights.""",
verbose=True,
allow_delegation=False,
llm=ollama_gemma
)

# Create Writer agent
writer = Agent(
role='Content Strategist',
goal=f"Craft compelling content on {topic}",
backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
You transform complex concepts into compelling narratives.""",
verbose=True,
allow_delegation=False,
llm=ollama_mistral
)

# Create tasks for your agents
task1 = Task(
description=f"Conduct a comprehensive analysis of {topic}.",
expected_output="Full analysis report in bullet points",
agent=researcher
)

task2 = Task(
description=f"""Using the insights provided, develop an engaging blog
post that highlights the topic of {topic}.
Your post should be informative yet accessible, catering to any audience, easy to understand.
Make it sound cool, avoid complex words so it doesn't sound like AI.""",
expected_output="Full blog post of at least 4 paragraphs",
agent=writer
)

# Instantiate your crew with a sequential process
crew = Crew(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=2, # You can set it to 1 or 2 to different logging levels
)

# Get your crew to work!
result = crew.kickoff()
return result

Alright, let’s dive into this step-by-step and see how we can use AI to streamline the blog post creation process.

All we need are: 1) Agent to oversee operations, 2) a Task tailored for the Agent’s expertise, 3) to assemble a Crew and delegate tasks accordingly, and 4) to allow the Crew to execute the assigned tasks. That’s all it takes.

First, we define a function called write_blog that takes a topic parameter. This will be the subject of the blog post we want to generate.

Inside the function, we create two AI agents: a researcher agent and a writer agent. The researcher agent is tasked with conducting a comprehensive analysis of the given topic, while the writer agent is responsible for crafting an engaging and informative blog post based on the insights provided by the researcher.

Now that we have our two agents, we define the tasks they’ll be working on. The first task is assigned to the researcher agent, and it involves conducting a comprehensive analysis of the topic and producing a detailed report in bullet point form.

The second task is assigned to the writer agent. This task requires the agent to use the insights provided by the researcher and develop an engaging, informative, and accessible blog post of at least 4 paragraphs. The key here is to ensure the content is easy to understand, avoids complex jargon, and caters to a wide audience.

With the agents and tasks defined, we create a Crew object that brings everything together. The Crew object manages the workflow, ensuring the tasks are executed in the correct order (research first, then writing) and providing the necessary coordination between the agents.

A Powerful Crew — Credit to OPFandom

Finally, we call the crew.kickoff() method, which triggers the AI agents to get to work. The result of this process is the completed blog post, which we return from the write_blog function.

This approach allows us to leverage the strengths of different AI models (the ollama_gemma and ollama_mistral models) to handle the research and writing tasks, respectively. By dividing the work and allowing the agents to focus on their specific strengths, we can streamline the content creation process and produce high-quality blog posts efficiently.

Absolutely, the concept of “CrewAI” makes sense, drawing parallels between a diverse crew with various expertise and an AI model library offering specialized capabilities. With CrewAI, users can leverage the strengths of different AI models tailored to specific tasks, similar to how a crew with diverse skills can tackle different aspects of a project. It’s a fitting analogy that highlights the versatility and adaptability of AI models to address a wide range of challenges effectively.

Finally, let’s add a main function to accept the topic from the command line and execute the program.

def main():
if len(sys.argv) < 2:
print("Please provide a topic as an argument.")
print('Usage: python blog.py "AI and data science trends in 2024"')
sys.exit(1)

# Get the first command-line argument after the script name
topic = sys.argv[1]

# Now you can use 'argument' in your script
print("\n\n#### Topic ####")
print(topic)

if topic == '':
print("Topic is empty.")
sys.exit(1)

result = write_blog(topic)

print("\n\n#### Result ####")
print(result)

if __name__ == '__main__':
main()

Say, you want to write a blog about “AI and data science trends in 2024”, you open the terminal and write following

python blog.py "AI and data science trends in 2024"

You might get the output as following


#### Topic ####
AI and data science trends in 2024

[DEBUG]: == Working Agent: Senior Research Analyst
[INFO]: == Starting Task: Conduct a comprehensive analysis of AI and data science trends in 2024.


> Entering new CrewAgentExecutor chain...
**Thought:**

I am well-positioned to provide a comprehensive analysis of AI and data science trends in 2024. My expertise in identifying emerging trends, research, and dissecting complex data enables me to uncover valuable insights into this rapidly evolving field.

**Final Answer:**

**AI and Data Science Trends in 2024:**

* **AI-powered Data Analytics:** AI is revolutionizing data analytics by automating tasks, improving accuracy, and providing actionable insights. Key advancements include natural language processing (NLP) for text and image analysis, as well as deep learning for predictive modeling.

* **Emerging AI Applications:** AI is permeating various industries, including healthcare, finance, retail, and transportation. Notable applications include self-driving cars, facial recognition, and fraud detection.

.
.
.

[DEBUG]: == Working Agent: Content Strategist
[INFO]: == Starting Task: Using the insights provided, develop an engaging blog
post that highlights the topic of AI and data science trends in 2024.
Your post should be informative yet accessible, catering to any audience, easy to understand.
Make it sound cool, avoid complex words so it doesn't sound like AI.


> Entering new CrewAgentExecutor chain...
Thought: With the insights provided, I'm ready to craft an engaging blog post on AI and data science trends in 2024.

Final Answer:

**Welcome to the Future:** **AI and Data Science Trends Shaping Our World in 2024**

Imagine a world where machines learn, adapt, and make decisions just like us. That's not science fiction anymore; it's our present-day reality. Artificial Intelligence (AI) and Data Science are leading the charge, bringing us exciting advancements that reshape industries and our daily lives. Let's explore some trends that will dominate these fields in 2024.

.
.
.


#### Result ####
**Welcome to the Future:** **AI and Data Science Trends Shaping Our World in 2024**

Imagine a world where machines learn, adapt, and make decisions just like us. That's not science fiction anymore; it's our present-day reality. Artificial Intelligence (AI) and Data Science are leading the charge, bringing us exciting advancements that reshape industries and our daily lives. Let's explore some trends that will dominate these fields in 2024.

**Transforming Data with AI:** **Revolutionizing Analytics**

In the data-driven world we live in today, understanding trends and making informed decisions is crucial. And here comes AI to the rescue! Instead of spending hours manually processing and analyzing data, AI systems are automating tasks, improving accuracy, and providing actionable insights. Key advancements include Natural Language Processing (NLP) for text and image analysis and deep learning for predictive modeling. These technologies enable us to gain valuable insights from vast datasets in various industries like finance, healthcare, and retail.

**AI Everywhere:** **Emerging Applications in Multiple Sectors**

From self-driving cars revolutionizing transportation to facial recognition systems enhancing security, AI is permeating various sectors. In healthcare, it's being used for diagnosis and personalized treatment plans. In finance, AI is powering fraud detection and risk assessments. Retail is experiencing the benefits with personalized recommendations based on customer preferences. The applications of AI are endless!

**Breaking Down Data Silos:** **Data Democratization**

Data is no longer locked away in silos; it's becoming more accessible to everyone thanks to advancements in data visualization tools and platforms. This empowers individuals and organizations to make informed decisions based on data, fostering a data-driven culture where insights lead the way!

**Protecting Our Data:** **Data Privacy and Security**

As we embrace these technological advancements, data privacy and security are becoming growing concerns in the wake of increasing data breaches and cyberattacks. Regulations like GDPR and CCPA are driving the adoption of data protection measures to secure our sensitive information.

.
.
.

In conclusion, these trends in AI and data science are shaping our future in remarkable ways. From revolutionizing industries to making informed decisions, AI and Data Science are leading us towards a more connected, intelligent, and data-driven world. Stay tuned for more updates on these exciting trends!

We can see the output of each agent and how each agent is working together to perform the task for us.

Bonus

One limitation of AI models is that they only know what they have been trained on. So, if you ask an AI model to perform a task it hasn’t been trained for or inquire about recent news, it’s impossible for the AI to know the answer, and it might provide incorrect information or generate a hallucination.

In this section, we will address this problem by providing additional contextual information, which could come from various sources such as PDFs, websites, videos, audios, etc. This will enable the AI to answer our questions more effectively. To put it simply, we are going to develop another program where we can query the AI both with and without supplementary assistance.

Let’s create a file called ask.py and add the following content:

from langchain_community.llms import Ollama
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA
import sys

ollama = Ollama(model="mistral:7b")

def answer_with_context(question, context_url):
# Now let's load a document to ask questions against.
loader = WebBaseLoader(context_url)
data = loader.load()

# This file is pretty big. Which means the full document won't fit into the context for the model. So we need to split it up into smaller pieces.
text_splitter=RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
all_splits = text_splitter.split_documents(data)

# It's split up, but we have to find the relevant splits and then submit those to the model.
# We can do this by creating embeddings and storing them in a vector database.
# We can use Ollama directly to instantiate an embedding model.
oembed = OllamaEmbeddings(model="nomic-embed-text")
vectorstore = Chroma.from_documents(documents=all_splits, embedding=oembed)

# And here is the relevant part of the document to the question.
docs = vectorstore.similarity_search(question)
print("\n\n#### Context From Document ####")
print("url: ", context_url)
print(docs)

# The next thing is to send the question and the relevant parts of the docs to the model to see if we can get a good answer.
qachain = RetrievalQA.from_chain_type(ollama, retriever=vectorstore.as_retriever())
output = qachain.invoke({"query": question})
return output['result']

def answer(question):
output = ollama.invoke(question)
return output

def main():
if len(sys.argv) < 2:
print("Please provide a topic as an argument.")
print('Usage 1: python ask.py "Who is current President in USA?"')
print('Usage 2: python ask.py "Who is current President in USA?" "https://en.wikipedia.org/wiki/President_of_the_United_States"')

# Get the first command-line argument after the script name
question = sys.argv[1]

print("\n\n#### Question ####")
print(question)

# If the user provides a second argument, we will use that as the context URL to find the answer.
context_url = ''
if len(sys.argv) == 3:
context_url = sys.argv[2]

if context_url == '':
result = answer(question)
else:
result = answer_with_context(question, context_url)

print("\n\n#### Result ####")
print(result)

if __name__ == '__main__':
main()

We have created two functions: answer and answer_with_context.

In the answer function, the AI model attempts to provide an answer without any additional assistance.

In contrast, the answer_with_context function operates as follows:

  1. It retrieves data (text) from the provided URL.
  2. The retrieved data is converted into vectors using the nomic-embed-text model and stored in the Chroma vector database.
  3. Only the portions of the documents relevant to the user’s question are extracted.
  4. Finally, this information from the vector database is passed to the AI model to generate an answer to the question.

To run the application, use the following command:

> python ask.py "Who is current President in USA?"


#### Question ####
Who is current President in USA?


#### Result ####
As of my knowledge up to 2021, the current President of the United States is Joe Biden. He assumed office on January 20, 2021.

Now with context

python3.10 ask.py "Who is current President in USA?" "https://en.wikipedia.org/wiki/President_of_the_United_States"


#### Question ####
Who is current President in USA?


#### Context From Document ####
url: https://en.wikipedia.org/wiki/President_of_the_United_States
[Document(page_content='^ "The Presidents of the United States of America".
Enchanted Learning. Retrieved August 2, 2018.\n\n^
"Political Parties of the Presidents". Presidents USA.
Retrieved August 2, 2018.\n\n\nFurther reading', .....
...

#### Result ####
The current President of the United States is Joe Biden.

With little bit of help, our AI can answer the question correctly. Here is the link to Github Repo — https://github.com/yong-asial/local-llm.

Conclusion

In this article, we have learned how to use CrewAI to create multiple AI agents to perform the task for us. And in the bonus part, we demonstrate how we can provide some context to AI to perform a better job. I hope you can find them useful and maybe your homework is to combine these techniques together and create even a better and interesting application.

Happy Coding.

--

--