Coder Social home page Coder Social logo

gpterm's Introduction

GPTerm

GPTerm is a tiny but full featured Python script that provides a command line interface for ChatGPT. It offers auto saved chat history, shell command generation, customisable roles, code block highlighting, multi-line input and many other simple usability features in a single script.

terminal

terminal2

Suggested installation

Copy script to ~/.local/bin/gpterm and make executable. This will also allow chat list autocomplete (see below) to work correctly.

Dependencies

Run the following command to install dependencies:

pip install openai distro pyperclip prompt_toolkit

Usage

You can start a standard chat session by running the script with no arguments, or get single replies without entering a chat session by including your question:

$ gpterm "approximately how many emperor penguins can you fit in a lamborghini aventador?"
$ uptime | gpterm "How's the system load looking?"
GPT: The system load average is 0.89, 0.84, and 0.81, indicating a moderate load on the system.
$ gpterm -c "can you search the current directory for files containing 'gpt-3.5'?"    
grep -r "gpt-3.5" .
Execute command? [y/n] y
./gptbackup.py:        response = openai.ChatCompletion.create(model='gpt-3.5-turbo', messages=messages, stream=True)
./chatgpt-stream/gpterm.py:model = "gpt-3.5-turbo-16k" #"gpt-3.5-turbo-16k"
./gptterm-prompt:models = ["gpt-3.5-turbo-16k", "gpt-4"]
./gptbs4.py:        model='gpt-3.5-turbo',

To enter multi-line data, such as code snippets, you can use the !multi command. Enter the lines of your input, and finish with !end on a new line.

Chat history will be automatically saved when you type !quit, or !q.

Listing Previous Chats

You can list all previous chat sessions by using the -l flag:

gpterm -l

This will display the names of all the chat files in the default directory ~/.local/share/gpterm.

Resuming Previous Chats

You can resume a previous chat session by providing the -r flag followed by the name of the chat to resume:

gpterm -r CHAT_NAME

You can view previous chat history once resumed by typing !history.

Chat Resume Autocomplete

The following can be added for chat list autocmplete

Add to .bashrc

_gpterm_completion() {
  local curr_word
  local -a opts 
  curr_word="${COMP_WORDS[COMP_CWORD]}"
  while IFS= read -r line; do
    opts+=("$line")
  done < <(gpterm -l | awk -F '(' '{print $1}' | sed 's/ *$//')
  COMPREPLY=($(compgen -W "${opts[*]}" -- "$curr_word"))
  return 0
}
complete -F _gpterm_completion gpterm

Add to .zshrc

function _gpt_completion {
  local -a opts
  IFS=$'\n' opts=($(gpterm -l | awk -F '(' '{print $1}' | sed 's/ *$//'))
  _describe 'values' opts
}
compdef _gpt_completion gpterm

Configuration

Before using GPTerm, make sure to set your OpenAI API key by replacing the placeholder value "YOUR_API_KEY" with your actual API key in the script. Extra roles and models can also be added in the script itself.

gpterm -h :

GPTerm: A Command-Line Interface for ChatGPT. Version 0.2

USAGE:
    gpt [OPTION]... [QUESTION]

OPTIONS:
    -l                  Lists all previous stored chats in {storage_location}.
    -r CHAT_NAME        Resumes a previous chat session. CHAT_NAME should be replaced with the name of the chat file.
    -c QUERY            Submits a QUERY to ChatGPT for a shell command and prompts the user to execute the command.
          
CHAT COMMANDS:
    !quit or !q         Ends the current chat and saves it with automatic naming.
    !kill               Ends the current chat without saving.
    !role               Cycle through roles
    !model              Cycle through models
    !temperature        Set the temperature as a float value: 0.0 to 2.0
    !tokens             Rudimentary token count
    !history            Prints the current or resumed chat session history.
    !copy CODEBLOCK_ID  Copies the specified code block to the clipboard. Replace CODEBLOCK_ID with the ID of the code block.
    !multi              Initiates multi-line input mode. Useful for pasting data with multiple lines such as code. 
                        Finish multi-line entry with '!end' on a newline or hit CTRL-D.

gpterm's People

Contributors

deviousd4n avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.