👋 Introduction
Imagine you are an entrepreneur who have a product idea but it require extra 1-2 persons to help you achieve that task or you are a manager who got an feature idea but you have to hire more 2-3 employees for that task. But what if i say you can hire a group of AI which can work for you as an employee. sounds good right? 👀
💡You can get all of the code shown in this blog from here.
You can achieve this using AI agents. These agents can automate any task for you and they can be any personality in your company like software developer, content writer or travel agency expert. You have full command on these agents and these agent will only listen to you and do the tasks which you will provide to them. So basically you are their boss 😎.
You can create such agents using crewAI which is a framework that allows you to create such autonomous AI agents. crewAI works on the top of Langchain framework which is one major framework to connect any LLM with external sources and create applications on top of these LLMs.
To learn more about Langchain and Langchain agents, take a look at my blog where i automated a meeting workflow using Langchain Agent.
In this blog, we will explore crewAI and its functionalities and also we will create one crew which will automate a code review workflow for us.
This crew will do following tasks
- It will take url of github repository of a project as an input and a folder or file name which you want to review
- It will get the content of specified files using github API
- Review the given file content and make changes in a code and provide feedbacks
- Store the review in a notion document so that it can be reviewed later by a human (You)
Here is the sneak peek of our crew 👀
So let’s start! 🚀
👥 What is crewAI?
CrewAI is an AI framework which is designed to orchestrate role-playing autonomous AI agents. This framework empowers agents to share information among each other, assign tasks to each other and delegate tasks to each other to get the final result.
One of the key feature of crewAI is that every agent have a specific task assigned to it so that it can focus on the specific task and try to reach the end goal which is assigned to it by using built in or custom tools provided to it.
What is an agent?
Agents are the employees or little helpers within the CrewAI ecosystem, bringing the magic of automation to life. These agents are essentially intelligent entities, programmed to understand and respond to human language, allowing them to perform a myriad of tasks autonomously.
In CrewAI, every agent have following parameters:
- Role: The role or personality of an agent, for example software engineer or content writer
- Goal: Goal specifies the final goal which agent have to achieve, for example perform maths calculations
- Backstory: Provides context to the agent's role and goal, enriching the interaction and collaboration dynamics.
- Tools: The tools which agent can use to achieve the given goal. These tools can be default tools provided by CrewAI or Langchain, or it can be custom tools created by you.
A sample agent will look like this in code:
What is task?
In the CrewAI framework, tasks are individual assignments that agents complete. For example, one agent might gather data while another analyzes it. This collaborative approach can be defined within the task properties and managed by the Crew's process.
Every task have following parameters:
- description: A detailed description about task which includes information about end goal, what needs to be achieved and how to achieve it
- agent: An agent which is going to complete this task
- expected_output: Clear and detailed definition of expected output for the task.
A sample task will look like this:
What is crew?
A crew in crewAI represents a collaborative group of agents working together to achieve a set of tasks. Each crew defines the strategy for task execution, agent collaboration, and the overall workflow.
You can think of crew as your own organization where agents are your employees and tasks are the roles provided to them.
You can create 2 types of crew flows:
- sequential: Where output of first agent is used as input for next agent which creates a chain of agents which is working sequentially.
- hierarchical: Where these agents can work independently without being dependent on any other agent. There will be one manager LLM which manages all these agents and shares information among agents to reach the end goal.
Let’s create our first crew
Let’s take a basic example, you want to get the latest value of any currency in INR and then you want to perform some mathematical operations on that value. Let’s create a crew for that!
We will need 2 agents for this task:
- Search Expert: A google search agent which will give us the latest price of given currency in INR
- Maths Expert: An agent which can perform mathematical operations on given numbers
We will use serper API to get data from google search and we will create custom tools for both search and calculation.
🛠️ Prerequisites:
- OpenAI API key: we will use gpt-4 as our LLM for this task. Get your api key from openai dashboard and add it as a environment variable in your code as it’s recommended to keep it private.
- Serper API key: It will be used to search internet. Get your API key from serper website (it provides free tokens for new users without credit card!).
Let’s first install all required modules and dependencies
Let’s import the methods and modules which we are going to use
Don’t forget to store your openai API key in environment variables with name OPENAI_API_KEY otherwise it won’t work!
Now let’s create our custom tools, one for mathematical calculation and one for internet search
Here we will use @tool
decorator from Langchain to specify a tool name and description so that our agents can use it. These tools are just simple functions where we will write our business logic
Now let’s create our agents aka AI employees
And now let’s create the tasks which we will give to these agents
Let’s create our crew class, we will call it MathsCrew
for now but you can give any name you want because it’s your company 😎
Remember that the sequence of tasks matter here because it is a sequential process.
Lastly, add a code to take input from user and run the crew
Let’s run the above code with these 2 inputs:
- stock - USD
- operation - add 10 into it
After running it, you will get this output from crew:
As you can see, we can see the whole thinking process of both agents and how data is getting passed between these 2 agents in crew and finally we are getting our expected output!
So now we know how to create a basic crew so let’s make one mini project to automate code review workflow of any organization or repository 🚀
💻 Mini Project: Automating code review workflow
Imagine you are a CEO of a company which works with information technology industry and you have a team of software developers but as a good CEO you want to maintain the quality of code which these developers have written to match the industry standards and to get a good reputation among your clients. That’s why you need someone senior who can review codes written by these developers.
Now the main problem is that you need to hire a senior software developer who can review code of these developers but you don’t want to pay him just for a code review and doing this manually might take some time. So instead of doing this manually you can create your own crew of agents which will do this task for you. 👀
Workflow
We will create a crew which can do following tasks
- Get the tree structure of github repo
- Get the content of the files which you want to review
- Review the given file
- Add the review and updated code in notion document
The workflow of our crew will look like this
We will first take a url of github repository which we have to review and then we will take a file or folder name as an input from a user. if user provides a folder name then we will review every file inside that folder and if user provides a filename then we will only review that file.
Prerequisites
- OpenAI API key: we will use gpt-4 as our LLM for this task. Get your api key from openai dashboard and add it as a environment variable in your code as it’s recommended to keep it private.
- Github personal access token: we will use this token to make requests to github API as an authenticated user because you can’t access your private repositories without this token and we are assuming that as an organization, you like to keep your repository private. Get your personal access token (PAT) from github settings.
- Notion API key: we will use this notion key to create notion pages and to add data into a notion document using notion API. Create your integration from here and add it in your document by clicking on 3 dots icon → connect to → your integration name.
So now we have all the things ready so let’s code it! 🚀
Let’s code it
Start by installing the required modules and dependencies
Import all the modules and dependencies
Make sure to keep all of your API keys private and store it as an environment variable
We will create one notion page and we will keep pushing the review in that notion file instead of creating separate notion pages for every file. we will use notion_client module to interact with notion API.
Let’s create a helper function to create a notion page which will take project name as a parameter which will be used as a file name. Also get the page_id of a notion page in which you want to add all these documents.
Now let’s create a helper method to get the tree structure of given github repository
Define all of your tasks for your agents
Let’s create custom tools which our agents will use.
We will create 2 custom tools here:
- addToNotion: It will add the given content into notion document with given page_id
- getFileContents: It will return the content of given file using github API
We will @tool
decorator from Langchain to define the custom tool
We will require 4 agents in our crew:
- ReviewAgent: It will review given file based on filename and file content
- NotionAgent: It will add the given data into given notion document
- ContentAgent: It will return the content of given file using github API
- PathAgent: It will return the array of full paths of files from the given tree structure so that we can build the API url for that file
Let’s create our agents!
Now let’s create our crew class, we will call it ReviewCrew
in which we will have a run method which will start the crew execution
ReviewCrew will accept 4 parameters:
- owner: name of github repository owner
- repo: name of github repository
- page_id: page id of the notion page in which we want to add the review
- path: full path of file which needs to be reviewed
Now we are only one step close to our final crew!
Let’s add the code to do following tasks:
- Take input from user
- Get tree structure of given github repository
- Get array of file paths which needs to be reviewed
- Traverse these paths one by one and review them one by one using our crew
Now we are ready to launch our crew 🚀
After running the above code, you will be able to see the thought process of agents and how they are sharing information with each other in your console. Once the process is completed you will see the newly created notion document in your workspace which looks like this:
After opening the document you can see the detailed review about the files which you specified 🤩
And now we have successfully created a crew of autonomous agents which can review your code without a need of human interaction 🚀
💡You can get all of the code shown in this blog from here.
👀 Challenges
Let’s discuss the challenges which I faced while making this mini project 👀
Problem in reviewing the github repository
The first question i came in my mind was that “How do i review the whole github repository and if i review every file one by one then how the agent will know which file is related to which directory 🤔? “
After thinking about it, i found one way that we can create the tree structure of given github repository and send it as a context to our agent so that it can know the relationship of files and also it can review the code structure if needed.
Which file to review?
Now i had the tree structure of code but still i was unaware about the files which i need to review so after thinking for a while, i found that there are basically 2 scenarios while doing code review:
- You are maintainer of a repository or a person who already have knowledge about the repository which you want to review and you only want to review a commit of specific files from whole repository
- You have no idea about the repository and you just want to review the basic structure of repo if it follows the industry standards or not
So for this project i chose the 1st scenario because most of the time we do have idea about the project or the code which we are reviewing so i added an input field where user will provide the folder or file name which they want to review and then from that input, we will provide both tree structure and userInput to our agents and then they will only fetch those file contents using github API.
So here we solved the openAI cost and github API rate limiting issue by only reviewing files which are needed 🤧.
If you want to go with 2nd scenario then you can take the tree structure of a repository and first check if it follows the industry standards for a folder structure. For example, if its a react app then check if routing is there or redux setup is there or does it follows your company folder structure.
Choosing the files/folders to review is a crucial task and also depends on your way of code review. you can also create agents to only review Routing or a redux setup in an application.
Response token limit of openAI
When i was making this project, I was using all of the 4 agents in a single crew and i was running that crew only once. In current version of crew it runs for each path in paths array but the main problem was that it was reviewing every file at once and was returning it to another agents and because of that the output was not perfect and i was not getting updated code because of the output token limit of openAI.
Also for a files which have more than 200-300 lines of code was not getting reviewed properly because of the context length because in previous version every file content was getting added in a single context to openai and because of that i was not able to review more than 2-3 files.
To solve this issue, I used the pathAgent separately and then i iterated through the file paths one by one and for each path I ran the ReviewCrew so that it can review one file at a time and can review more bigger files. It increased the openAI API calls but it made the code review process more smooth, scalable and accurate.
📝 Conclusion
As we've explored in this blog, the potential of autonomous agents and CrewAI is vast. By establishing the crew of agents withing your organization you can increase efficiency and accuracy of a code review process. Integrating CrewAI into your company's operations can revolutionize how code reviews are conducted, leading to higher-quality code, faster delivery times, and a more agile development environment.
If you want to take a look at more crew examples like this then check it here.
So, whether you're a small team looking to optimize your code review workflows or a large organization seeking to revolutionize your workflow, autonomous agents are a very good consideration to automate your tasks with the use of Artificial Intelligence.
If you are looking to build custom AI agents to automate your workflows then kindly book a call with us and we will be happy to convert your ideas into reality and make your life easy.
Thanks for reading 😄.