5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazon Qを使ってCLIの補完/自然言語でshellコマンド作成

Last updated at Posted at 2025-02-16

これは何?

Amazon Q DeveloperにCLIツールがあり,これを使うことでCLIでのコマンド入力を効率化できます。
本記事では,Amazon Q Developer CLIツールについて調べたことをまとめます。
以下は何ができるかのイメージです。

コマンドの補完
image.png

Inline: 入力途中のものから予測してくれる

image.png
(自分の環境だと灰色での補完候補が見えなかったので公式の画像をお借りしました)

q chatでチャットを使う
image.png

translateで自然言語からbashスクリプトを作成する

image.png


Amazon Q Developerを使い始める前にチェックしたこと

料金

Amazon Q Developer公式によるとDeveloper Free Tierがあるので無料で試せます。
Proライセンスだとエンタープライズアクセスコントロールなど企業で使うなら欲しい機能がついてそうですが,自分はFree Tierを一旦使っています。

入力データを学習に使用させない設定

Amazon Q Developerのサービス改善に記載があります。

Amazon Q Developer Pro または Amazon Q Business のコンテンツは、サービスの改善には使用されません。

とあるので有料ユーザは設定不要です。

Amazon Q Developer 無料利用枠でサービス改善のためのコンテンツ使用からオプトアウトする方法は、Amazon Q を利用する環境によって異なります。

無料ユーザで気になる人は自分でオプトアウトの設定が必要です。
今回の環境であれば,インストール後にqコマンドで立ち上がる画面の設定からオフにできます。

image.png


環境構築

インストール

コマンドライン用のAmazon Qをインストールするに記載があるのでそのままやるだけです。

macOS,Linux(AppImage),Ubuntuで使用可能です。

  • 自分はUbuntuを使っているので以下を実行しました。
curl --proto '=https' --tlsv1.2 -sSf https://desktop-release.q.us-east-1.amazonaws.com/latest/amazon-q.deb -o amazon-q.deb
sudo apt install -y ./amazon-q.deb
q

(自分は再起動が求められたので再起動したら動いた)

qを実行するとAmazon Builder IDでのサインインが求められます。

Amazobn Builder IDはAWSのアカウントとは別なので注意

既にCLIでの補完機能を使っている場合干渉するかも

自分はoh-my-zshのカスタムプラグインである,zsh-autocompleteを使用しているのですが,これが干渉してしまいAmazon Qによる補完がすぐに消えてしまう事象が発生しました。
2つ補完がでてもあれなので,zsh-autocompleteを一旦止めました。


各機能を試してみる!

コマンド補完

image.png

デフォルトでは上下矢印キーで補完候補の切り替えが行えます。

試しにネットワークをオフにしても動いたので出先でも使えて安心です。

自分は使いづらかったのでtabキーにより補完候補を一つ下に進むように設定しました。

image.png

shift tabで補完候補を一つ上にしたかったのですが,自分の環境だとうまく動きませんでした。

また,インストールされているコマンドのオプションとは別に,現在入力中にコマンドから補完候補を推測してくれる機能もあるようです。(こっちはネットワークをオフにすると動かなくなった)
https://docs.aws.amazon.com/ja_jp/amazonq/latest/qdeveloper-ug/command-line-inline.html
image.png
image.png

Inline

現在ターミナルに入力しているコマンドから次の候補を推測して補完候補を出してくれます(2025年2月16日現在ではzshのみ)。

デフォルトでは入力中に→を入力することでコマンドの続きを書いてくれます(イメージ的にはGitHub Copilotの補完に近いかも)。

image.png
(自分の環境だと灰色での補完候補が見えなかったので公式の画像をお借りしました)

Chat

q chatコマンドで起動するチャット機能です。
コンテキスト変数を使ったチャットができます。

  • @git: 現在のブランチ、ステージングされた変更とステージングされていない変更、コミット履歴など、git リポジトリのステータスに関する情報を渡すことができます。
  • @env: Amazon Q にローカルシェル環境変数を提供できます。これは、開発のセットアップと設定を理解するのに役立ちます。
  • @history: 最近のシェルコマンド履歴を Amazon Q と共有して、実行したアクションと作業中のコンテキストに関するインサイトを得ることができます。
    コマンドラインでの Amazon Q とのチャット

以下は自分のリポジトリに対して使い方を聞いて見た例です。

q chat                                  2

> @history how to start this repository
Using shell history

Based on the command history, I can see you're in the /home/tomita/GateKeeper 
directory and tried to run docker compose up which was interrupted (exit code 
130, typically indicating a Ctrl+C interruption).

To start this repository, here are the steps you should follow:

1. First, make sure you're in the correct directory (which you already are):
bash
cd GateKeeper


2. Run Docker Compose to start the services:
bash
docker compose up


If you want to run it in detached mode (in the background), you can use:
bash
docker compose up -d


If you encounter any issues, you might want to try:

1. Rebuilding the containers:
bash
docker compose up --build


2. Or if you need to start fresh:
bash
# Stop and remove containers, networks
docker compose down

# And then start again
docker compose up


3. To view logs when running in detached mode:
bash
docker compose logs -f


If you're encountering any specific errors while starting up, please share them 
and I can provide more targeted assistance

試した感じですが,遅くても1~2秒くらいでは応答が帰ってくるのでGitHub Copilot Chatに比べてだいぶ早いなと感じます。
TODO: 精度などもそのうち検証する。

2025年2月16日現在では英語でのチャットしかできないので,今後に期待。

q chat                   127

Hi, I'm Amazon Q. I can answer questions about your shell and CLI tools!
You can include additional context by adding the following to your prompt:

@history to pass your shell history
@git to pass information about your current git repository
@env to pass your shell environment

> こんにちは

I can only respond to messages in English. Please rephrase your message in 
English and I'll be happy to help

Translate

自然言語からbashスクリプトを作成する機能です。
個人的にはこの機能が一番気に入っています。

  • ターミナル上からプロンプトを入力できる(q translateするだけ)
  • 作成したコマンドをレビュー後にそのまま実行できる
    • 実行前に手直しすることも可能。
  • コマンド実行後にq translateから抜けてくれる(地味に嬉しい)

image.png


感想

ターミナルを使うのが自分は結構好きなので,ターミナルとブラウザの往復をすることがなくLLMによる恩恵を受けられるのがすごく便利だなと感じました。
また,通常の補完のみであればオフラインでも使用できる点も嬉しいです。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?