SGPT(別名Shell-GPT )は、端末から直接OpenAIモデルとのシームレスなインタラクション用に設計された強力なコマンドラインインターフェイス(CLI)ツールです。簡単にクエリを実行し、シェルコマンドまたはコードを生成し、テキストから画像を作成して、単純なコマンドを使用して作成します。この強力でユーザーフレンドリーなCLIツールで、ワークフローを合理化し、生産性を向上させます。
SGPTの助けを借りて開発されました。
これはGO実装です。元のPython実装については、Shell-Gptにアクセスしてください。問題を報告するときは、これに留意してください。
注:現在、V3の重いリファクタリング中ですが、V2は依然として維持されています。
これらの汎用性の高い機能を提供することにより、SGPTは全体的な生産性を高め、ワークフローを合理化し、複雑なタスクを簡素化する強力なツールとして機能します。
SGPTはUbuntu LTSリリースでテストされており、次のLinux分布と互換性があると予想されます。
インストールするには、リリースページから最新リリースをダウンロードし、配布に固有のパッケージマネージャーを使用します。
Package ManagerとしてHomeBrewを持つユーザーの場合、ターミナルで次のコマンドを実行します。
brew install tbckr/tap/sgptScoopをパッケージマネージャーとして持っているユーザーの場合、PowerShellでこれらのコマンドを実行します。
scoop bucket add tbckr https://github.com/tbckr/scoop-bucket.git
scoop install tbckr/sgptGOTタグに基づいてGOでSGPTをインストールするには、このコマンドを使用します。
go install github.com/tbckr/sgpt/v2/cmd/sgpt@latestDockerでSGPTを実行するには、次のコマンドを使用して最新の画像をプルします。
docker pull ghcr.io/tbckr/sgpt:latestDockerでSGPTを使用する方法の例は、こちらをご覧ください。
AnsibleでSGPTをインストールするには、次のAnsible Playbookをベースとして使用し、それに応じて適応できます。
---
- hosts : all
tasks :
- name : Get latest sgpt release
uri :
url : " https://api.github.com/repos/tbckr/sgpt/releases/latest "
return_content : yes
register : sgpt_release
- name : Set latest version of sgpt
set_fact :
sgpt_latest_version : " {{ sgpt_release.json.tag_name }} "
- name : Install sgpt for debian based, amd64 systems
ansible.builtin.apt :
deb : https://github.com/tbckr/sgpt/releases/download/{{ sgpt_latest_version }}/sgpt_{{ sgpt_latest_version[1:] }}_amd64.deb
allow_unauthenticated : trueプレイブックは、次のコマンドで実行できます。
ansible-playbook -i < inventory > < playbook > .ymlプレイブックの最新バージョンはこちらをご覧ください。
他のプラットフォームについては、GitHubリリースページにアクセスして、システムに適した最新リリースをダウンロードしてください。
詳細な使用手順については、ドキュメントを参照してください。
OpenAI APIを使用するには、最初にAPIキーを取得する必要があります。
.bashrcまたは.zshrcファイルを更新して、次のエクスポートステートメントを含めるように、APIキーを値として追加します。 export OPENAI_API_KEY= " sk-... "OPENAI_API_KEY変数を含めるようにします。これらの手順を完了した後、SGPTツールを介してOpenAIモデルと対話するために使用できるOpenAI APIキーがあります。
注: APIキーは機密情報です。誰とも共有しないでください。
SGPTを使用すると、簡単な質問をして、有益な回答を受けることができます。例えば:
$ sgpt " mass of sun "
The mass of the sun is approximately 1.989 x 10^30 kilograms.また、パイプを使用してプロンプトをSGPTに渡すこともできます。
$ echo -n " mass of sun " | sgpt
The mass of the sun is approximately 1.989 x 10^30 kilograms.また、 stdin修飾子を指定し、プロンプトを指定することにより、パイプデータに別のプロンプトを追加することもできます。
$ echo " Say: Hello World! " | sgpt stdin ' Replace every "World" word with "ChatGPT" '
Hello ChatGPT !完了をコマンドラインにストリーミングする場合は、 --streamフラグを追加できます。これにより、生成されたコマンドラインに出力がストリーミングされます。
SGPTはさらに、GPT-4OおよびGPT-4 Vision APIの利用を促進します。 -iまたは--inputフラグを使用して入力画像を含め、URLとローカル画像の両方をサポートします。
$ sgpt -m " gpt-4o " -i pkg/fs/testdata/marvin.jpg " what can you see on the picture? "
The picture shows a robot with a large, round head and an expressive, downward-slanting triangular eye. The body of the robot is designed with a sleek, somewhat shiny, metallic structure and it is pointing with its right hand. The design appears to be humanoid with distinct arms, legs, and a segmented torso.
$ sgpt -m " gpt-4-vision-preview " -i " https://upload.wikimedia.org/wikipedia/en/c/cb/Marvin_%28HHGG%29.jpg " " what can you see on the picture? "
The image shows a figure resembling a robot with a humanoid form. It has aURLとローカル画像を組み合わせることもできます。
$ sgpt -m " gpt-4o " -i " https://upload.wikimedia.org/wikipedia/en/c/cb/Marvin_%28HHGG%29.jpg " -i pkg/fs/testdata/marvin.jpg " what is the difference between those two pictures "
The two pictures you provided appear to be identical. There are no visible differences between them. Both show the same character in the same pose with the same lighting and background.デフォルトモデルを構成ファイルに設定することにより、GPT-4OまたはGPT-4 Visionに設定することもできます。
重要: GPT-4OおよびGPT-4-Vision API統合は現在ベータ版であり、将来変化する可能性があります。
O1 APIのためにすでにホワイトリストに登録されている場合は、 -mフラグを使用してモデルを指定して使用できます。また、O1 APIによってサポートされていないため、出力をストリーミングしないように--stream=falseフラグを提供する必要があります(これは、構成ファイルを介してストリームオプションを提供した場合にのみ必要です)。
例:
$ sgpt -m " o1-preview " --stream=false " how many rs are in strawberry? "
There are three " r " s in the word " strawberry " .また、O1 APIをより簡単に使用するBashエイリアスを作成することもできます。たとえば、 .bashrcに次の行を追加します。
alias sgpt-o1= " sgpt -m " o1-preview " --stream=false "次に、次のようなエイリアスを使用できます。
$ sgpt-o1 " how many rs are in strawberry? "
There are three " r " s in the word " strawberry " .重要: O1 APIはペルソナをサポートしていません。
SGPTは、OpenAIモデルとのインタラクティブな会話を可能にするチャット機能を提供します。 --chatフラグを使用して、チャットセッションを開始および参照できます。
チャット機能により、OpenAIモデルとよりダイナミックで魅力的な方法でやり取りできるようになり、継続的な会話を通じて関連する応答、コード、またはシェルコマンドを簡単に取得できます。
以下の例は、よりターゲットを絞った結果のためにモデルの応答を微調整する方法を示しています。
ls-filesという名前のチャットセッションを開始し、モデルに「すべてのファイルディレクトリのリスト」を求めます。 $ sgpt sh --chat ls-files " list all files directory "
lsls-filesチャットセッション内の会話を継続し、「名前でソートする」を要求します。 $ sgpt sh --chat ls-files " sort by name "
ls | sortモデルは、適切なシェルコマンドls | sortを提供しますls | sort 、ディレクトリ内のすべてのファイルをリストし、名前で並べ替えます。
SGPTは、入力に基づいてシェルコマンドを生成できます。
$ sgpt sh " make all files in current directory read only "
chmod -R 444 *シェルコマンドを生成して直接実行することもできます。
$ sgpt sh --execute " make all files in current directory read only "
chmod -R 444 *
Do you want to execute this command ? (Y/n) y shコマンドは、シェルコマンドを生成するデフォルトのペルソナです。ペルソナの詳細については、ドキュメントを参照してください。
現在、SGPTはインタラクティブなシェルセッションをサポートしていません。ただし、 rlwrap使用して、インタラクティブなシェルセッション(ソース)を有効にすることができます。
$ rlwrap bash -c 'echo ▶; while read in; do [[ -n "$in" ]] && echo ■ && sgpt --chat chat_name "$in" && echo ▶; done'
▶
mass of sun
■
The mass of the Sun is approximately 1.989 x 10^30 kilograms, or about 330,000 times the mass of Earth. It contains about 99.86% of the total mass of the Solar System and is by far the most dominant object in it. The Sun's mass is composed mostly of hydrogen (~74%) and helium (~24%), with the remaining 2% consisting of heavier elements.
▶
convert to earth masses
■
To convert the mass of the Sun to Earth masses, you can simply divide the Sun's mass by the mass of the Earth. Given that:
A. The Sun's mass is approximately 1.989 x 10^30 kilograms.
B. The Earth's mass is approximately 5.972 x 10^24 kilograms.
Using these values, you can calculate how many Earth masses the Sun is:
(1.989 x 10^30 kg) / (5.972 x 10^24 kg/Earth) = approximately 333,000 Earth masses
So the Sun is about 333,000 times more massive than the Earth.
▶
自動化されたセッション名の生成と通知サポートを備えたスクリプトは、次のようになります。
#! /usr/bin/env bash
shopt -s -o errexit
shopt -s -o pipefail
shopt -s -o nounset
shopt -s inherit_errexit
export CHAT= " $( date ' +%Y%m%d%H%M%S%3N ' ) _ $( tr -dc ' A-Za-z ' < /dev/urandom | head -c 3 ) "
rlwrap bash -c ' echo ▶; while read in; do [[ -n "$in" ]] && echo ■ && sgpt --chat "$CHAT" "$in" && echo ▶ && notify-send --urgency=low ; done 'このソリューションを考え出してくれた @Ilya-Bystrovに感謝します。
SGPTは、指定された命令に基づいてコードを効率的に生成できます。たとえば、Pythonを使用した古典的なFizzbuzzの問題を解決するには、次のようにプロンプトを提供するだけです。
$ sgpt code " Solve classic 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)SGPTは、適切なPythonコードを返して、Fizzbuzzの問題に対処します。
codeコマンドは、コードを生成するデフォルトのペルソナです。ペルソナの詳細については、ドキュメントを参照してください。
SGPTは、バッシュエイリアスと機能を作成することにより、さらにワークフローに統合できます。これにより、OpenAIモデルとシェルコマンドを使用するときに、一般的なタスクを自動化し、効率を向上させることができます。
実際、次のBASH関数を使用してGITコミットメッセージを生成するようにSGPTを構成できます。
gsum () {
commit_message= " $( sgpt txt " Generate git commit message, my changes: $( git diff ) " ) "
printf " %sn " " $commit_message "
read -rp " Do you want to commit your changes with this commit message? [y/N] " response
if [[ $response =~ ^[Yy]$ ]] ; then
git add . && git commit -m " $commit_message "
else
echo " Commit cancelled. "
fi
}たとえば、この説明とbash関数のコミットメッセージは次のように表示されます。
$ gsum
feat: Add bash function to generate git commit messages
Added `gsum () ` function to ` .bash_aliases ` that generates a commit message using sgpt to summarize git changes.
The user is prompted to confirm the commit message before executing ` git add . & & git commit -m " <commit_message> " ` .
This function is meant to automate the commit process and increase productivity in daily work.
Additionally, updated the README.md file to include information about the new bash function and added a section to
showcase useful bash aliases and functions found in ` .bash_aliases ` .
Do you want to commit your changes with this commit message ? [y/N] y
[main d6db80a] feat: Add bash function to generate git commit messages
2 files changed, 48 insertions(+)
create mode 100644 .bash_aliases更新されたGSUM関数を含む有益なバッシュエイリアスと関数の編集は、.bashrcで入手できます。
Shell-Gptに触発されました。