手把手教你用 Amazon Q Developer 免费畅玩 Claude 3.7 Sonnet AI 编程

前言

如今,AI辅助编程工具种类繁多。我平时常用的有 Cline、Cursor、Windsurf,以及 Amazon Q Developer。可能很多人对 Amazon Q Developer 还不太熟悉,实际上它据说是基于 Claude Sonnet 3.7 构建的,在编程方面表现非常出色。

我平时经常使用 Amazon Q 和Amazon Q for Command Line,因此一直非常期待它能支持中文交互。没想到前几天的更新就带来了这个功能,实在是个惊喜!

A lightning fast, new agentic coding experience within the Amazon Q Developer CLI | AWS DevOps & Developer Productivity Bloghttps://aws.amazon.com/jp/blogs/devops/introducing-the-enhanced-command-line-interface-in-amazon-q-developer/引用一部分文章中的内容

While Amazon Q Developer’s CLI capabilities are helpful, I miss the power of the agents I have access to in my IDE. Earlier today, Amazon Q Developer announced an enhanced CLI agent within the Amazon Q CLI. Amazon Q Developer, and the new agent is powered by Amazon Bedrock, as a result, the CLI has the power of Claude 3.7 Sonnet step-by-step reasoning.

安装方法

安装Amazon Q

本文以macOS为例进行安装。

如果你正在使用macOS,可以在Homebrew上安装Amazon Q应用程序。

$ brew install amazon-q
==> Auto-updating Homebrew...
Adjust how often this is run with HOMEBREW_AUTO_UPDATE_SECS or disable with
HOMEBREW_NO_AUTO_UPDATE. Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Downloading https://desktop-release.q.us-east-1.amazonaws.com/1.7.0/Amazon%20Q.dmg
####################################################################################################################################################### 100.0%
==> Installing Cask amazon-q
==> Moving App 'Amazon Q.app' to '/Applications/Amazon Q.app'
🍺  amazon-q was successfully installed!

这一步已经安装好了 Amazon Q 应用,接下来要进行使用 Amazon Q 所需的认证信息等设置。

获取 AWS Builder ID

AWS Builder ID,是一种非常方便的账号,即使没有 AWS 账户,也可以使用 Amazon Q Developer 和 AWS Skill Builder,为了免费使用Amazon Q我们要先进行注册。

AWS | Community | Builder IDAWS Community Builder IDhttps://community.aws/builderid/?trk=fdb19d08-6b96-423b-bb89-e13508f57dc9&sc_channel=el跳转到的新页面中会出现这个画面,点击 “Sign up with Builder ID”,然后按步骤填写邮箱等信息进行注册。

按照页面的指引就可以完成 Builder ID 的设置。

Amazon Q 应用的设置

现在登录用的 ID 已经创建完成,接下来开始设置 Amazon Q 应用。
首先需要选择登录方式。虽然首页的对话框看起来是这样,但在下方可以看到一个小字的选项:“Use for Free with Builder ID”(使用 Builder ID 免费体验)。

点击那个 “Use for Free with Builder ID” 的链接,用刚才创建的 Builder ID 登录后,会跳转到如下页面。

这样一来,Amazon Q 应用就处于已登录状态,至此设置就告一段落啦。

终端的设置

在这个状态下,如果你在终端应用中启动一个新的 Shell,应该会看到类似下图的显示内容。(具体显示可能因所使用的终端应用不同而有所差异) 

🚀 Amazon Q supports Wezterm!

Enable integrations with Wezterm by running:
  q integrations install input-method


Amazon Q now supports AI-powered inline completions!

To disable run: q inline disable

我使用的是 WezTerm,所以显示内容是这样的。按照提示所说的,接下来只需使用 q 命令安装所需的组件即可。

$ q integrations install input-method
Installed!
You must restart your terminal to finish installing the input method.

点击了 “Allow” 按钮并完成了授权。

 

$ q chat
Hi, I'm Amazon Q. Ask me anything.

Things to try
• Fix the build failures in this project.
• List my s3 buckets in us-west-2.
• Write unit tests for my application.
• Help me understand my git status

/acceptall    Toggles acceptance prompting for the session.
/help         Show the help dialogue
/quit         Quit the application

>

这样一来,Amazon Q for Command Line 就顺利启动啦!接下来来试试看提问吧。

据官方的博客介绍,它是基于 Claude 3.7 Sonnet 进行推理的,我们就来试试看用中文提问看看效果如何。

编写一个 Lambda 函数,用于检测添加到 S3 存储桶 my-test-bucket 中的 WAV 音频文件,并将其发送到 MediaConvert 转换为 mp3 格式。

我们来创建一个使用 MediaConvert 将上传到 S3 存储桶的 WAV 文件转换为 MP3 的 Lambda 函数。

首先,确认所需的 AWS 资源和 Lambda 函数结构。

Lambda 函数需要有读取 S3 存储桶中对象的权限
使用 MediaConvert 的权限
保存转换后文件的目标位置
那么我们来创建 Lambda 函数吧。

import json
import boto3
import os
import urllib.parse

def lambda_handler(event, context):
    # 从 S3 事件中获取存储桶名称和对象键
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

    # 检查文件扩展名是否为 WAV
    if not key.lower().endswith('.wav'):
        print(f"文件 {key} 不是 WAV 格式,跳过处理")
        return {
            'statusCode': 200,
            'body': json.dumps('不是 WAV 文件,已跳过')
        }
...(略)...
    except Exception as e:
        print(f"发生错误: {str(e)}")
        return {
            'statusCode': 500,
            'body': json.dumps(f'错误: {str(e)}')
        }

要使用此函数,需要进行以下设置:

1.创建 Lambda 函数

Write to filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Path: /Users/yoshiyyy/q-cli-test/wav_to_mp3_converter.py

Contents:


   1 import json
   2 import boto3
   3 import os
   4 import urllib.parse
   5
   6 def lambda_handler(event, context):
...(略)...
  95             'statusCode': 500,
  96             'body': json.dumps(f'错误: {str(e)}')
  97         }

Enter y to run this tool, otherwise continue chatting.

>

它居然用很自然的中文给出了像模像样的回答!而且还真的像个智能代理一样,主动帮我生成文件并尝试写入!那就继续试试看吧。

> y

Writing to filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Creating: /Users/yoshiyyy/q-cli-test/wav_to_mp3_converter.py

🟢 Completed in 0.4s

2. 环境变量的设置:  
   • `MEDIACONVERT_ROLE_ARN`:供 MediaConvert 访问 S3 所需的 IAM 角色的 ARN

3. 为 Lambda 函数配置所需的 IAM 策略:


json
   {
       "Version": "2012-10-17",
       "Statement": [
           {
               "Effect": "Allow",
               "Action": [
                   "s3:GetObject",
                   "s3:PutObject"
               ],
               "Resource": [
                   "arn:aws:s3:::my-test-bucket/*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": [
                   "mediaconvert:CreateJob",
                   "mediaconvert:DescribeEndpoints"
               ],
               "Resource": "*"
           },
           {
               "Effect": "Allow",
               "Action": "iam:PassRole",
               "Resource": "arn:aws:iam::*:role/MediaConvertRole"
           }
       ]
   }

4. S3 存储桶的事件通知设置:  
   • 将 S3 存储桶 `my-test-bucket` 配置为在有新对象创建时触发 Lambda 函数

这样一来,每当有新的 WAV 文件上传到 S3 存储桶时,就会自动使用 MediaConvert 转换为 MP3 格式。转换后的文件将保存在同一个存储桶下的 `converted/` 前缀目录中。

>

文件也成功生成了!

$ ls ~/q-cli-test/*.py
/Users/yoshiyyy/q-cli-test/wav_to_mp3_converter.py

这段代码我已经实际运行过了,成功完成了文件的转换处理,说明代码和相关设置是可行的。整体来看,Amazon Q 给出的回答确实非常不错!

最后

AI Agent 功能是 Amazon Q for Command Line 的新特性,但除此之外,它也提供了像在 zsh 中模糊补全这样的实用功能,非常方便。如果你也想试试的话,强烈推荐先注册一个 Builder ID 来体验一下!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值