通过 Amazon CodeWhisperer 和生成式 AI 开发应用程序的指南

Python
机器学习
SDK
Amazon CodeWhisperer
生成式人工智能
0
0
本指南旨在帮助用户熟悉由[机器学习](https://aws.amazon.com/cn/machine-learning/?trk=cndc-detail)提供技术支持的代码生成器,从而促进并提高用户的开发效率。本指南介绍了 CodeWhisperer 如何为各种开发相关用途生成代码,例如单元测试、创建和集成亚马逊云科技资源。 ### 启动 CodeWhisperer 使用以下文档,为开发环境配置 CodeWhisperer:\ • 设置 - 首次使用 CodeWhisperer 前所需的步骤。\ • 开始使用 - 如何通过每个支持的集成开发环境 (IDE) 设置 CodeWhisperer。 ### 用例:自动生成单元测试 (Python) CodeWhisperer 可减轻编写重复单元测试代码的任务。根据自然语言注释,CodeWhisperer 可自动建议与实施代码匹配的单元测试代码。在以下片段中,您将看到 CodeWhisperer 如何帮助开发人员自动生成单元测试,从而提高代码覆盖率。 1、在 Visual Studio Code 等 IDE 中,打开空目录。对于本用例,我们已在 Visual Studio Code 中使用 Python。 | 备注:CodeWhisperer 使用人工智能 (AI),提供具有非确定性的代码建议。CodeWhisperer 在开发会话中生成的代码建议可能有所不同。| | | --- | --- 2、(可选)在终端,新建 Python 虚拟环境: ``` python3 -m venv .venv source .venv/bin/activate ``` 3、安装基本测试库:\ `pip install pytest pytest-cov` 4、创建名为 calculator.py 的新文件 5、在文件开头插入以下注释,以便开始构建简单的计算器类,然后选择 Enter: `# example Python class for a simple calculator` 然后,CodeWhisperer 将开始提供建议,从而生成新代码。 6、要接受这些建议,选择 **Tab**。 ![插图1.gif](https://dev-media.amazoncloud.cn/b36115b07f6d478fb38302de2316e07d_%E6%8F%92%E5%9B%BE1.gif "插图1.gif")\ 图 6 - 构建简单的计算器类 如果 CodeWhisperer 未自动提供建议,对于 Windows/Linux,可按 **Alt + C**,或对于 macOS,可按 **Option + C**,从而手动触发 CodeWhisperer。通过选择**向右**箭头键,查看其他建议。要查看之前的建议,选择**向左**箭头键。要拒绝建议,选择 **ESC** 或**退格/删除**键。 通过选择 **Enter** 键并接受 CodeWhisperer 建议(自动或手动),继续构建计算器类。CodeWhisperer 将为计算器类提供基本函数建议,例如 add()、subtract()、multiply() 和 divide()。也建议更高级的函数,例如 square()、cube() 和 square_root()。 ``` # example Python class for a simple calculator class Calculator: # add two numbers def add(self, a, b): return a + b # subtract two numbers def subtract(self, a, b): return a - b # multiply two numbers def multiply(self, a, b): return a * b # divide two numbers def divide(self, a, b): return a / b # square a number def square(self, a): return a * a # cube a number def cube(self, a): return a * a * a # square root a number def square_root(self, a): return a ** 0.5 # cube root a number def cube_root(self, a): return a ** (1/3) ``` def square_root(self, a): return a ** 0.5 **自动生成单元测试** 现在,我们运行一些测试,检查代码覆盖率。输入: ` pytest --cov=. ` 您可能会看到: ![插图2.png](https://dev-media.amazoncloud.cn/93314a94f34a43f092ce4f54d3231e86_%E6%8F%92%E5%9B%BE2.png "插图2.png")\ 图 7 - 显示“无测试运行” 没错,我们没有任何测试,自然也没有任何代码覆盖率!我们使用 CodeWhisperer 帮助自动生成单元测试,并提高代码覆盖率。 1. 创建名为 **test_calculator** 的新文件 2. 在文件开头插入以下代码和注释,以便开始为计算器类构建单元测试,然后选择 **Enter**: ``` import pytest from calculator import Calculator # fixture for calculator ``` ![插图3.gif](https://dev-media.amazoncloud.cn/74c9ae2f5ea841dd8ca836eb569399fa_%E6%8F%92%E5%9B%BE3.gif "插图3.gif")\ 图 8 - 为计算器类构建单元测试 3. 通过按 **Enter** 键并接受 CodeWhisperer 建议(自动或手动),继续构建计算器类。CodeWhisperer 将使用此前实施的类作为上下文,建议单元测试。 ``` import pytest from calculator import Calculator # fixture for calculator @pytest.fixture def calculator(): return Calculator() # unit test for multiply() def test_multiply(calculator): assert calculator.multiply(2, 3) == 6 # unit test for divide() def test_divide(calculator): assert calculator.divide(6, 3) == 2 with pytest.raises(ZeroDivisionError): calculator.divide(6, 0) # unit test for add() def test_add(calculator): assert calculator.add(2, 3) == 5 assert calculator.add(2, -3) == -1 assert calculator.add(0, 0) == 0 # unit test for subtract() def test_subtract(calculator): assert calculator.subtract(2, 3) == -1 assert calculator.subtract(2, -3) == 5 assert calculator.subtract(0, 0) == 0 # unit test for square() def test_square(calculator): assert calculator.square(2) == 4 assert calculator.square(0) == 0 with pytest.raises(TypeError): calculator.square("a") # unit test for cube() def test_cube(calculator): assert calculator.cube(2) == 8 assert calculator.cube(0) == 0 with pytest.raises(TypeError): calculator.cube("a")* ``` 我们尝试运行新实施的单元测试,检查代码覆盖率。输入: `pytest --cov=.` ![插图4.png](https://dev-media.amazoncloud.cn/f861c14d6914485c8d4ad047d940518d_%E6%8F%92%E5%9B%BE4.png "插图4.png")\ 图 9 - CodeWhisperer 自动生成单元测试方法 如您所见,CodeWhisperer 可自动生成单元测试方法(包括适当的断言值),从而提高代码覆盖率并缩短实施时间。 ### 用例:使用亚马逊云科技服务构建应用程序 通过跨 Amazon Elastic Compute (Amazon EC2)、Amazon Lambda 和 [Amazon Simple Storage Service](https://aws.amazon.com/cn/s3/?trk=cndc-detail) ([Amazon S3](https://aws.amazon.com/cn/s3/?trk=cndc-detail)) 等大多数备受欢迎的服务为 Amazon API 提供代码建议,构建者可加速应用程序开发流程。CodeWhisperer 可分析并建议专为提供的上下文定制的亚马逊云科技资源。 1、在 Visual Studio Code 或 JetBrains 等 IDE 中,打开空目录。对于本用例,我们在 JetBrains PyCharm IDE 中使用 Python。 | 备注:CodeWhisperer 使用人工智能,提供具有非确定性的代码建议。CodeWhisperer 在开发会话中生成的代码建议可能有所不同。 | | | --- | --- | 2、(可选)在终端,新建 Python 虚拟环境: ``` python3 -m venv .venv source .venv/bin/activate ``` 3、安装基本软件开发套件 (SDK) 库: `pip install boto3` 4、打开全新或现有 Python 文件,并尝试下方部分示例。 #### 示例 ##### 生成自定义 IAM 策略 ``` # create an IAM policy with read and write access to S3 ``` ![插图5.gif](https://dev-media.amazoncloud.cn/ae9358c17b67472a8c72222beb73a385_%E6%8F%92%E5%9B%BE5.gif "插图5.gif")\ 图 10 - 创建对 [Amazon S3](https://aws.amazon.com/cn/s3/?trk=cndc-detail) 具有读写访问权限的 IAM 策略 ##### 从 SDK 对结果分页 ``` # retrieve and iterate through paginated IAM users in account ``` ![插图6.gif](https://dev-media.amazoncloud.cn/533493b9ddcb405a84a2727a18d91293_%E6%8F%92%E5%9B%BE6.gif "插图6.gif")\ 图 11 - 检索和遍历已分页的 IAM 用户 ##### 创建已启用加密的资源 ``` # create bucket with server-side encryption enabled ``` ![插图7.gif](https://dev-media.amazoncloud.cn/aa7ea9cfd3e14d64bfc276bf094a46a1_%E6%8F%92%E5%9B%BE7.gif "插图7.gif")\ 图 12 - 创建具有服务器端加密的存储桶 ##### 创建数据库模式 ``` # create DynamoDB table for users using email as primary key and date created as sort key ``` ![插图8.gif](https://dev-media.amazoncloud.cn/9908f95983fd4c40bacb215e16a1e0e1_%E6%8F%92%E5%9B%BE8.gif "插图8.gif")\ 图 13 - 创建 [Amazon DynamoDB](https://aws.amazon.com/cn/dynamodb/?trk=cndc-detail) 表
目录
亚马逊云科技解决方案 基于行业客户应用场景及技术领域的解决方案
联系亚马逊云科技专家
亚马逊云科技解决方案
基于行业客户应用场景及技术领域的解决方案
联系专家
0
目录
关闭