• Home
  • About Me
  • Contact
  • Privacy Policy
✉ NEWSLETTER
Tech News, Magazine & Review WordPress Theme 2017
  • Home
  • Tech
    • A.I.
    • EdTech
  • Stock Market
  • Crypto
    • Bitcoin
    • Ethereum
    • NFT
  • All Links
  • Shop
    • Merch!
    • Shop Amazon Gadgets
No Result
View All Result
  • Home
  • Tech
    • A.I.
    • EdTech
  • Stock Market
  • Crypto
    • Bitcoin
    • Ethereum
    • NFT
  • All Links
  • Shop
    • Merch!
    • Shop Amazon Gadgets
No Result
View All Result
Technical Terrence

Create an AI Peer Programmer with CrewAI

Technical Terrence Team by Technical Terrence Team
10/08/2024
Home Tech A.I.
ADVERTISEMENT
Share on FacebookShare on Twitter

Introduction

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 as pair programmers. In this article, we will learn how to build an ai peer scheduler using ai Crew agents to simplify your coding tasks and increase your productivity in software development.

Overview

  • Understand the basics of CrewAI and its role in assisting with coding tasks.
  • Recognize the key components (agents, tasks, tools, and teams) and their interactions.
  • Get hands-on experience setting up ai agents for code generation and review.
  • Learn how to configure multiple ai agents to work together on coding tasks.
  • Develop skills to use CrewAI to evaluate and optimize code quality.

<h2 class="wp-block-heading" id="h-what-can-an-ai-pair-programmer-do”>What can an ai peer programmer do?

Let's explore some use cases of what an ai peer programmer can do.

  1. Write code: We can generate the code for a given problem using one ai agent and review it with another agent.
  2. Improve existing code: If we write the code, our peer programmer can evaluate it based on the evaluation requirements.
  3. Optimize code: We can request any changes to the code to optimize it, such as adding comments, a docstring, etc.
  4. Debug code: Bugs are inevitable in code. But unlike the rubber duck, our peer programmer can make suggestions.
  5. Write test cases: Let the ai ​​agent write the test cases for each edge case. We can even use it for test-driven development.

In this article, we will cover the first two tasks.

What is CrewAI?

CrewAI is a popular framework for creating ai agents. It consists of key components such as agents, tasks, tools, and teams.

  • Agent: Basically, an agent uses a large language model (LLM) to generate results based on input cues. You can call various tools, accept human input, and communicate with other agents to complete tasks.
  • Task: A task describes what the agent will accomplish, specifying the description, the agent to use, and the tools that can be called upon.
  • Tool: Agents can use various tools to perform tasks such as web searches, read files, and execute code, enhancing the capabilities of the ai ​​agent.
  • Crew: A crew is a group of agents who collaborate to complete a set of tasks. Each team defines how agents interact, share information, and delegate responsibilities.

Also Read: Creating Collaborative ai Agents with CrewAI

Now, let's build an agent to understand it better!

What are the prerequisites?

Before creating an ai Pair Developer with a CrewAI agent, make sure you have the API keys for the LLMs.

Accessing an LLM through API

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 also making it easily accessible within your project.

Example of an .env file

Below is an example of what a .env file looks like:

Required Libraries

We have used the following versions for the main libraries:

  • crew – 0.66.0
  • tools-tripai – 0.12.1

Automating code creation with CrewAI

In this section, we will import the necessary libraries and define agents to generate and review code. This practical approach will help you understand how to use CrewAI effectively.

Import the necessary libraries

from dotenv import load_dotenv
load_dotenv('/.env')

from crewai import Agent, Task, Crew

Code Writer Agent Definition

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)

Explanation of agent parameters

  • role: It defines what the agent is and defines it based on the tasks we want the agent to complete.
  • Goal: Defines what the agent is trying to achieve. The agent makes the decisions to achieve this goal.
  • backstory: Provides context to the agent's role and objective. This can be used for better interaction with the agent as it is sent as a message to the LLM.
  • llm: LLM used in the agent. CrewAI uses LiteLLM to call LLMs, so check their documentation for available models.
  • verbose: we can set verbose=True to observe the input and output of the agent.

Defining the code writer's task

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)

Explanation of task parameters

  • description: Write a clear and detailed statement of the objectives of the task that needs to be completed. Braces {} are used to indicate variables. In this case, we pass the “problem” and “language” values ​​to the task.
  • expected_output: What the result of the task should look like. We can also get structured results using Pydantic classes.
  • agent: defines the agent that should be used to accomplish this task.

Defining the agent and the code reviewer task

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)

Building and leading the crew

Now we can build the team 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 result will be the following:

Building and leading the crew
Building and leading the crew

Result

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:

three in a row

<h2 class="wp-block-heading" id="h-automated-code-evaluation-with-crew-ai“>Automated code evaluation with Crew ai

After creating the code generation and review agents, we will now evaluate an existing code file.

Meeting requirements

First, we will gather evaluation requirements for a problem using one agent and then evaluate the code against those requirements using another agent.

Using tools for enhanced capabilities

We will use FileReadTool to read system files. The tools enhance agent capabilities by enabling actions such as reading files or searching the Internet.

We can assign tools to both tasks and agents. Tools assigned in Tasks will override agent tools.

Initialize agent and task for requirements gathering

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 previous task, we set human_input to True. Then, the agent requests information from the user once it generates the requirements. We may ask you to make changes if necessary.

Code evaluation

Now, make the same evaluation. Here, we use the tool to read the file. We also use GPT-4o to get better results since 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)                            

Form the team for the evaluation

Let's form the team and define the problem to obtain the requirements.

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})

Task output

This is what the result will look like when requesting human participation:

Task output

We can also get the result of any task as follows:

print(code_requirement_task.output.raw)

# final output
print(result.raw)

This way, we can form versatile teams to create our own ai peer programmer.

Conclusion

CrewAI offers a powerful framework to improve 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 peer programmer with CrewAI into your software development workflow can significantly improve productivity and code quality.

CrewAI's flexible framework enables seamless collaboration between ai agents, ensuring your code is optimized, maintainable, and bug-free. As ai technology evolves, leveraging tools like CrewAI for pair programming will become an essential strategy for developers to optimize their work and increase efficiency. With its versatile tools and collaborative features, CrewAI has the potential to revolutionize the way we approach programming, making the process more efficient and effective.

Frequently asked questions

P1. What is CrewAI and how does it help in software development?

A. CrewAI is a framework that leverages ai agents. It can be used to help developers with tasks such as writing, reviewing, and evaluating code. Improves productivity by automating repetitive tasks, allowing developers to focus on more complex aspects of development.

P2. What are the key components of CrewAI?

A. The core components of CrewAI include agents, tasks, tools, and teams. Agents perform actions based on their defined roles, Tasks specify objectives, Tools extend agent capabilities, and Teams allow multiple agents to collaborate on complex workflows.

P3. How do I configure an ai agent in CrewAI to generate code?

A. To configure an ai agent, you define its role (e.g., “Software Engineer”), goal (e.g., “Write optimized code”), and its backstory for context, and specify the LLM (language model) to be used. It also creates a corresponding task that details the problem and the expected result.

Q4. Can CrewAI agents work together on tasks?

A. Yes, CrewAI agents can collaborate on tasks as part of a “crew.” Each crew agent can have a specific task, such as writing code or reviewing it, allowing for efficient teamwork between ai agents.

Q5. What tools can be used with CrewAI agents?

A. CrewAI agents can use various tools to enhance their capabilities, such as reading files, performing web searches, or executing code. These tools can be assigned to agents and tasks, allowing for more dynamic and powerful workflows.

Santhosh Reddy Dandavolu

I work as an Associate Data Scientist at Analytics Vidhya, a platform dedicated to building the data science ecosystem. My interests lie in the fields of deep learning and natural language processing (NLP).

Related

Tags: AIAI Pair ProgrammerCodingcreateCrewAIPeerProgrammersoftwaresoftware developmenttools
Technical Terrence Team

Technical Terrence Team

Next Post
Will it be too late to buy Nvidia stock in March?

Could a new AI deal with Google give Vodafone's share price a fresh boost?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

Nvidia announced real-time AI NPCs in video games

Nvidia announced real-time AI NPCs in video games

06/01/2023
Winners Announced for “Educators Pick Best of STEM® 2023” Awards

Winners Announced for “Educators Pick Best of STEM® 2023” Awards

08/25/2023
Decoding the Data Scientist Hierarchy: Junior to Senior – What Sets Them Apart?  |  by Guillaume Colley |  October 2023

Decoding the Data Scientist Hierarchy: Junior to Senior – What Sets Them Apart? | by Guillaume Colley | October 2023

10/13/2023
Beware of these NFT scammers on Twitter, warns On-Chain Sleuth

This is how scammers adapted to Crypto Winter: Chainalysis

03/25/2023
UK government not going forward with NFT issuance

UK government not going ahead with issuing NFTs

03/29/2023
ADVERTISEMENT
Youtube Twitter Instagram Facebook Twitch
Technical Terrence

Follow Us

Categories

  • A.I.
  • Bitcoin
  • Crypto
  • EdTech
  • Ethereum
  • NFT
  • Stock Market
  • Tech
✉ NEWSLETTER

Important Links

  • Home
  • About Me
  • Contact
  • Privacy Policy

Copyright 2023 © All rights Reserved. TechnicalTerrence Team

No Result
View All Result
  • Home
  • Tech
    • A.I.
    • EdTech
  • Stock Market
  • Crypto
    • Bitcoin
    • Ethereum
    • NFT
  • All Links
  • Shop
    • Merch!
    • Shop Amazon Gadgets

Copyright 2023 © All rights Reserved. TechnicalTerrence Team

bitcoin
Bitcoin (BTC) $ 99,500.66
ethereum
Ethereum (ETH) $ 3,555.07
bnb
BNB (BNB) $ 683.27
solana
Solana (SOL) $ 202.35
xrp
XRP (XRP) $ 2.29
cardano
Cardano (ADA) $ 0.932201
dogecoin
Dogecoin (DOGE) $ 0.344247
shiba-inu
Shiba Inu (SHIB) $ 0.000023
avalanche-2
Avalanche (AVAX) $ 41.39
polkadot
Polkadot (DOT) $ 7.37
matic-network
Polygon (MATIC) $ 0.500737
litecoin
Litecoin (LTC) $ 105.11
optimism
Optimism (OP) $ 2.05
crypto-com-chain
Cronos (CRO) $ 0.170849
kaspa
Kaspa (KAS) $ 0.125146
injective-protocol
Injective (INJ) $ 23.04
pepe
Pepe (PEPE) $ 0.000019
bonk
Bonk (BONK) $ 0.000029
jasmycoin
JasmyCoin (JASMY) $ 0.035948
bitcoin
Bitcoin (BTC) $ 99,500.66
ethereum
Ethereum (ETH) $ 3,555.07
bnb
BNB (BNB) $ 683.27
solana
Solana (SOL) $ 202.35
xrp
XRP (XRP) $ 2.29
cardano
Cardano (ADA) $ 0.932201
dogecoin
Dogecoin (DOGE) $ 0.344247
shiba-inu
Shiba Inu (SHIB) $ 0.000023
avalanche-2
Avalanche (AVAX) $ 41.39
polkadot
Polkadot (DOT) $ 7.37
matic-network
Polygon (MATIC) $ 0.500737
litecoin
Litecoin (LTC) $ 105.11
optimism
Optimism (OP) $ 2.05
crypto-com-chain
Cronos (CRO) $ 0.170849
kaspa
Kaspa (KAS) $ 0.125146
injective-protocol
Injective (INJ) $ 23.04
pepe
Pepe (PEPE) $ 0.000019
bonk
Bonk (BONK) $ 0.000029
jasmycoin
JasmyCoin (JASMY) $ 0.035948

Get daily news updates to your inbox!

Subscribe to our mailing list to receives daily updates!