As the need for efficient software development continues, artificial intelligence becomes a valuable colleague for programmers. AI-powered coding assistants are changing the game by making it easier for developers to write, debug, and optimize their code like pair programmers. In this article, we’ll learn how to build an AI pair programmer using CrewAI agents to simplify your coding tasks and boost your productivity in software development.
Let’s explore a few use cases of what an AI pair programmer can do.
In this article, we will cover the first two tasks.
CrewAI is a popular framework for building AI agents. It consists of key components such as Agents, Tasks, Tools, and Crews.
Also Read: Building Collaborative AI Agents With CrewAI
Now, let’s build an agent to gain a better understanding!
Before building an AI Pair Programmer with a CrewAI agent, ensure you have the API keys for LLMs.
Start by generating an API key for the LLM you plan to use. Then, create a .env file to store this key securely. This will keep it private while making it easily accessible within your project.
Here’s an example of what a .env file looks like:
We have used the following versions for the major libraries:
In this section, we will import the necessary libraries and define agents to generate and review code. This hands-on approach will help you understand how to utilize CrewAI effectively.
from dotenv import load_dotenv
load_dotenv('/.env')
from crewai import Agent, Task, Crew
We can use one agent to generate the code and another agent to review the code.
code_writer_agent = Agent(role="Software Engineer",
goal='Write optimized code for a given task',
backstory="""You are a software engineer who writes code for a given task.
The code should be optimized, and maintainable and include doc string, comments, etc.""",
llm='gpt-4o-mini',
verbose=True)
code_writer_task = Task(description='Write the code to solve the given problem in the {language} programming language.'
'Problem: {problem}',
expected_output='Well formatted code to solve the problem. Include type hinting',
agent=code_writer_agent)
Similarly, let’s define code_reviewer_agent and code_reviewer_task.
code_reviewer_agent = Agent(role="Senior Software Engineer",
goal='Make sure the code written is optimized and maintainable',
backstory="""You are a Senior software engineer who reviews the code written for a given task.
You should check the code for readability, maintainability, and performance.""",
llm='gpt-4o-mini',
verbose=True)
code_reviewer_task = Task(description="""A software engineer has written this code for the given problem
in the {language} programming language.' Review the code critically and
make any changes to the code if necessary.
'Problem: {problem}""",
expected_output='Well formatted code after the review',
agent=code_reviewer_agent)
Now, we can build the crew and run it:
crew = Crew(agents=[code_writer_agent, code_reviewer_agent],
tasks=[code_writer_task, code_reviewer_task],
verbose=True)
result = crew.kickoff(inputs={'problem': 'create a game of tic-tac-toe', 'language': 'Python'})
The sample output will be as follows:
The result will have the following
result.dict().keys()
>>> dict_keys(['raw', 'pydantic', 'json_dict', 'tasks_output', 'token_usage'])
# we can also check token usage
result.dict()['token_usage']
>>> {'total_tokens': 2656,
'prompt_tokens': 1425,
'completion_tokens': 1231,
'successful_requests': 3}
# we can print the final result
print(result.raw)
We can run the code generated by the Agent:
After building the code generation and review agents, we will now evaluate an existing code file.
First, we will gather evaluation requirements for a problem using an agent and then evaluate the code based on those requirements using another agent.
We will use the FileReadTool to read files from the system. Tools enhance agent capabilities by enabling actions like reading files or searching the Internet.
We can assign tools to both tasks and agents. Tools assigned in Tasks will override tools in the agent.
code_requirements_agent = Agent(role="Data Scientist",
goal='provide are all things that should be required in the code to solve the given problem.',
backstory="""You are a Data Scientist who decides what are all things required
in the code to solve a given problem/task. The code will be written based on
the requirements provided by you.""",
llm='gpt-4o-mini',
verbose=True)
code_requirement_task = Task(description='Write the requirements for the given problem step-by-step.'
'Problem: {problem}',
expected_output='Well formatted text which specifies what is required to solve the problem.',
agent=code_requirements_agent,
human_input=True)
In the above task, we have assigned human_input to True. So, the agent asks for input from the user once it generates the requirements. We can ask it to make any changes if required.
Now, do the same evaluation. Here, we use the tool to read the file. We also use GPT-4o for better output as the context size is larger.
from crewai_tools import DirectoryReadTool, FileReadTool
file_read_tool = FileReadTool('EDA.py')
code_evaluator_agent = Agent(role="Data Science Evaluator",
goal='Evaluate the given code file based on the requirements provided for a given problem',
backstory="""You are a Data Science evaluator who reviews and evaluates the code.
You should check the code based on the requirements given to you""",
llm='gpt-4o',
verbose=True)
code_evaluator_task = Task(description="""A code file is given to you.
Evaluate the file based on the requirements given as the context.
Provide the only review and evaluation of the code as the output, not the code.
""",
expected_output='Detailed evaluation results of the code file based on the requirements.'
'Review the code file for each point of the requirements given to you'
'Provide evaluation results as text',
tools=[file_read_tool],
agent=code_evaluator_agent)
Let us build the crew and define the problem to get the requirements for it.
crew = Crew(agents=[code_requirements_agent, code_evaluator_agent],
tasks=[code_requirement_task, code_evaluator_task],
verbose=True)
problem = """
Perform EDA on the NYC taxi trip duration dataset.
Here is the description of all the variables/features available in the dataset which will help you to perform EDA:
id - a unique identifier for each trip
vendor_id - a code indicating the provider associated with the trip record
pickup_datetime - date and time when the meter was engaged
dropoff_datetime - date and time when the meter was disengaged
passenger_count - the number of passengers in the vehicle (driver entered value)
pickup_longitude - the longitude where the meter was engaged
pickup_latitude - the latitude where the meter was engaged
dropoff_longitude - the longitude where the meter was disengaged
dropoff_latitude - the latitude where the meter was disengaged
store_and_fwd_flag - This flag indicates whether the trip record was held in vehicle memory before sending to the vendor because the vehicle did not have a connection to the server (Y=store and forward; N=not a store and forward trip)
trip_duration - (target) duration of the trip in seconds
"""
result = crew.kickoff(inputs={'problem': problem})
Here’s how the result will look like while asking for human input:
We can also get the output of any task as follows:
print(code_requirement_task.output.raw)
# final output
print(result.raw)
In this way, we can build versatile crews to make our own AI pair programmer.
CrewAI offers a powerful framework for enhancing software development by leveraging AI agents to automate code generation, review, and evaluation tasks. Developers can streamline their workflow and improve productivity by defining clear roles, goals, and tasks for each agent. Incorporating an AI Pair Programmer with CrewAI into your software development workflow can significantly enhance productivity and code quality.
CrewAI’s flexible framework allows for seamless collaboration between AI agents, ensuring your code is optimized, maintainable, and error-free. As AI technology evolves, leveraging tools like CrewAI for pair programming will become an essential strategy for developers to streamline their work and boost efficiency. With its versatile tools and collaborative features, CrewAI has the potential to revolutionize how we approach programming, making the process more efficient and effective.
A. CrewAI is a framework that leverages AI agents. It can be used to assist developers with tasks like writing, reviewing, and evaluating code. It enhances productivity by automating repetitive tasks, allowing developers to focus on more complex aspects of development.
A. The core components of CrewAI include Agents, Tasks, Tools, and Crews. Agents perform actions based on their defined roles, Tasks specify the objectives, Tools extend the agents’ capabilities, and Crews allow multiple agents to collaborate on complex workflows.
A. To set up an AI agent, you define its role (e.g., “Software Engineer”), goal (e.g., “Write optimized code”), and backstory for context, and specify the LLM (language model) to be used. You also create a corresponding Task that details the problem and expected output.
A. Yes, CrewAI agents can collaborate on tasks by being part of a “Crew.” Each agent in the crew can have a specific task, such as writing code or reviewing it, allowing for efficient teamwork among AI agents.
A. CrewAI agents can use various tools to enhance their capabilities, such as reading files, conducting web searches, or running code. These tools can be assigned to agents and tasks, allowing for more dynamic and powerful workflows.