[Cloud Computing | AWS] New AI Programming Assistant Amazon CodeWhisperer: Advantages and Practical Tips

Insert image description here

1. Introduction to Amazon CodeWhisperer

1.1 What is CodeWhisperer

Amazon CodeWhisperer uses machine learning to provide real-time code suggestions as you code. These tailored recommendations fit your current codebase, from comments to full functionality. Additionally, the tool performs security scans to improve code quality by pinpointing possible vulnerabilities. By providing personalized, context-sensitive support, CodeWhisperer increases efficiency and promotes the adoption of coding best practices.

In one sentence, Amazon CodeWhisperer is an AI programming assistant launched by Amazon.

Let me mention here that there are two types of Amazon CodeWhisperer, one is for personal use and the other is professional grade. The rough distinction is summarized as follows:

CodeWhisperer Personal use: Code suggestions and reference tracking are free; plus 50 free code scans per month.
CodeWhisperer Professional: Billed monthly based on maximum number of users (i.e. per user per month).

1.2 How Amazon CodeWhisperer works

My feeling when using Amazon CodeWhisperer is that you can think of Amazon CodeWhisperer as an artificial intelligence companion.

When you write code in the IDE, or have questions about writing comments, etc., the IDE will automatically send your requirements or what you want to Amazon CodeWhisperer. Amazon CodeWhisperer will use AI to generate a code based on the previous code and comments in the IDE. Provides suggestions like autocompletion as you write code. You can start by writing the code you want to use, or write a natural language description of what you want the code to do.

Insert image description here

2. Advantages and functions of Amazon CodeWhisperer

2.1 Advantages of Amazon CodeWhisperer

During personal research and study, I found that Amazon CodeWhisperer has many advantages. After all, it is developed by a large company. Its main functions can be summarized as follows:

  1. Universal code generator: Amazon CodeWhisperer is a tool designed to help developers by generating code suggestions in real time.

  2. Machine Learning Powered: CodeWhisperer uses machine learning algorithms to understand and analyze the code you write. It leverages this analysis to generate relevant and context-aware code recommendations.

  3. Real-time suggestions: CodeWhisperer provides automatic, instant code suggestions as you write code. This real-time feedback can help streamline the coding process and increase productivity.

  4. Context-aware recommendations: CodeWhisperer generates recommendations that are tailored to your specific codebase. It takes into account your existing code, comments, and current context to provide relevant recommendations.

  5. Recommendations vary in scope: CodeWhisperer's recommendations may vary in complexity. They can range from simple comments or code comments to entire functions or blocks of code.

  6. Increased productivity: By providing relevant code suggestions, CodeWhisperer aims to help developers write code more efficiently, reducing the need to manually search for code examples or syntax.

  7. Code Security Analysis: In addition to code generation, CodeWhisperer has the ability to scan your code for security issues. It highlights potential security vulnerabilities and helps you identify and resolve security issues in your code base. By providing real-time recommendations and highlighting security issues, CodeWhisperer promotes coding best practices and encourages compliance with coding standards.

  8. Developer-centric: CodeWhisperer is designed to support developers in their coding tasks. It is designed to provide meaningful recommendations that are consistent with developer intent and coding practices. CodeWhisperer's code suggestions are tailored to a developer's style and coding preferences, creating a more personalized coding experience.

  9. Learning potential: Developers can learn from the recommendations generated by CodeWhisperer, gaining insights into coding patterns and best practices.

2.2 Code functions of Amazon CodeWhisperer

In fact, as developers, most of our focus is on the diamante-level suggestions that Amazon CodeWhisperer can give. After experiencing it, we can see several suggestions given by CodeWhisperer:

  • Single-line code completion: Provides suggestions for completing single-line code as you type, helping with syntax and function calls.

  • Complete function generation: Automatically generate complete functions based on context and encoding mode.

  • Block Completion: Provides suggestions for completing code blocks such as loops or conditionals to improve code structure.

  • Docstring and Javadoc completion: Generate docstrings for functions and classes to help document code.

  • Line-by-line suggestions: Provide real-time line-by-line coding suggestions to improve code quality and efficiency.

  • Single-line comment completion: Provides suggestions for completing single-line comments to help code explanation and comments.

For example, you can accept suggestions or view multiple code suggestions by clicking the right arrow.

3. Amazon CodeWhisperer installation

3.1 Install into IntelliJ IDEA

  1. Click Setting in IntelliJ IDEA to open the settings window, then click Plugins to enter the plug-in page
  2. Search for AWS ToolKit in the search bar
  3. Click Install to install the plug-in

Insert image description here

  1. After the plug-in is installed, click OK in the lower right corner to confirm the installation. After the installation is complete, you will see the AWS ToolKit plug-in in the IDE.

Insert image description here

  1. Enable CodeWhisperer In the pop-up window, select "Register and log in with Builder ID using your personal email address" and click Connect in the lower right corner.

Insert image description here

  1. Login Builder ID

Insert image description here

When logging in you will see the following window. Please click "Open and copy verification code" and use the verification code to verify.

Insert image description here

Jump to your browser and paste the verification code you just copied

Insert image description here

You will then be redirected to the login registration page. If this is your first time, please register with your email address. After registering and logging in, you can start using CodeWhisperer

Insert image description here

3.2 Install into VS Code

It is similar to installing into IntelliJ IDEA. I won’t go into too much introduction here. Just find Amazon’sAWS Toolkit plug-in in the plug-in market and install it directly.

Insert image description here

4. Practical use of Amazon CodeWhisperer

4.1 Generate corresponding code based on comments

The prompt words given are as follows (create a function to create a DynamoDB table):

# Create a function to create a DynamoDB table

Solution given by Amazon CodeWhisperer:
Insert image description here

In addition to trying to generate more specific cases, the prompt words we gave are as follows:

# 函数创建DynamoDB表,以员工ID为主键和 readcapacity单 位为100和写入容量单位为200
# Function to create a DynamoDB table with employee ID as primary key and ReadCapacityUnits at 100 and WriteCapacityUnits at 200

Solution given by Amazon CodeWhisperer:
Insert image description here

It can be seen from here that, just like ChatGPT, if your prompt words are more specific, the content generated by CodeWhisperer will be more specific and meet your expectations. Therefore, if you write the prompt words appropriately to CodeWhisperer, you can better let him Come to help you.

4.2 Test returns multiple records

Here we use CodeWhisperer to generate multiple records based on the provided code.

The test code used is as follows:
Insert image description here
We give our own needs and corresponding prompt words to CodeWhisperer

# Patch get_products and test lambda_handler returns a multiple records
# Patch get_products和test lambda_handler返回多条记录

CodeWhisperer generates and returns the corresponding code based on our prompt words

# Patch get_products and test lambda_handler returns multiple records
@patch('product_api.get_products')
def test_lambda_handler_multiple(self, mock_get_products):
mock_get_products.return_value = [{
    
    'id': '1', 'name': 'test', 'description': 'test'}, {
    
    'id': '2', 'name': 'test2', 'description': 'test2'}]
response = product_api.lambda_handler(None, None)
self.assertEqual(response['statusCode'], 200)
self.assertEqual(response['body'], '[{
    
    "id": "1", "name": "test", "description": "test"}, {
    
    "id": "2", "name": "test2", "description":
"test2"}]')

5. CodeWhisperer Learning Gifts

Since the launch of the Cloud Discovery Lab, more and more people have praised the AI ​​programming assistant Amazon CodeWhisperer. In order for everyone to avoid detours during the experience and quickly get started with Amazon CodeWhisperer, the Cloud Exploration Laboratory-Code School is here! In order to allow more partners to join the course camp, the Cloud Exploration Laboratory is open to all partners and specially recruits students to lead the Code School. Inviting people to join the Code School will give you good gifts.

Activity link:https://mp.weixin.qq.com/s/WgeRzML5MogLSWeM57zv3w

Insert image description here

(Prizes for participating in learning)

CodeWhisperer course channel:

  • CSDN platform: https://edu.csdn.net/course/detail/38969?utm_source=AWS-Paid-Builder-MKT-Social-YS-CW-CSDN
  • NetEase Cloud Classroom:
    https://study.163.com/course/introduction/1213602804.htm?from=AWS-Paid-Builder-MKT-Social-YS- CW-NECC
  • Skill Builder 平台:
    https://explore.skillbuilder.aws/learn/course/external/view/elearning/17168/amazon-codewhisperer-getting-started-simplified-chinese?trk=d15c46ed-1e4e-4e8b-82c0-63ba2feb26b9&sc_channel=el

Insert image description here

Guess you like

Origin blog.csdn.net/dietime1943/article/details/134453885