SGPT is a powerful command-line interface (CLI) tool designed for seamless interaction with OpenAI models directly from your terminal. Effortlessly run queries, generate shell commands or code, create images from text, and more, using simple commands. Streamline your workflow and enhance productivity with this powerful and user-friendly CLI tool.
Developed with the help of sgpt.
There are two primary ways to install SGPT:
- Install using Go:
With Go installed on your system, run the following command:
go install github.com/tbckr/sgpt/cmd/[email protected]- Download the latest release:
Visit the GitHub release page and download the latest release for your platform.
To build SGPT from the source code using Go, follow these steps:
- Ensure you have Go installed on your system.
- Clone the SGPT repository:
git clone https://github.com/tbckr/sgpt.git- Navigate to the cloned repository:
cd sgpt- Build the SGPT executable using Go:
go build -o sgpt cmd/sgpt/main.goThis will create an executable named sgpt in the current directory.
To build a Docker image for SGPT, use the docker-build.sh script located in the bin folder:
- Make sure you are in the root of the cloned SGPT repository.
- Make the
docker-build.shscript executable:
chmod +x bin/docker-build.sh- Run the
docker-build.shscript to build the Docker image:
bin/docker-build.shThe script will build the Docker image for the linux/amd64 platform with the tag sgpt:latest. Change the platform,
build args or tag according to your needs.
To use the OpenAI API, you must first obtain an API key.
- Visit https://platform.openai.com/overview and sign up for an account.
- Navigate to https://platform.openai.com/account/api-keys and generate a new API key.
- Update your
.bashrcor.zshrcfile to include the following export statement:
export OPENAI_API_KEY="sk-..."After completing these steps, you'll have an OpenAI API key that can be used to interact with the OpenAI models through the SGPT tool.
SGPT allows you to ask simple questions and receive informative answers. For example:
$ sgpt txt "mass of sun"
The mass of the sun is approximately 1.989 x 10^30 kilograms.You can also pass prompts to SGPT using pipes:
$ echo -n "mass of sun" | sgpt txt
The mass of the sun is approximately 1.989 x 10^30 kilograms.SGPT provides chat functionality that enables interactive conversations with OpenAI models. You can use the --chat
flag with the txt, sh, and code subcommands to initiate and reference chat sessions.
The chat capabilities allow you to interact with OpenAI models in a more dynamic and engaging way, making it easier to obtain relevant responses, code, or shell commands through continuous conversations.
The example below demonstrates how to fine-tune the model's responses for more targeted outcomes.
- The first command initiates a chat session named
ls-filesand asks the model to "list all files directory":
$ sgpt sh --chat ls-files "list all files directory"
lsThe model responds with the shell command ls, which is used to list all files in a directory.
- The second command continues the conversation within the
ls-fileschat session and requests to "sort by name":
$ sgpt sh --chat ls-files "sort by name"
ls | sortThe model provides the appropriate shell command ls | sort, which lists all files in a directory and sorts them by
name.
To manage active chat sessions, use the sgpt chat command. Here are the available options for chat session management:
sgpt chat ls: List all active chat sessions.sgpt chat show <chat session>: Display the content of a specific chat session.sgpt chat rm <chat session>: Remove a chat session.sgpt chat rm --all: Delete all chat sessions.
For users who prefer to use Docker, SGPT provides a Docker image:
- Pull the latest Docker image:
docker pull ghcr.io/tbckr/sgpt:latest- Run queries using the Docker image:
$ docker run --rm -e OPENAI_API_KEY=${OPENAI_API_KEY} ghcr.io/tbckr/sgpt:latest txt "mass of sun"
The mass of the sun is approximately 1.989 x 10^30 kilograms.When using SGPT within a Docker container, you can mount a local folder to the container's /home/nonroot path to save
and persist all active chat sessions. This allows you to maintain your chat history and resume previous conversations
across different container instances.
To mount a local folder and save chat sessions, follow these steps:
- Pull the SGPT Docker image:
$ docker pull ghcr.io/tbckr/sgpt:latest- Create a local folder to store your chat sessions, e.g.
sgpt-chat-sessions:
mkdir sgpt-chat-sessions- Change the permissions of the folder to the nonroot user of the Docker image:
sudo chown 65532:65532 sgpt-chat-sessions- Run the Docker container with the local folder mounted to
/home/nonroot:
$ docker run --rm -e OPENAI_API_KEY=${OPENAI_API_KEY} -v $(pwd)/sgpt-chat-sessions:/home/nonroot ghcr.io/tbckr/sgpt:latest txt "mass of sun"
The mass of the sun is approximately 1.99 x 10^30 kilograms.
$ docker run --rm -e OPENAI_API_KEY=${OPENAI_API_KEY} -v $(pwd)/sgpt-chat-sessions:/home/nonroot ghcr.io/tbckr/sgpt:latest txt "convert to earth masses"
To convert the mass of the sun to earth masses, we need to divide it by the mass of the Earth:
1.99 x 10^30 kg / 5.97 x 10^24 kg = 333,000 Earth masses (rounded to the nearest thousand)
So the mass of the sun is about 333,000 times greater than the mass of the Earth.SGPT can generate shell commands based on your input:
$ sgpt sh "make all files in current directory read only"
chmod -R 444 *You can also generate a shell command and execute it directly:
$ sgpt sh --execute "make all files in current directory read only"
chmod -R 444 *
Do you want to execute this command? (Y/n) ySGPT can be further integrated into your workflow by creating bash aliases and functions. This enables you to automate common tasks and improve efficiency when working with OpenAI models and shell commands.
Indeed, you can configure SGPT to generate your git commit message using the following bash function:
gsum() {
commit_message="$(sgpt txt "Generate git commit message, my changes: $(git diff)")"
printf "%s\n" "$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
}For instance, the commit message for this description and bash function would appear as follows:
$ 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_aliasesA compilation of beneficial bash aliases and functions, including an updated gsum function, is available in .bash_aliases.
SGPT can efficiently generate code based on given instructions. For instance, to solve the classic FizzBuzz problem using Python, simply provide the prompt as follows:
$ 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 will return the appropriate Python code to address the FizzBuzz problem
SGPT can also generate images based on textual descriptions using the DALLE API. To create an image from text, input your desired description as shown below:
$ sgpt image "v for vendetta"
<image url>SGPT will return an image URL representing the "V for Vendetta" prompt.
If you prefer to download the generated image directly to your current working directory, use the --download flag:
$ sgpt image --download "v for vendetta"
1c561592-6d93-438f-9bee-d96c898a31a8.pngThe image will be downloaded with a unique file name, making it easily accessible within your working directory.
Inspired by shell-gpt.