tbckr/sgpt
GitHub: tbckr/sgpt
一个用于在终端与AI模型交互的命令行工具,简化查询和自动化任务。
Stars: 444 | Forks: 35
# SGPT
[](https://github.com/tbckr/sgpt/releases/latest)
[](/LICENSE.md)
[](https://codecov.io/gh/tbckr/sgpt)
[](https://goreportcard.com/report/github.com/tbckr/sgpt/v2)
[](http://godoc.org/github.com/tbckr/sgpt)
[](https://github.com/goreleaser)
[](https://sgpt.readthedocs.io/)
[](https://conventionalcommits.org)
[](https://github.com/gitleaks/gitleaks-action)
SGPT(*又名 shell-gpt*)是一款强大的命令行界面(CLI)工具,专为从终端无缝交互 OpenAI 模型而设计。使用简单命令,轻松运行查询、生成 shell 命令或代码、从文本创建图像等等。使用这款强大且用户友好的 CLI 工具,简化您的工作流程并提高生产力。
借助 [SGPT](https://github.com/tbckr/sgpt) 开发。
这是一个 Go 实现。原始的 Python 实现,请访问 [shell-gpt](https://github.com/TheR1D/shell_gpt)。报告问题时请留意这一点。
- [功能](#features)
- [安装说明](#installation)
- [Linux](#linux)
- [macOS](#macos)
- [Windows](#windows)
- [使用 Go](#using-go)
- [Docker](#docker)
- [Ansible](#ansible)
- [其他平台](#other-platforms)
- [使用指南](#usage-guide)
- [入门:获取 OpenAI API 密钥](#getting-started-obtaining-an-openai-api-key)
- [查询 OpenAI 模型](#querying-openai-models)
- [GPT-4o 和 GPT-4 Vision API 支持](#gpt-4o-and-gpt-4-vision-api-support)
- [o1 API 支持](#o1-api-support)
- [OpenRouter API 支持](#openrouter-api-support)
- [本地 LLM 支持](#local-llm-support)
- [聊天功能](#chat-capabilities)
- [生成和执行 Shell 命令](#generating-and-executing-shell-commands)
- [交互式 Shell 会话](#interactive-shell-sessions)
- [代码生成功能](#code-generation-capabilities)
- [提示模板](#prompt-templating)
- [使用 Bash 别名和函数增强工作流程](#enhancing-your-workflow-with-bash-aliases-and-functions)
- [致谢](#acknowledgements)
## 功能
- **即时答案:** 直接在 shell 中获取针对简单问题的快速准确响应,简化您的工作流程。
- **GPT-4o 集成:** 访问 [GPT-4o API](https://platform.openai.com/docs/models/gpt-4o) 的功能,生成详细且信息丰富的响应。
- **GPT-4 Vision API:** 利用 [GPT-4 Vision API](https://platform.openai.com/docs/guides/vision) 的功能,分析图像并生成见解。
- **Shell 命令生成:** 轻松生成并执行 shell 命令,简化复杂任务并提高生产力。
- **代码生成:** 生成多种编程语言的代码片段,使学习新语言或找到编程问题的解决方案变得更加容易。
- **ChatGPT 集成:** 利用 ChatGPT 的交互式聊天功能来优化您的提示并获得更精确的结果,受益于强大的语言模型。
- **Bash 函数和别名:** 将 SGPT 响应无缝集成到自定义 bash 函数和别名中,优化您的工作流程,使日常任务更加高效。
- **OpenRouter 支持:** 使用 [OpenRouter](https://openrouter.ai) 通过单一 API 访问各种大型语言模型(LLMs),为您提供与不同模型交互的灵活性和便利性。
- **提示模板:** 使用 Go `text/template` 语法将结构化数据注入提示。通过 stdin 管道传输 YAML 或 JSON 变量,以构建动态、可重用的提示模式。
## 安装说明
### Linux
SGPT 已在 Ubuntu LTS 版本上进行测试,预计兼容以下 Linux 发行版:
- Debian
- Ubuntu
- Arch Linux
- Fedora
要安装,请从[发布页面](https://github.com/tbckr/sgpt/releases)下载最新版本,并使用适用于您发行版的包管理器。
### macOS
对于使用 Homebrew 作为包管理器的用户,在终端中运行以下命令:
```
brew install tbckr/tap/sgpt
```
### Windows
对于使用 Scoop 作为包管理器的用户,在 PowerShell 中执行以下命令:
```
scoop bucket add tbckr https://github.com/tbckr/scoop-bucket.git
scoop install tbckr/sgpt
```
### 使用 Go
要通过 Go 安装 SGPT,基于 git 标签,请使用此命令:
```
go install github.com/tbckr/sgpt/v2/cmd/sgpt@latest
```
### Docker
要使用 Docker 运行 SGPT,请使用以下命令拉取最新镜像:
```
docker pull ghcr.io/tbckr/sgpt:latest
```
如何使用 Docker 运行 SGPT 的示例可以在[这里](https://sgpt.readthedocs.io/en/latest/usage/docker/)找到。
### Ansible
要使用 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
```
可以使用以下命令运行该 playbook:
```
ansible-playbook -i .yml
```
playbook 的最新版本可以在[这里](https://github.com/tbckr/sgpt/blob/main/playbook.yml)找到。
### 其他平台
对于其他平台,请访问 GitHub [发布页面](https://github.com/tbckr/sgpt/releases)并下载适合您系统的最新版本。
## 使用指南
详细使用说明请参阅[文档](https://sgpt.readthedocs.io/en/stable/)。
### 入门:获取 OpenAI API 密钥
要使用 OpenAI API,您必须先获取一个 API 密钥。
1. 访问 [https://platform.openai.com/overview](https://platform.openai.com/overview) 并注册一个账户。
2. 导航到 [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys) 并生成一个新的 API 密钥。
3. 在 Linux 或 macOS 上:更新您的 `.bashrc` 或 `.zshrc` 文件,添加以下 export 语句,将您的 API 密钥作为值:
```
export OPENAI_API_KEY="sk-..."
```
4. 在 Windows 上:[更新您的环境变量](https://geekflare.com/system-environment-variables-in-windows/),添加 `OPENAI_API_KEY` 变量,将其值设置为您的 API 密钥。
完成这些步骤后,您将拥有一个可用于通过 SGPT 工具与 OpenAI 模型交互的 OpenAI API 密钥。
### 查询 OpenAI 模型
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` 标志。这将在生成输出时将其流式传输到命令行。
### GPT-4o 和 GPT-4 Vision API 支持
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.
```
您还可以在[配置文件](https://sgpt.readthedocs.io/en/stable/configuration/)中设置默认模型为 GPT-4o 或 GPT-4 Vision。
**重要提示:** GPT-4o 和 GPT-4-vision API 集成目前处于测试阶段,未来可能会发生变化。
### o1 API 支持
如果您已被列入 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 不支持 persona。
### OpenRouter API 支持
SGPT 与 [OpenRouter API](https://openrouter.ai) 无缝集成,让您可以访问 OpenAI 产品之外的广泛 AI 模型。
1. 设置 OpenRouter API 基础 URL 环境变量:
export OPENAI_API_BASE="https://openrouter.ai/api/v1"
2. 在 [OpenRouter](https://openrouter.ai/settings/keys) 创建 API 密钥并将其设置为您的环境变量:
export OPENAI_API_KEY="your_openrouter_api_key"
配置完成后,您可以使用 `-m` 标志指定任何 OpenRouter 支持的模型:
```
$ sgpt -m "anthropic/claude-3.7-sonnet" "mass of sun"
The mass of the Sun is approximately:
1.989 × 10^30 kilograms (kg)
This is roughly 333,000 times the mass of Earth. The Sun contains about 99.86% of all the mass in our solar system.
```
在 [OpenRouter 模型页面](https://openrouter.ai/models)浏览可用模型的完整列表。
### 本地 LLM 支持
SGPT 可与任何兼容 OpenAI 的本地 LLM 后端(Ollama、LiteLLM、vLLM、llama.cpp 等)配合使用,只需将 `OPENAI_API_BASE` 指向后端的 URL。对于本地设置,允许使用纯 `http://` 连接环回和私有网络范围:
```
export OPENAI_API_KEY="dummy" # most local backends ignore the key but the env var must be set
export OPENAI_API_BASE="http://localhost:11434/v1" # e.g. Ollama
sgpt -m "llama3" "mass of sun"
```
非 IP 字面量的 LAN 主机名(例如 `http://thinkbox:8080/v1`)需要显式选择退出,因为在没有 DNS 解析的情况下无法将其归类为私有。在命令行上传递标志,或在配置文件中设置键:
```
sgpt --insecure-api-base "..."
# 或者在 ~/.config/sgpt/config.yaml 中设置 `insecureAPIBase: true`
```
完整的验证规则和原理请参见[查询模型 — 覆盖 OpenAI API 基础 URL](docs/usage/query-models.md#override-openai-api-base-url)。
### 聊天功能
SGPT 提供聊天功能,支持与 OpenAI 模型进行交互式对话。您可以使用 `--chat` 标志来启动和引用聊天会话。
聊天功能让您能够以更具动态性和吸引力的方式与 OpenAI 模型交互,从而更容易通过连续对话获得相关的响应、代码或 shell 命令。
下面的示例展示了如何微调模型的响应以获得更具针对性的结果。
1. 第一个命令启动一个名为 `ls-files` 的聊天会话,并要求模型“列出目录中的所有文件”:
```
$ sgpt sh --chat ls-files "list all files directory"
ls
```
2. 第二个命令在 `ls-files` 聊天会话中继续对话,并请求“按名称排序”:
```
$ sgpt sh --chat ls-files "sort by name"
ls | sort
```
模型提供了相应的 shell 命令 `ls | sort`,该命令列出目录中的所有文件并按名称排序。
### 生成和执行 Shell 命令
SGPT 可以根据您的输入生成 shell 命令:
```
$ 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 命令的默认 persona。有关 persona 的更多信息,请参阅[文档](https://sgpt.readthedocs.io/en/latest/usage/personas/)。
### 交互式 Shell 会话
目前,SGPT 不支持交互式 shell 会话。但是,可以使用 `rlwrap` 来启用类似交互式的 shell 会话([来源](https://github.com/tbckr/sgpt/issues/111#issuecomment-1869814041)):
```
$ 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' "`.
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
```
有益的 bash 别名和函数汇编,包括更新后的 gsum 函数,可在 [.bashrc](.bashrc) 中找到。
## 致谢
灵感来源于 [shell-gpt](https://github.com/TheR1D/shell_gpt)。
标签:AI助手, API工具, EVTX分析, Go语言, OpenAI, Petitpotam, shell工具, SOC Prime, 代码生成, 内存规避, 命令生成, 图像生成, 开发工具, 技术工具, 日志审计, 渗透测试工具, 生产力工具, 程序破解, 系统提示词, 终端工具, 脚本工具, 请求拦截, 软件工具