实测 亚马逊 AI 编程助手 Amazon CodeWhisperer(全网最全)
文章作者:疯狂的驸马
一、CodeWhisperer 简介
CodeWhisperer 是亚⻢逊出品的一款基于机器学习的通用代码生成器,可实时提供代码建议。
在编写代码时,它会自动根据我们现有的代码和注释生成建议。从单行代码建议到完整的函数,它可为我们提供各种大小和范围的个性化建议。
亚马逊云科技开发者社区为开发者们提供全球的开发技术资源。这里有技术文档、开发案例、技术专栏、培训视频、活动与竞赛等。帮助中国开发者对接世界最前沿技术,观点,和项目,并将中国优秀开发者或技术推荐给全球云社区。如果你还没有关注/收藏,看到这里请一定不要匆匆划过,点这里让它成为你的技术宝库!
CodeWhisperer 还可以扫描我们的代码以突出显示和定义安全问题。

codewhispereropen in new window:亚马逊官方出品
目前仅以插件的形式在VS Codeopen in new window、Jetbrainsopen in new window等 IDE 里面使用,如果想试一试可以用 VS Code,目前无限制免费使用免费,并且对环境要求不高,还轻量 ~
亚马逊在 2022 年 6 月发布了 CodeWhisperer 预览版,现在它支持 Python、Java 和 JavaScript。

CodeWhisperer 经过数十亿行代码的训练,由机器学习提供支持,旨在实现相同的目标。无论我们是学生、新开发人员,还是经验丰富的专业人士,CodeWhisperer 都有助于我们提高工作效率。
其支持多种 IDE 和语言。要开始使用,我们只需安装合适的 Amazon IDE Toolkit,启用 CodeWhisperer 功能,输入我们的预览访问代码,然后开始键入:

CodeWhisperer 可以持续检查我们的代码和注释,并为我们提供语法正确的推荐。这些推荐根据您的编码风格和变量名称合成,而不仅仅是代码段。
CodeWhisperer 使用多个上下文线索来提供推荐,包括源代码中的光标位置、光标前面的代码、注释以及同一项目中其他文件中的代码。您可以按原样使用推荐,也可以根据需要对其进行改善和自定义。正如我之前所提到的,我们使用从开源存储库、内部 Amazon 存储库、API 文档和论坛中提取的数十亿行代码训练(并将继续训练)CodeWhisperer。
在亚马逊云科技博客的一篇文章中,Mark Richman 解释说,CodeWhisperer 的模型是在“包括 Amazon 开源代码在内的各种数据源”上训练的。有了这个语料库(显然确实存在)完善 CodeWhisperer 的模型,编写从 S3 读取文件的代码应该是一个很好的测试用例。
在使用 CodeWhisperer(CW)时,我们需要写一个注释,描述我们希望函数去做什么。注释的描述性和准确性越高,系统就越能更好地推断出我们想要的逻辑。
Function to open an S3 file
比如注释以 Function 开头,让 CW 知道你想要创建一个函数。也就是说,你需要添加一个注释,作为给 CW 的提示。
CW 分析注释并生成一个函数的定义。此时,你可以在生成函数体之前修改函数定义。CW 还可能提供多种函数定义供你选择。
IntelliJ 集成 CodeWhisperer 的截图:

点击“插入代码”,你的函数就在注释的下方创建好了。注意 CodeWhisperer 不仅插入了代码,还创建了一个文档字符串。
看起来不错!这段代码实现了你的注释所期望的功能,并且是在几秒钟内就生成了。
节省了查找 boto3 API 的时间,你只需要检查代码,确保语义正确。
接着看看提出更多要求时,会发生什么。
这有一个很有用的例子:写一个函数从 S3 的文件中返回前“n”行。
真厉害!CodeWhisperer 使用了之前创建的辅助方法 open_s3_file,并生成了正确的函数。
在 CW 生成函数体之前,你可以修改参数名,提高代码的可读性。例如,如果我们输入:
CW 会提示:
在生成函数体之前,我们可以修改函数定义,使其更具描述性:
如果我们接受它的函数定义,CodeWhisperer 将使用函数签名和注释来构建函数体。得到的函数将使用改进后的参数名。
CodeWhisperer 不仅仅可以创建整个函数;它还可以在函数中插入代码片段,并在过程中推断相关变量。
1.1 CodeWhisperer 是一个重要的生产力助推器
使用 CodeWhisperer,我能够更快地编写代码。到目前为止,最大的价值是 S3 示例所演示的功能。
如果我自己写代码,大部分时间将会用来查找 boto API 文档,以确保写出正确的调用。使用 CodeWhisperer,则只需三秒钟。
这让我想到了一段曾花了很多时间的烦人代码:给 CloudWatch 发送数据。让我们看看 CodeWhisperer 能否有所帮助,试试提供注释:“Function to emit a CloudWatch metric”。
非常有用!CW 为我节省了许多打字和查阅 API 文档的时间。
我有可能需要重构这段代码,但即使我从零开始编写它,第一步也要写出类似这样的原始逻辑。如今只花三秒钟,我节省了编写大量样板代码的时间,有了一个很好的自定义或重构的起点。
1.2 CodeWhisperer 的实际应用
我在 PyCharm 中安装了 CodeWhisperer ,并对其进行了测试。以下是一些示例,向您展示了它可以执行的操作。我想构建一个质数列表。我键入 # See if a number is pr。CodeWhisperer 完成此操作,然后我按下 TAB(实际键特定于每个 IDE)接受推荐:

在下一行,我按下 Alt+C(同样是 IDE 特定),然后我可以在一对函数定义之间进行选择。我接受第一个函数,CodeWhisperer 推荐函数体,以下是我所拥有的:

我编写 for 语句,CodeWhisperer 推荐循环的主体:

CodeWhisperer 还可以帮助我编写用于访问各种亚马逊云科技服务的代码。我从 # create S3 bucket 开始,然后按 TAB 键完成剩余部分:

看到这里,大家是不是觉得还不错,接下来教大家如何安装,很简单的~
二、CodeWhisperer 安装教程
2.1 IntelliJ IDEA 安装 CodeWhisperer 插件
在 IDEA 中打开配置窗⼝,选择 Plugins,搜索"Amazon Toolkit",点击 Install,点击 OK 按钮,如下图:

安装完之后重启 IDEA,如下图:

打开 Amazon Toolkit 视图(菜单 View/Tool Windows/AWS Toolkit),点击"Developer Tools"tab⻚⾯,选择“CodeWhisperer/Start",如下图:


弹出的窗⼝中选择“Use a personal email to sign up and sign in with Amazon Builder ID",点击“Connect”按钮,如下图:

在弹出的窗⼝中,选择“Open and Copy Code”,如下图:

此时会在浏览器中打开⼀个⻚⾯,按 ctrl-v 粘贴 code 值,点击“Next“,如下图:

输⼊邮箱地址,点击"Next",如下图:

输⼊名字,点击“Next”,CodeWhisperer 会向邮箱中发送⼀个验证码,如下图:

打开邮箱,可以看到验证码,如下图:

复制验证码,粘贴到输入框,点击“Verify”按钮,如下图:

设置密码,点击“Create AWS Builder ID“,如下图:

在最后⼀个⻚⾯中点击“Allow”按钮,如下图:

出现如下提示后,即表示注册 Amazon builder ID 成功,如下图:

返回 IDEA,在 Amazon Toolkit 视图中的 Developer Tools 中可以打开或关闭代码⽣成功能,如下图:

此时,就可以愉快的使用 AI 编程了。
2.2 VSCode 安装 CodeWhisperer 插件
安装 Amazon Toolkit 插件,具体到 VS Code 侧边栏搜索并安装

侧边栏点击 aws,>> DEVELIOPER TOOL >> CodeWhisper >> Start

在下拉菜单中点击 Use a personal email to sign up and sign in with Amazon Builder ID

点击 Copy Code and Proceed,这将自动复制代码

一般会提示外部网站打开提醒,选择打开

5.打开网站后,输入点击 Copy Code and Proceed 时得到的代码,点击 Next

6.输入自己的邮箱地址(同时登录你的邮箱等待验证码),点击 Next

7.重复之前步骤,但是由于已经在浏览器上登录成功了,所以步骤非常简单 粘贴代码后将会提示以下内容,点击 Allow 即可



等左下角的亚马逊云科技扩展颜色正常,对勾状态,说明连接成功

此时,就可以愉快的使用 AI 编程了。
2.3 PyCharm 安装 CodeWhisperer
1.打开 Pycharm 插件管理
在 JetBrains IDE 中,导航到设置菜单(在 macOS 上为⌘ + ,在 Windows 上为文件→设置),然后单击左侧菜单上的“插件”。
在菜单顶部,单击 Marketplace 并在搜索栏中键入 “Amazon Tookit”。然后点击安装。

2.安装完成后重启 IDE

3.将 Pycharm 连接到亚马逊云科技(Amazon Build ID 创建),重复之前步骤
IDE 重新启动后,您将看到一个新的 Amazon Toolkit 工具窗口。还可以通过 View -> Tool Windows -> Amazon Toolkit(视图 -< 工具窗口 -< Amazon Explorer)访问此窗口。
然后点击"Developer Tools"标签,选择“CodeWhisperer/Start"

此时,就可以愉快的在 PyCharm 里使用 AI 编程了。
2.4 快捷键使用方法
全程需要按快捷键调用 Codewhisperer,主要的几个用法如下:

三、简单自动编码演示
回到 IDEA,看到如下界面,即表示注册并启动成功:

下面我们新建任意 Java 文件,看看他如何能够帮助我们自动生成代码。
3.1 单行代码自动补全
输入字符串 public。根据输入,CodeWhisperer 生成了建议列表

3.2 CodeWhisperer 生成完整代码
CodeWhisperer 可以根据编写的注释生成完整的函数。


类似于 if/for/while 等代码块的生成。

大家如果看到这有疑问,可以接着看第四部分,有详细的测试说明。
3.3 其根据签名自动生成代码

看到这里,大家应该有个简单的了解了,咱们接着往下看~
四、AI 编程代码实测及项目体验
4.1 简单函数代码测试
首先,我先用中文注释了“写一个读取 csv 文件的函数” 回车后可以看到 CodeWhisperer 自动显示出来

按快捷键 Tab 确认键入后,接着回车后 CodeWhisperer 又自动生成了以下代码:

感觉这个 CodeWhisperer 自动生成的代码还不错,我选择了 Insert Code 键入使用。
紧接着用中文又注释了“创建一个简单的爬虫简单函数”

在这选择了第三个自动代码,同样 Tab 键入。
接下来是注释“冒泡排序”后回车

注释“选择排序”后回车

中文注释实测完后又对英文注释进行了测试,如下图所示:

乘胜追击,又对创建和上传文件代码进行自动生成测试,同样得到了预期的效果。

对于简单的函数实测让我感觉这个工具还挺不错的,因为可以根据自己想要的注释自动生成多个代码建议,这样不仅节省了大量的编码时间,而且多种代码建议可供选择,一定程度上提升了自己编写代码的质量。
4.2 代码安全扫描实测
接下来用 CodeWhisperer 去扫描我们代码中的安全漏洞,以下是运行按钮的位置

打开我想扫描的文件,然后运行安全扫描

结果发现 CodeWhisperer 探测到该文件中有两个安全问题。 通过查看问题栏,点击事件定位到代码中,我们将鼠标移入到突出显示的代码,查看建议的措施,如下所示,问题指出在该例子中未加密的亚马逊云科技凭证被记录下来,是一个安全漏洞,它建议我们重写代码并且修复该漏洞。

同样查看第二个问题,CodeWhisperer 告诉我们讲应该把标识设置为 True。

根据 CodeWhisperer 的建议,我进行了对应修改,两处安全问题修改如下:


修改后重新扫描了文件,扫描完成,显示没找到问题。

对于安全扫描这项功能,我还是挺惊艳的,没想到能实测找出问题并给出对应的解决问题,因为这一点我对 CodeWhisperer 的好感又增加了。
4.3 代码引用跟踪功能实测
在这里想实现一个函数来创建一个 dynamo DB 表。 如下图所示进行了实际测试 “implement a function to create a dynamoDB table”

以下是 CodeWhisperer Reference Log 给出的建议:

根据代码建议标注被文件跟踪器标记为 MIT 许可证,其引用了 Apache-2.0 许可证下的代码。我选择接受了这些标记代码,然后发现 CodeWhisperer 自动记录了引用我需要的许可证信息,这样我就可以在我的代码上添加适当的许可证和归属信息。 这个功能实测完感觉也挺有用的,对代码涉及到的许可证和归属信息问题进行了有效解决。
4.4 计算机视觉工程项目实测
在对官网显示的几项功能测试完后,又在平时用到的实际项目上进行了测试,我选取了常用的一些 CV 检测模型,以下是 ShuffleNet 的实际测试效果。
首先,我在实例化训练数据集上,对缺失的代码段进行测试,看 CodeWhisperer 是否能联系上下文进行代码补全。

进行回车操作后,CodeWhisperer 显现出来了所给的建议。

目前看所给的建议正确,接着我又对冻结权重部分进行了测试,

这回我又利用了 CodeWhisperer 的快捷键 ALT+C 进行了测试,

结果显示自动生成了 False 选项,测试到这里真的让我觉得这个工具是真的好,可以帮助我解决代码中遇到的很多问题。 在训练代码中测试完,我又接着对检测模型代码进行了测试,如下图所示在 transform 指向中为我建议了 img。

最后,我对检测模型代码进行了运行测试,正确的检测出图片为蒲公英 dandelion。

总的来说,CodeWhisperer 辅助我完成了 CV 模型的代码编写和模型检测,达到了官网所展示的预期。接下来,我要让 CodeWhisperer 帮助我完成更复杂的任务了,哈哈
五、Amazon CodeWhisperer 实测体验总结
5.1 CodeWhisperer 可以帮助我成为一个更好的开发者吗?
通过以上的测试,我觉得它可以帮助我成为一个更好的开发者。
首先,它可以为我节省大量的时间和精力,让我能够专注于改进、重构和测试。
其次,它通过承担一些同质化的繁重工作,让我有机会成为一个更好的程序开发人员。
比如上面的测试的例子是 Amazon 工具(经过 Amazon 开源代码训练)能够表现出色的例子。
当然,在大多数开发人员需要花费很多时间的地方,比如编写领域相关的逻辑时,我又多测试了一下,让我们看看 CodeWhisperer 会不会也有帮助。
比如从 Python 文档中的数据类示例开始。

其实我想知道 CodeWhisperer 是否可以向这个类添加一个方法。让我们看看如果添加注释:" Function that return this item costs more than $10",会发生什么?

结果是非常酷的。值得注意的是,CodeWhisperer 给函数起了一个直观的名字,并包含了对 self 的引用。
接着,让我们尝试用 CodeWhisperer 来做测试,看是否会触及它的极限。

在上面的代码中,我输入了注释,CW 自动完成了剩下的工作。 测试似乎是一个极好的证明 CW 可以节省时间的例子。我不需要浪费时间去想测试的值,也不用输入所有的成员变量和方法。
总的来说,可以帮助我成为一个更好的开发者,但是任何辅助工具都有利有弊,CodeWhisperer 也是才发布不久,通过测试我也想邀请大家去进行实际测试,可以对使用 CodeWhisperer 遇到的问题大家一起相互讨论,一起促进这个编程助手迭代和完善~
评论