sgpt

module
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2023 License: MIT

README

SGPT

Release Software License Codecov branch Go Report Card Go Doc Read the Docs Conventional Commits Protected by Gitleaks

SGPT (aka shell-gpt) 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.

This is a Go implementation. For the original Python implementation, visit shell-gpt. Please keep this in mind when reporting issues.

Features

  • Instant Answers: Obtain quick and accurate responses to simple questions directly in your shell, streamlining your workflow.
  • Shell Commands Generation: Effortlessly generate and execute shell commands, simplifying complex tasks and enhancing productivity.
  • Code Production: Generate code snippets in various programming languages, making it easier to learn new languages or find solutions to coding problems.
  • ChatGPT Integration: Utilize ChatGPT's interactive chat capabilities to refine your prompts and achieve more precise results, benefiting from the powerful language model.
  • Bash Functions and Aliases: Seamlessly integrate SGPT responses into custom bash functions and aliases, optimizing your workflows and making your daily tasks more efficient.

By offering these versatile features, SGPT serves as a powerful tool to enhance your overall productivity, streamline your workflow, and simplify complex tasks.

Installation

Linux

SGPT has been tested on Ubuntu LTS releases and is expected to be compatible with the following Linux distributions:

  • Debian
  • Ubuntu
  • Arch Linux
  • Fedora

To install, download the latest release from the release page and use the package manager specific to your distribution.

macOS

For users with Homebrew as their package manager, run the following command in the terminal:

brew install tbckr/tap/sgpt
Windows

For users with Scoop as their package manager, execute these commands in PowerShell:

scoop bucket add tbckr https://github.com/tbckr/scoop-bucket.git
scoop install tbckr/sgpt
Using Go

To install SGPT with Go, based on the git tag, use this command:

go install github.com/tbckr/sgpt/cmd/sgpt@latest
Docker

To run SGPT with Docker, use the following command to pull the latest image:

docker pull ghcr.io/tbckr/sgpt:latest

Examples on how to use SGPT with Docker can be found here.

Other platforms

For other platforms, visit the GitHub release page and download the latest release suitable for your system.

Usage Guide

See the docs for detailed usage instructions.

Getting started: Obtaining an OpenAI API Key

To use the OpenAI API, you must first obtain an API key.

  1. Visit https://platform.openai.com/overview and sign up for an account.
  2. Navigate to https://platform.openai.com/account/api-keys and generate a new API key.
  3. On Linux or macOS: Update your .bashrc or .zshrc file to include the following export statement adding your API key as the value:
export OPENAI_API_KEY="sk-..."
  1. On Windows: Update your environment variables to include the OPENAI_API_KEY variable with your API key as the value.

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.

Querying OpenAI Models

SGPT allows you to ask simple questions and receive informative answers. For example:

$ sgpt "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
The mass of the sun is approximately 1.989 x 10^30 kilograms.
Chat Capabilities

SGPT provides chat functionality that enables interactive conversations with OpenAI models. You can use the --chat flag 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.

  1. The first command initiates a chat session named ls-files and asks the model to "list all files directory":
$ sgpt sh --chat ls-files "list all files directory"
ls
  1. The second command continues the conversation within the ls-files chat session and requests to "sort by name":
$ sgpt sh --chat ls-files "sort by name"
ls | sort

The model provides the appropriate shell command ls | sort, which lists all files in a directory and sorts them by name.

Generating and Executing Shell Commands

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) y

The sh command is a default persona to generate shell commands. For more information on personas, see the docs.

Code Generation Capabilities

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.

The code command is a default persona to generate code. For more information on personas, see the docs.

Enhancing Your Workflow with Bash Aliases and Functions

SGPT 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_aliases

A compilation of beneficial bash aliases and functions, including an updated gsum function, is available in .bash_aliases.

Acknowledgements

Inspired by shell-gpt.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL