🐟

LLM搭載の次世代シェルButterfishを使ってみた

2023/08/16に公開

Butterfishとは

Butterfishとは Peter Bakkum さんが開発しているツールで、コマンドラインからOpenAI APIを実行するツールの一つです。GitHub で公開されています。

ターミナルからOpenAI APIを叩くためのツールは様々ありますが、Butterfish の特徴は以下の三点です。

  1. LLMによるコマンド補完・生成
  2. ターミナルからコマンド履歴(エラー含む)についての質問が可能
  3. ローカルファイルの要約

特に2のこれまでのコマンド実行の結果を含めて質問できるのが便利で、エラー内容に対する説明を聞くこともできます。

architecture

Butterfish の仕組みは至ってシンプルで Butterfish Shell から Open AI API に接続し、その結果を表示するというものです。そのため、Open AI API の利用料金がかかりますのでご注意ください。

インストール

Butterfish のインストールは以下のコマンドで行えます。

brew install bakks/bakks/butterfish

以下のコマンドを打って何か返答が返ってきたら成功です

butterfish prompt "Is this thing working?"

初回実行時に Open AI API キーの入力を求められます。 sk- から始まるキーをターミナルに貼り付ければOKです。

Open AI API のキーは こちら から取得できます。

シェルモードを使ってみる

Butterfish の便利さを特に象徴しているのが Shell モードです。以下のコマンドで Shell モードに入ることができます。

butterfish shell

ターミナルからChat GPTを使って質問する

Butterfish shell では大文字から始まるコマンドを入力すると、そのコマンドに対して Chat GPT が応答してくれます。以下の例では What is the weather like today? と入力すると、Chat GPT がその質問に対して回答を返してくれます。

🐠 How is the whether today?
I'm sorry, but as a Unix shell assistant, I don't have the ability to provide weather information. However, you can use various commands to check the weather, such as "curl" to retrieve weather data from an API, or "wget" to download weather information from a website.

curl か wget を使うように言われたので curl を使って東京の天気を取得する方法を聞いてみましょう

Get today's whether in Tokyo by using curl.
Certainly! To get today's weather in Tokyo using curl, you can use the following command:


curl wttr.in/Tokyo


This will fetch the weather information for Tokyo and display it in your terminal.

実際にこのコマンドを実行してみると以下のように天気が表示されます。

🐠 curl wttr.in/Tokyo                               Weather report: Tokyo                                          

   _`/"".-.     Light rain shower
    ,\_(   ).   +28(32) °C     
     /(___(__)21 km/h      
       ‘ ‘ ‘ ‘  10 km          
      ‘ ‘ ‘ ‘   0.7 mm         

というわけでターミナルから一歩も出ずにChat GPTを使って天気を取得することができました。

Chat GPT 本体で天気を聞いても答えてくれないのは定番ですが、Butterfish ですとコマンドラインツールなのでこのようにコマンドを使った解決方法を教えてくれるのが強みです。

エラー内容に対する説明

同じ原理でエラー内容に対する説明を聞くこともできます。例えば embulk をインストールしている最中に以下のようなエラーが出たとします。

🐠 embulk example ./try1                                    


================================== [ NOTICE ] ==================================
 Embulk will not be executable as a single command, such as 'embulk run'.
 It will happen at some point in v0.11.*.

 Get ready for the removal by running Embulk with your own 'java' command line.
 Running Embulk with your own 'java' command line has already been available.

 For instance in Java 1.8 :
  java -XX:+AggressiveOpts -XX:+UseConcMarkSweepGC -jar embulk-X.Y.Z.jar run ...
  java -XX:+AggressiveOpts -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Xverify:none -jar embulk-X.Y.Z.jar guess ...

 See https://github.com/embulk/embulk/issues/1496 for the details.
================================================================================

[ERROR] The Java version is not recognized by the self-executable single 'embulk' command.
[ERROR]   The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
[ERROR]
[ERROR] Build your own 'java' command line instead of running Embulk as a single command.
[ERROR]
[ERROR] See https://github.com/embulk/embulk/issues/1496 for the details.

私はとてつもなく面倒くさがりなので、エラーの文章が長いと読む気力がなくなってしまいます。でもそんな時こそ Butterfish です。

🐠 How to fix the error?
The error message suggests that Embulk requires Java to be installed on your system. To fix the error, you can follow these steps:

1. Verify if Java is installed on your system by running the command `java -version`. If Java is not installed, you can download and install it from the official Java website: http://www.java.com.

2. After installing Java, you need to update the PATH environment variable to include the Embulk binary directory. Run the following command to add it to your `~/.bashrc` file:
   
   echo 'export PATH="$HOME/.embulk/bin:$PATH"' >> ~/.bashrc
   

3. Reload the updated `~/.bashrc` file by running:
   
   source ~/.bashrc
   

4. Try running the Embulk command again to see if the error is resolved.

If you still encounter issues, make sure you have the correct version of Java installed and that it is properly configured on your system.

エラーが出た場合は続けて How to fix the error? と聞くことでエラー内容に対する説明を聞くことができます。

これまではエラー内容をググって解決するか、そのままChat GPT に聞いてみるか、という感じでしたが、Butterfish では質問するだけで自動的にエラー内容に対する説明を聞くことができます。

個人的にはこの機能が一番使い勝手が良いと思います。

GOAL モード

まだ実験的な機能ですが、GOAL モードというのがあります。これはゴールを設定すると、そのゴールに向かって Butterfish がコマンドの生成・実行を自動的に行なってくれるというものです。

例えばカレントディレクトリからマークダウンファイルをカウントしたいとします。実際にそれを行うコマンドは以下のようになります。

find . -type f -name "*.md" | wc -l

これを Butterfish にやらせてみましょう。GOALモードを使うには質問(命令)の最初に!マークをつけます

🐠 !Count the number of markdown files in the current directory recursively.

エンターキーを押すとコマンドの生成が始まり、次のプロンプトに生成されたコマンドが入力された状態で表示されます。

find . -type f -name '*.md' | wc -l

コマンドの実行を了承する場合はエンターキーを押します。するとコマンドが実行され、その結果が表示されます。

🟦 find . -type f -name '*.md' | wc -l
       5
finish({
  "success": true
})
Exited goal mode with SUCCESS.

!の代わりに !! を使うとコマンドの実行までを自動的にやってくれますが、任意のコマンドが実行されてしまう可能性があるため取り扱いには注意してください。

その他の機能

その他の機能は Githubまたは butterfish help で確認できます。
コマンドラインからChat GPTを使いたい!という人にはもってこいのシェルだと思います。ぜひ使ってみてください。

Discussion