AI大言語モデル(LLM)搭動く、コマンドラインの生産性向上ツールです。このコマンドラインツールは、シェルコマンド、コードスニペット、ドキュメントの効率的な生成を提供し、外部リソース(Google検索など)の必要性を排除します。Linux、macOS、Windows に対応しており、PowerShell、CMD、Bash、Zsh などすべての主要なシェルと互換性があります。
main_h264_high_bitrate.mov
pip install shell-gptデフォルトでは、ShellGPT は OpenAI の API と GPT-4 モデルを使用します。API キーが必要です。ここで生成できます。キーの入力を求められ、その後 ~/.config/shell_gpt/.sgptrc に保存されます。OpenAI API は有料サービスです。詳しくは OpenAI の価格情報を参照してください。
ヒント
または、オープンソースモデルをローカルで無料で実行することができます。これには Ollama などのような独自の LLM バックエンドの設定が必要です。ShellGPT を Ollama で動作させるには、この詳細なガイドに従ってください。
❗️ShellGPT はローカルモデルに対して最適化されていないため、期待通りに動作しない場合があります。
ShellGPT は、情報を素早く分析・取得するために設計されています。技術設定から一般知識まで、さまざまなシンプルなリクエストに対応できます。
sgpt "What is the fibonacci sequence"
# -> The Fibonacci sequence is a series of numbers where each number ...ShellGPT はプロンプトを標準入力とコマンドライン引数の両方から受け入れます。ターミナルを通じて入力をパイプするか、直接引数として指定するか、どちらを選んでも sgpt に対応できます。例えば、diff に基づいて Git のコミットメッセージを簡単に生成できます:
git diff | sgpt "Generate git commit message, for my changes"
# -> Added main feature details into README.mdプロンプトと一緒に標準入力を使用して、さまざまなソースからログを分析できます。例えば、ログを素早く分析し、エラーを特定して、考えられる解決策の提案を得ることができます:
docker logs -n 20 my_app | sgpt "check logs, find errors, provide possible solutions"Error Detected: Connection timeout at line 7.
Possible Solution: Check network connectivity and firewall settings.
Error Detected: Memory allocation failed at line 12.
Possible Solution: Consider increasing memory allocation or optimizing application memory usage.
入力を渡すために、あらゆる種類のリダイレクト演算子も使用できます:
sgpt "summarise" < document.txt
# -> The document discusses the impact...
sgpt << EOF
What is the best way to lear Golang?
Provide simple hello world example.
EOF
# -> The best way to learn Golang...
sgpt <<< "What is the best way to learn shell redirects?"
# -> The best way to learn shell redirects is through...find などの一般的なシェルコマンドを忘れてしまい、オンラインで構文を調べる必要があったことがありませんか?--shell オプション、またはショートカット -s を使用すれば、ターミナル内で必要なコマンドをすぐに生成・実行できます。
sgpt --shell "find all json files in current folder"
# -> find . -type f -name "*.json"
# -> [E]xecute, [D]escribe, [A]bort: eShell GPT はあなたが使用している OS と $SHELL を認識し、あなたのシステム固有のシェルコマンドを提供します。例えば、sgpt にシステムを更新するよう依頼すると、あなたの OS に基づいたコマンドが返されます。macOS を使用した例を以下に示します:
sgpt -s "update my system"
# -> sudo softwareupdate -i -a
# -> [E]xecute, [D]escribe, [A]bort: e同じプロンプトを Ubuntu で使用すると、異なる提案が生成されます:
sgpt -s "update my system"
# -> sudo apt update && sudo apt upgrade -y
# -> [E]xecute, [D]escribe, [A]bort: eDocker で試してみましょう:
sgpt -s "start nginx container, mount ./index.html"
# -> docker run -d -p 80:80 -v $(pwd)/index.html:/usr/share/nginx/html/index.html nginx
# -> [E]xecute, [D]escribe, [A]bort: eパイプを使用して入力を sgpt に渡し、シェルコマンドを生成することもできます:
sgpt -s "POST localhost with" < data.json
# -> curl -X POST -H "Content-Type: application/json" -d '{"a": 1, "b": 2}' http://localhost
# -> [E]xecute, [D]escribe, [A]bort: eプロンプトに追加のシェルマジックを適用し、この例では ffmpeg にファイル名を渡しています:
ls
# -> 1.mp4 2.mp4 3.mp4
sgpt -s "ffmpeg combine $(ls -m) into one video file without audio."
# -> ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex "[0:v] [1:v] [2:v] concat=n=3:v=1 [v]" -map "[v]" out.mp4
# -> [E]xecute, [D]escribe, [A]bort: e生成されたシェルコマンドをパイプを使用して渡したい場合は、--no-interaction オプションを使用できます。これはインタラクティブモードを無効にし、生成されたコマンドを stdout に出力します。この例では pbcopy を使用して、生成されたコマンドをクリップボードにコピーしています:
sgpt -s "find all json files in current folder" --no-interaction | pbcopyこれは 非常に便利な機能 で、sgpt とプロンプト・引数を入力する必要なく、ターミナル内で sgpt シェル補完を直接使用できます。シェル統合により、Bash と ZSH シェルの両方でサポートされているホットキーを使用してターミナルで ShellGPT を使用できるようになります。この機能は sgpt 補完をターミナルバッファ(入力行)に直接挿入し、提案されたコマンドを即座に編集できるようにします。
Shell_GPT_Integration.mp4
シェル統合をインストールするには、sgpt --install-integration を実行し、ターミナルを再起動して変更を反映してください。これにより、.bashrc または .zshrc ファイルに数行が追加されます。その後、Ctrl+l(デフォルト)を使用して ShellGPT を呼び出すことができます。Ctrl+l を押すと、現在の入力行(バッファ)が提案されたコマンドに置き換わります。その後、それを編集して Enter を押して実行できます。
--code または -c パラメータを使用することで、純粋なコード出力を特別にリクエストできます。例えば以下のようにします:
sgpt --code "solve fizz buzz problem using python"for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)有効な Python コードなので、出力をファイルにリダイレクトできます:
sgpt --code "solve classic fizz buzz problem using Python" > fizz_buzz.py
python fizz_buzz.py
# 1
# 2
# Fizz
# 4
# Buzz
# ...また、パイプを使用して入力を渡すこともできます:
cat fizz_buzz.py | sgpt --code "Generate comments for each line of my code"# Loop through numbers 1 to 100
for i in range(1, 101):
# Check if number is divisible by both 3 and 5
if i % 3 == 0 and i % 5 == 0:
# Print "FizzBuzz" if number is divisible by both 3 and 5
print("FizzBuzz")
# Check if number is divisible by 3
elif i % 3 == 0:
# Print "Fizz" if number is divisible by 3
print("Fizz")
# Check if number is divisible by 5
elif i % 5 == 0:
# Print "Buzz" if number is divisible by 5
print("Buzz")
# If number is not divisible by 3 or 5, print the number itself
else:
print(i)会話を保持し再呼び出しすることは重要です。sgpt は、LLM 完了要求ごとに会話型ダイアログを作成します。ダイアログは 1 つずつ (チャットモード) または対話的に REPL ループ (REPL モード) で展開されます。両方の方法は同じ基礎オブジェクト (chat session と呼ばれます) に依存しています。セッションは 設定可能な CHAT_CACHE_PATH に格納されています。
会話を開始するには、--chat オプションに続いて一意のセッション名とプロンプトを使用します。
sgpt --chat conversation_1 "please remember my favorite number: 4"
# -> I will remember that your favorite number is 4.
sgpt --chat conversation_1 "what would be my favorite number + 4?"
# -> Your favorite number is 4, so if we add 4 to it, the result would be 8.チャットセッションを使用して、追加の詳細情報を提供することで GPT の提案を段階的に改善できます。--code または --shell オプションを使用して --chat を開始することが可能です。
sgpt --chat conversation_2 --code "make a request to localhost using python"import requests
response = requests.get('http://localhost')
print(response.text)LLM にリクエストにキャッシングを追加するよう要求しましょう。
sgpt --chat conversation_2 --code "add caching"import requests
from cachecontrol import CacheControl
sess = requests.session()
cached_sess = CacheControl(sess)
response = cached_sess.get('http://localhost')
print(response.text)シェルコマンドにも同じことが当てはまります。
sgpt --chat conversation_3 --shell "what is in current folder"
# -> ls
sgpt --chat conversation_3 "Sort by name"
# -> ls | sort
sgpt --chat conversation_3 "Concatenate them using FFMPEG"
# -> ffmpeg -i "concat:$(ls | sort | tr '\n' '|')" -codec copy output.mp4
sgpt --chat conversation_3 "Convert the resulting file into an MP3"
# -> ffmpeg -i output.mp4 -vn -acodec libmp3lame -ac 2 -ab 160k -ar 48000 final_output.mp3いずれかの会話モードからすべてのセッションをリスト表示するには、--list-chats または -lc オプションを使用します。
sgpt --list-chats
# .../shell_gpt/chat_cache/conversation_1
# .../shell_gpt/chat_cache/conversation_2特定の会話に関連するすべてのメッセージを表示するには、--show-chat オプションに続いてセッション名を使用します。
sgpt --show-chat conversation_1
# user: please remember my favorite number: 4
# assistant: I will remember that your favorite number is 4.
# user: what would be my favorite number + 4?
# assistant: Your favorite number is 4, so if we add 4 to it, the result would be 8.