SGPT(又名Shell-GPT )是一種功能強大的命令行接口(CLI)工具,旨在直接從終端與OpenAI模型無縫交互。毫不費力地運行查詢,生成shell命令或代碼,使用簡單命令從文本中創建圖像以及更多圖像。通過此功能強大且用戶友好的CLI工具簡化工作流程並提高生產力。
在SGPT的幫助下開發。
這是GO實施。對於原始的Python實施,請訪問Shell-GPT。報告問題時請記住這一點。
注意:目前,V3的重構重構,但V2仍保持維護。
通過提供這些多功能功能,SGPT是提高整體生產力,簡化工作流程並簡化複雜任務的強大工具。
SGPT已在Ubuntu LTS版本上進行了測試,並有望與以下Linux分佈兼容:
要安裝,請從發布頁面下載最新版本,並使用特定於您的發行版的軟件包管理器。
對於以自己的軟件包管理器為軟件包的用戶,在終端中運行以下命令:
brew install tbckr/tap/sgpt對於將SCOP作為其軟件包管理器的用戶,請在PowerShell中執行這些命令:
scoop bucket add tbckr https://github.com/tbckr/scoop-bucket.git
scoop install tbckr/sgpt要根據GIT標籤安裝SGPT,請使用此命令:
go install github.com/tbckr/sgpt/v2/cmd/sgpt@latest要使用Docker運行sgpt,請使用以下命令來拉最新圖像:
docker pull ghcr.io/tbckr/sgpt:latest有關如何將SGPT與Docker一起使用的示例可以在此處找到。
要使用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變量。完成這些步驟後,您將擁有一個OpenAI API密鑰,可用於通過SGPT工具與OpenAI模型進行交互。
注意:您的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 a也可以將URL和本地圖像結合在一起:
$ 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視覺。
重要的是: GPT-4O和GPT-4-VISION API集成目前正在Beta中,將來可能會發生變化。
如果您已經將O1 API列入白名單,則可以通過使用-m標誌指定模型來使用它。您還必須提供--stream=false標誌,以不流式傳輸輸出,因為O1 API不支持輸出(如果您通過配置文件提供了流選項,則只有必要)。
例子:
$ sgpt -m " o1-preview " --stream=false " how many rs are in strawberry? "
There are three " r " s in the word " strawberry " .您還可以創建一個bash別名,以更輕鬆地使用O1 API。例如,將以下行添加到您的.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 ,其中列出了目錄中的所有文件並按名稱對其進行分類。
SGPT可以根據您的輸入生成外殼命令:
$ sgpt sh " make all files in current directory read only "
chmod -R 444 *您還可以生成shell命令並直接執行它:
$ 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命令是生成shell命令的默認角色。有關角色的更多信息,請參見文檔。
當前,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命令是生成代碼的默認角色。有關角色的更多信息,請參見文檔。
通過創建bash別名和功能,可以將SGPT進一步集成到您的工作流程中。這使您可以在使用OpenAI模型和Shell命令時自動化常見任務並提高效率。
確實,您可以配置SGPT使用以下bash函數生成git提交消息:
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.bashrc提供了有益的bash別名和功能(包括更新的GSUM函數)的彙編。
靈感來自殼牌-GPT。