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。