Amazon CodeWhisperer - Setup and Test Drive

Sun, Dec 31, 2023

Read in 5 minutes

We go through the steps of setting up Code Whisperer and using it to automatically generate code suggestions

Amazon CodeWhisperer - Setup and Test Drive

You probably heard a lot about GitHub Copilot or may have tried the preview and found it impressive. For a lot of power developers looking to supercharge their productivity, scale their output, and confidently learn new technology at rapid pace, the concept of AI pair programming has been a real power horse. Amazon CodeWhisperer from the AWS stable offers the same functionality – currently still in preview and available at no cost to developers.

Here are the steps to set it up for your individual use.

Pre-requisites

The first pre-requisite to use CodeWhisperer service is to setup an AWS Builder ID for yourself using a personal email address. What is a “AWS Builder ID” you ask? Here is a post that answers just that question for you and with guide you through the steps of setting up one for yourself – What is AWS Builder ID and how to get one

Once this step is done, you are ready to proceed with the setup on your local development environment and IDE. At present time CodeWhisperer works with these IDEs – Jetbrains, VS Code, Cloud 9.

Setup for Visual Studio Code IDE

We will use Visual Studio Code for today’s walk-through.

Install the VS Code Extension called “AWS Toolkit”. The easiest way is to go to “Extensions” tab in your VS Code UI and search for “AWS toolkit”, confirm that you are seeing the official extension from Amazon Web Services, and click on “Install”

Installing AWS Toolkit extension for Visual Studio Code

Once Installed, you should see the “aws” icon added to the left navigation tabs on VS Code IDE.

Click on it, look for “Developer Tools” pane on the left and click on “Start” under CodeWhisperer (Preview)

You should see an option to “add a connection to AWS” popup on the command palette.

Setup CodeWhisperer

Do not worry about authenticating with IAM Credentials since that is not necessary for using CodeWhisperer. As a reminder, you do not need to connect to any AWS Account in order to access the CodeWhisperer services – and you do not incur any changes for the same (at least during the current preview which has no published timeframe or enddate).

Similarly SSO credentials are for those that will use CodeWhisperer at workplace through their work-related AWS Organizations setup and SSO identities. We will skip that for now and focus on the use case for individual developers.

Click on the first option – “Use a personal email to sign up or sign in with AWS Builder ID”.

If you followed the steps in the pre-requisites section above and have already created an AWS Builder ID for yourself, you can use that the login and “allow” this specific instance of VS Code access to your builder profile.

Allow VS Code access to your Builder profile

After this you are presented with Terms and Conditions to accept. You can also optionally turn off code sharing with AWS if you do not wish that your code and context be shared with and retained by AWS for future training and model enrichment purposes.

With that you are all set. There is nothing else for you to do in order to start using the service. Whenever you are programming in this IDE the CodeWhisperer service will be running in the background and offering you suggestions based on context.

Start Coding with AI Pair Programmer

We will use Python as example for this walk-through. CodeWhisperer supports a number of other popular languages like Java, Ruby, Go, C# as well.

Depending on the language you are programming in and the context provided through rest of the code and immediately adjacent comments, the model will constantly look to generate suggestions of code completion that appears in grey text near your cursor. You can press <tab> to accept a suggestion. You can keep typing or hit <esc> to ignore a suggestion. When there are more than one suggestions, you can use the left and right arrow keys to cycle through them and choose one. You can also force a CodeWhisperer suggestion at your current cursor position by hitting Alt+C (on a Mac use Command+C).

For the test drive I edited a python file and the context was the implementation of a Back-Propagation algorithm from scratch (hat tip: Andrej Karpathy).

I entered a brief comment describing an additional method and pressed Alt+C and accepted the suggestions – which were fairly accurate in terms of the intended functionality and syntactically correct implementation.

My input at this stage (apart from rest of the code context) was simply this comment (yes including a typo):

method for matix multiplication

And this is the code that was automatically generated:

    # method for matix multiplication
    def __mul__(self, other):
        out = Value(self.data*other.data,self.label+'*'+other.label, (self,other), '*')
        return out

Here is a screenshot showing this in action:

Python Code Generation

This is pretty impressive for a trial run. Obviously we need to use it in many different contexts to judge the effectiveness and the code suggestions may not always be this spot-on. And the need for human review and oversight cannot be over-stated. That said, this tool promises to be a powerful tool that every programmer can benefit from. Further iterations and refinements of this technology – especially with now multiple players competing for attention in this space – are bound to be exciting. Watch this space!