Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take the GenAI Test: 25 Questions, 6 Topics. Free from Activeloop & Towards AI

Publication

Building AI Agents With Crew AI using Google Gemini, Groq, LLama3
Latest   Machine Learning

Building AI Agents With Crew AI using Google Gemini, Groq, LLama3

Last Updated on June 10, 2024 by Editorial Team

Author(s): Suhaib Arshad

Originally published on Towards AI.

Inspired from: Enable AGI | How to Create Autonomous AI Agents with GPT-4 & Auto-GPT β€” YouTube

Introduction

In the recent uproar of Devin AI’s emergence, there was a genuine concern in the market saying, β€œDevin is going to take our software engineering jobs”. And that concern is valid, where Devin is said to be the world’s first AI software engineer that can execute an entire software project from designing to coding to debugging/testing. This AI-powered virtual software engineer was the biggest nightmare for any day-to-day software engineer. What is Devin intrinsically? How did it reach this point?

Devin at its core is an AI agent that has advanced capabilities of integrating with software systems, enhancing workflows and tasks using automation. AI agents can also be integrated with voice search to help automate your day-to-day task.

What are AI Agents?

AI agents are nothing but LLMs well-equipped with access to the right tools. These AI agents are capable of:

  • Reasoning capacity inherited from LLMs to help them pick the right tools
  • Automated browsing
  • web scraping
  • SQL query execution
  • file operations, and more.

Note: In cases of LLMs we use only a single agent to perform a task, However, AI agents use an assembly of multi-agents to complete complex and procedural tasks.

Currently the development of AI agents is in a budding phase. There are a handful of companies that are at the forefront of AI Agent development, one of which is CrewAI.

What is CrewAI?

CrewAI is an open-source framework that helps you build your own autonomous AI agents. CrewAI’s framework has been designed in a way to help users orchestrate roles, delegate tasks, and share expected goals, in a collaborative environment.

Here are some key characteristics of CrewAI agents:

  • We can build multi-agents by assigning each of them certain roles, backstories and expected goals/output we want to accomplish with the task.
  • We can integrate and specify tools for tasks and automate the process for various other tasks.
  • CrewAI’s agents are capable of delegating tasks within themselves according to the role specialization of each and work in a collaborative environment to achieve the output.

Component-wise breakdown:

  • Agents: They are team members with assigned roles, backstory, and goals.
  • Tools: Resouces or toolkits provided by LangChain or custom that can be used to integrate with LLMs.
  • Tasks: A to-do list given to an Agent for achieving the output.
  • Process: A workflow of operations and procedures the needs to adhere to crew follows for accomplishing the tasks.
  • Crew: When Agents, Tasks and Process comes together where the entire work gets executed.

Real world example of where and how we can use CrewAI?

Example #1

Let’s start with an objective in mind, for now, let’s say you have a zest for writing blogs about AI in the healthcare domain, you are keen to write about that topic but don’t know anything about how to do that, then what you would do is hire a team/crew. Breaking things down into tasks to be executed:

  1. Start with intensive research about the domain and the use of AI in that domain, in our case it is healthcare, find your niche and understand how it is getting used there.
  2. Only researching is not enough, you need to write the curated content in a precise and compelling manner so that people read while also taking care of the page ranking via SEO.

So, if we had to hire a team based on the above tasks, then, we need to hire:

  • Senior idea researcher: to perform extensive research on new content ideas and come up with content development and distribution strategies.
  • Senior content writer: to make sure our content is original, engaging, precise and SEO friendly.

Now that we have our tasks, roles, agents and crew ready, let’s deep dive into coding to see how this can be implemented using Crew AI.

As our implementation will be in Python, we have to do the standard procedures like activating virtual environment and install the dependencies. So, installing the CrewAi library, Crewtools for tool integration and Langchain’s implementation of Google GenAI.

pip install crewai langchain-google-genai crewai_tools

Ensure that you have your Gemini Api key by navigating to https://aistudio.google.com/ and logging in.

Click on Get API key->Create API key. Select a model of your choice, I have taken the recent Gemini 1.5 flash

The next step is to define your LLM model, it could be any LLM model, I have gone for Gemini over OpenAI because its free to use. Let’s write our agents.py.

import os
from dotenv import load_dotenv
load_dotenv()

from crewai import Agent
from langchain.tools import tool
from langchain_google_genai import ChatGoogleGenerativeAI

llm=ChatGoogleGenerativeAI(model="gemini-1.5-flash",verbose=True,
temperature=0.5,google_api_key=os.getenv("GOOGLE_API_KEY"))

Going ahead and defining our agent’s role, goal, backstory, tools(optional) and LLM model

researcher= Agent(
role="Senior Researcher",
goal="""Uncover groundbreaking topics
in {topic}"""
,
backstory=("""Driven by curiosity, you are at the forefront of Innovation sharing
knowledge and news that would change the world"""
)
,tools=[],llm=llm,allow_delegation=True,verbose=True,memory=True,)


writer= Agent(
role="Writer",
goal="Narrate compelling tech stories about {topic}",
backstory=("""Your expertise lies in breaking down complex topics into simpler and
digestable knowledge, you specialize in captivativing audience attention with your
writing and create engaging narratives and stories"""
)
,tools=[],llm=llm,allow_delegation=False,verbose=True,memory=True)

We have two agents, one who is a researcher and one who is a writer. All the parameters like role, goal, backstory will be used as a prompt to provide the LLMs with more context.

We need tools to research about our content so we will use Google’s free Search Api: serper ; Navigate to serper and get your Api key.

Now, inside tools.py we will define what tools to use for the task

from dotenv import load_dotenv
load_dotenv()

import os
from crewai_tools import SerperDevTool

os.environ["SERPER_API_KEY"]= os.getenv("SERPER_API_KEY")

tool= SerperDevTool()

Now that we have our tools ready, let’s move on to defining the task each agent is going to perform, into tasks.py.

from crewai import Task
from tools import tool
from agents import researcher,writer

research_task= Task(description=(
"Identify the next big trend in {topic}."
"Focus on identifying pros and cons and the overall narrative."
"Your final report should clearly articulate the key points,"
"its market opportunities, and potential risks."
),
expected_output='A comprehensive 3 paragraphs long report on the latest AI trends.',
tools=[tool],
agent=researcher)

write_task = Task(description=(
"Compose an insightful article on {topic}."
"Focus on the latest trends and how it's impacting the industry."
"This article should be easy to understand, engaging, and positive."
),
expected_output='A 4 paragraph article on {topic} advancements formatted as markdown.',
tools=[tool],
agent=writer,
async_execution=False,
output_file='new-blog-post.md' # output file containing final draft
)

Finally put together our functional crew: Agents with assigned Tasks and equipped Tools, lets feed the topic and kick things off in crew.py

from crewai import Crew,Process
from task import research_task,write_task
from agents import researcher,writer

crew= Crew(agents=[researcher,writer],
tasks=[research_task,write_task],
process= Process.sequential
)

result= crew.kickoff(inputs={"topic":"AI in healthcare"})

print(result)
python crew.py #run this command in terminal

I have attached the output and backend screen recording of all the processes crew ai goes through before writing its final draft.

Example #2:

Again, start with an objective in mind, let's say you want to develop a business and marketing plan for your idea, then what you would do is hire a team/crew. Breaking things down into tasks to be executed:

  1. Start with intensive research about the domain and the use of AI in that domain, in our case it is healthcare, find your niche and understand how it is getting used there.
  2. Only researching is not enough, it takes a seasoned entrepreneur to evaluate and distinguish good ideas from the great ones.

So, if we had to hire a team based on the above tasks then we need to hire:

  • Senior market researcher: to perform extensive market research on trends, industry, and competitors and validate ideas to curate an evaluation report.
  • Experienced Entrepreneur: To critically evaluate the idea and design a business and marketing plan based on his/her experience.

Let's start coding it, activate your virtual environment and install the dependencies. So, installing the CrewAi library, Crewtools for tool integration and Langchain’s implementation of Groq.

pip install crewai crewai_tools langchain_groq

Ensure that you have your Groq Api key by navigating to https://console.groq.com/keys and logging in.

Followed by defining your LLM model, I selected for llama3, there are other options as well (mistral or llama3–70b). Here is our agents.py.

from crewai import Agent
from dotenv import load_dotenv
from langchain_groq import ChatGroq
load_dotenv()

from tools import tool
import os

llama = ChatGroq(
model="llama3-8b-8192",
api_key=os.getenv("GROQ_API_KEY")
)

Going ahead and defining our agent’s role, goal, backstory, tools(optional) and LLM model

# Senior market researcher agent
market_researcher = Agent(
role="Senior market researcher",
goal="Ensure the business {idea} is backed by solid research and data \n"
"Carry out a comprehensive and realistic research for the given {idea}."
"Provide insights of your research to the enterpeneur.",
backstory="You are an expert market researcher skilled at validating business ideas"
"You are skilled at doing market research for a given {idea}"
"YOu have worked with numerous startups and established companies, helping them identify market trends and develop successful business strategies."
"Your expertise lies in giving research reports for a given {idea}.",
allow_delegation=False,
verbose=True,
llm=llama
)

# enterpreneur agent
enterpreneur_agent = Agent(
role="Experienced Entrepreneur",
goal="Create a marketing plan and business plan for {idea}",
backstory="You have built more than 10 successful companies."
"You are skilled at creating business ideas and marketing plans"
"You possess high experience in crafting business and marketing plans"
"You need to make sure that proper business plan and marketing plan is generated for {idea}",
verbose=True,
allow_delegation=False,
llm=llama
)

We need tools to research about our content so we will use Google’s free Search Api: serper ; Navigate to serper and get your Api key.

Now, inside tools.py we will define what tools to use for the task

from dotenv import load_dotenv
load_dotenv()

import os
from crewai_tools import SerperDevTool

os.environ["SERPER_API_KEY"]= os.getenv("SERPER_API_KEY")

tool= SerperDevTool()

Now that we have our tools ready, let’s move on to defining the task each agent is going to perform, into tasks.py.

from crewai import Task
from tools import tool
from agents import market_researcher,enterpreneur_agent


# task for market researcher
task_market_researcher = Task(
description=(
"Analyze the strengths, weaknesses, opportunities, and threats (SWOT analysis) of the business idea {idea}"
"Estimate market size and growth potential."
"Assess the feasibility of the business model."
"Give your insights for creating a Business Plan."
),
expected_output=(
"A detailed market research report for the mentioned idea {idea}"
"Include references to external data for market analysis"
),
tools=[tool],
agent=market_researcher,
)

# enterprenuer
task_enterpreneur = Task(
description=("Create the marketing plan and business plan for {idea}"
"Ensure that there are no discrepancies for the generated plans"
"There should be no inconsistences in either of the plans"
"Verify that all important concepts of business and marketing plans are covered"
),
expected_output=("Output should contain two main things: a final business plan for the {idea}\n"
"A final marketing plan for the {idea}"
),
tools=[tool],
agent=enterpreneur_agent,
output_file='idea-analysis.md'
)

Finally put together our functional crew: Agents with assigned Tasks and equipped Tools, lets feed the topic and kick things off in crew.py

from crewai import Crew,Process
from task import task_enterpreneur,task_market_researcher
from agents import market_researcher,enterpreneur_agent

crew = Crew(
agents=[market_researcher, enterpreneur_agent],
tasks=[task_market_researcher, task_enterpreneur],
verbose=2,
max_rpm=29
)

result = crew.kickoff(
inputs={"idea": "oversized tshirts in mumbai india"})
print(result)
python crew.py #run this command in terminal

I have attached the output and backend screen recording of all the processes crew ai goes through before writing its final draft.

Other Examples

Checkout their github repo for more usecases and projects

Key Takeaways

  • CrewAI is an open-source AI Agent framework made to expand the capabilities of an LLM into doing things that it wouldn’t be capable of doing otherwise.
  • The key characteristics of CrewAI are role assignment for Agents, integration of tools for task completion, and delegation of tasks between agents.
  • CrewAI sits on top of Langchain ecosystem, in that way it can leverage the tools and integrations by LLMs.

Frequently Asked Questions

Q1. What is CrewAI?

A. CrewAI is a framework built to help developers make and manage collaborative AI agents that work as a crew to complete tasks and achieve the final goal via collaborative learning and delegation.

Q2. What are some common applications of AI Agents?

A. Automation of Customer service with help of multi-agents that are capable of handling customer inquiries and grievances at scale.

Supply chain management, where agents can assist in inventory planning and backend operations handling.

Public information where people can seek assistance from AI agents, for example, asking AI agent to file for an ITR.

Q3. Where can I find documentation and tutorials for CrewAI?

A. You can find the full documentation from the GitHub repo here.

Q4. Can CrewAI agents interact with external APIs?

A. Yes, CrewAI agents have the functionality to interact with external APIs. Enables them to extract data,

Q5. What programming languages are supported by CrewAI?

A. CrewAI can be implemented using Python, JavaScript, and Java. It is built on top of langchain.js framework, enabling the performance of different kinds of browser operations.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓