
AWS CodeWhisperer - ML Augmented Development (3 of 3)
In the previous blog we talked about ChatGPT and went through some examples on how to use it to help develop solutions. Today I'm going to do a similar experiement with AWS CodeWhisperer.
AWS CodeWhisperer was released in preview in June 2022 as a ML powered developer productivity tool. With CodeWhisperer you provide a prompt via a code comment. Then CodeWhisperer reads the comment and provides a line or segment of code that best matches what you were trying to achieve.
Experiments:
We started very similar to ChatGPT. With a random walk.
I provided several prompts long the way but I ended up with
import random import matplotlib.pyplot as plt import numpy as np import math #a function for a random walk (prompt) def random_walk(n): x = 0 y = 0 for i in range(n): (dx, dy) = random.choice([(0,1), (0,-1), (1,0), (-1,0)]) x += dx y += dy return (x,y) #a function for a random walk displayed in matplotlib (prompt) def random_walk_plot(n): x = [] y = [] for i in range(n): (x1, y1) = random_walk(1) x.append(x1) y.append(y1) plt.plot(x, y, 'o') plt.show() random_walk_plot(200)
The interesting thing about AWS CodeWhisperer is that it learned from the previous section and reused the function below. This context awareness is incredibly helpful. Unfortunately this function doesn’t really plot a random walk with n steps. Instead it plots n walks 1 step from (0,0). These kinds of small logic bugs are pretty frequent in all the ML Programming Tools that I’ve tested.
Next I tried to create a lambda function to pull a message from SQS and sent it to MailChimp
a lambda function to pull a message from SQS and send it to Mail chimp (prompt)
import boto3 import json import os from botocore.exceptions import ClientError from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.image import MIMEImage from email.mime.base import MIMEBase from email import encoders from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText
It Very quickly got stuck in a loop suggesting the same code over and over again. removed duplicate code and provided another promot.
import boto3 import json #lambda function to get a message from SQS def lambda_handler(event, context): #get a message from SQS queue (prompt) sqs = boto3.resource('sqs') queue = sqs.get_queue_by_name(QueueName='test') message = queue.receive_messages() #parse message message = message[0] message_body = json.loads(message.body) message_body = message_body['Message'] message_body = json.loads(message_body) message_body = message_body['Message'] message_body = json.loads(message_body) message_body = message_body['Message']
It got further but got caught in another loop. In fact, CodeWhisperer got caught in a number of loops before it finished.
Eventually I ended up with:
import boto3 import json #lambda function to get a message from SQS def lambda_handler(event, context): #get a message from SQS queue (prompt) sqs = boto3.resource('sqs') queue = sqs.get_queue_by_name(QueueName='test') message = queue.receive_messages() #parse message message = message[0] message_body = json.loads(message.body) message_body = message_body['Message'] #send message_body to mailchimp: (prompt) data = { 'email_address': message_body, 'status': 'subscribed' } #get user and pass from secret manager (PROMPT) secret_name = "mailchimp" region_name = "us-east-1" session = boto3.session.Session() client = session.client( service_name='secretsmanager', region_name=region_name ) get_secret_value_response = client.get_secret_value( get_secret_value_response = client.get_secret_value( SecretId=secret_name ) ) secret = json.loads(get_secret_value_response['SecretString']) user = secret['user'] passw = secret['pass'] mailchimp_list = secret['list'] mailchimp_api = secret['api'] mailchimp_url = secret['url'] mailchimp_client = boto3.client( 'api-request-logs', aws_access_key_id=user, aws_secret_access_key=passw, region_name=region_name ) #send to mailchimp r = requests.post('https://us17.api.mailchimp.com/3.0/lists/a7c8c8a8a4/members', auth=('user', 'pass'), data=json.dumps(data)) #check response if r.status_code == 200: print('Success!') else: print('Error: %d' % r.status_code) print(r.text) raise Exception('Mailchimp Error') return 'Error' #delete message from queue message.delete() return 'Success'
Findings:
AWS CodeWhisperer can't provide whole functions of code the way ChatGPT did, but being able to provide a prompt and get started is extremely useful. Additionally, the ability to provide internal prompts make getting the next step correctly without needing to describe the whole solution from the beginning.
The biggest problem with AWS CodeWhisperer is that it did tend to get stuck frequently, and created some obvious syntax problems.
Add new comment