~kmaasrud/shatgpt

c3b7f04d5f8fbb5a079ccbe8d28a6ba855828103 — Knut Magnus Aasrud 1 year, 6 months ago 242e53f
fix newline escaping and some other stuff
1 files changed, 49 insertions(+), 36 deletions(-)

M shatgpt
M shatgpt => shatgpt +49 -36
@@ 39,7 39,7 @@ cwd() {

clear_line() {
    # clear the current line and place cursor at the beginning
    printf '\r\033[K'
    printf '\r\033[K' >&2
}

spinner() {


@@ 51,37 51,45 @@ spinner() {
        for char in $spinner_chars
        do
            clear_line
            printf '%s %s' "$char" "$1"
            printf '%s %s' "$char" "$1" >&2
            sleep 0.05
        done
    done
}

mkprompt_shell() {
    prompt="Provide only commands or scripts for the shell \`$(shell)\`."
    prompt="$prompt Do not give any explanations or descriptions."
    prompt="$prompt Provide the command exactly as it should be ran, so the user can copy and paste it."
    prompt="$prompt Ensure the output is a valid for the user's shell."
    prompt="$prompt Provide the most logical solution if the prompt is unclear."
    prompt="$prompt Try to make a one-line solution, but it is not required."
    prompt="$prompt The user is using the operating system $(os)."
    prompt="$prompt The user is currently in the directory \`$(cwd)\` on a host named \`$(hostname)\`."
    prompt="$prompt The user request is: $1"
    prompt="Provide only commands or scripts for the shell \`$(shell)\`.\n\
Do not give any explanations or descriptions.\n\
Provide the command exactly as it should be ran, so the user can copy and paste it.\n\
Ensure the output is a valid for the user's shell.\n\
Provide the most logical solution if the prompt is unclear.\n\
Try to make a one-line solution, but it is not required.\n\
The user is using the operating system $(os).\n\
The user is currently in the directory \`$(cwd)\` on a host named \`$(hostname)\`.\n"
    if [ -n "$2" ]; then
        prompt="$prompt""The user piped this in from stdin: $2\n"
    fi
    prompt="$prompt""The user request is: $1"
    printf '%s' "$prompt"
}

mkprompt_default() {
    prompt='You are a shell script called shatgpt, assisting the user with programming and system administration.'
    prompt="$prompt The user is using the operating system $(os)."
    prompt="$prompt The user is currently in the directory \`$(cwd)\` on a host named \`$(hostname)\`."
    prompt="$prompt The user request is: $1"
    prompt="You are a shell script called shatgpt, assisting the user with programming and system administration.\n\
The user is using the operating system $(os).\n\
The user is currently in the directory \`$(cwd)\` on a host named \`$(hostname)\`.\n"
    if [ -n "$2" ]; then
        prompt="$prompt""The user piped this in from stdin: $2\n"
    fi
    prompt="$prompt""The user request is: $1"
    printf '%s' "$prompt"
}

mkbody() {
    # make a body to send to the openai chat api
    # $1 - prompt
    printf '%s\n' "{\"model\": \"gpt-3.5-turbo\", \"stream\": true, \"messages\": [{\"role\": \"user\", \"content\": \"$1\"}]}"
    printf '%s' "{\"model\": \"gpt-3.5-turbo\", \"stream\": true, \"messages\": [{\"role\": \"user\", \"content\": \""
    printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
    printf '%s' "\"}]}"
}

get_field() {


@@ 103,8 111,9 @@ get_field() {
          break
        ;;
        '\')
          printf '%s' "$c"
          rest="${tmp#?}"
          c="$char${tmp%"$rest"}"
          c="${tmp%"$rest"}"
          tmp="$rest"
        ;;
        *);;


@@ 157,7 166,7 @@ term() {
trap term INT TERM

show_help() {
  printf '%s\n' "usage: $0 [-h|--help] [-e|--edit] [-s|--shell] prompt"
  printf '%s\n' "usage: $(basename $0) [-hes] prompt"
  printf '%s\n' "options:"
  printf '%s\n' "  -h, --help     show this help message and exit."
  printf '%s\n' "  -e, --edit     open an editor to enter the prompt interactively."


@@ 188,26 197,30 @@ parse_args() {
  prompt="$*"
}

main() {
    editor_mode=false
    shell_mode=false
editor_mode=false
shell_mode=false

    parse_args $*
if [ -t 0 ]; then
  stdin=""
else
  stdin="$(cat)"
fi

    if [ $editor_mode = true ]; then
      tmp=$(mktemp)
      printf '%s' "$prompt" > $tmp
      $(editor) "$tmp"
      prompt="$(cat $tmp)"
    fi
parse_args $*

    if [ $shell_mode = true ]; then
      prompt="$(mkprompt_shell "$prompt")"
    else
      prompt="$(mkprompt_default "$prompt")"
    fi
if [ $editor_mode = true ]; then
  tmp=$(mktemp)
  printf '%s' "$prompt" > $tmp
  $(editor) "$tmp"
  prompt="$(cat $tmp)"
fi

    stream_events "https://api.openai.com/v1/chat/completions" "$(mkbody "$prompt")"
}
if [ $shell_mode = true ]; then
  prompt="$(mkprompt_shell "$prompt" "$stdin")"
else
  prompt="$(mkprompt_default "$prompt" "$stdin")"
fi

request=$(mkbody "$prompt")

main "$*"
stream_events "https://api.openai.com/v1/chat/completions" "$request"