From eb54393f966124e834e0072bd4223513e27db2ec Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 1 Apr 2023 19:41:37 -0400 Subject: [PATCH 1/4] Add a function to communicate with chatgpt on the command line Why is this change needed? -------------------------- It's an experiment in accelerating development with the convenient use of generative AI. There are currently LOTS of articles about it. This implementation is based on this article: https://kadekillary.work/posts/1000x-eng/ How does it address the issue? ------------------------------ It adds a bash function, ask_chatgpt, that takes a prompt in the same way chatgpt would in the browser. I also aliased it to "h". I'm not sure I think that's the best alias, but it works for now. I don't actually expect this function to be all that useful by itself. I think a key feature of chatgpt is that it remembers what you're talking to it about, so you can make modifications and adjustments. That doesn't happen using this command line interface. However, I expect it will be a lot easier to automate things and provide large bodies of input to chatgpt over command line. Maybe even setting large bodies of context before asking a question. Per discussion about making the common files more development friendly, I added a line to the top of bashrc that sources all the bash files in a directory. The directory is hardcoded right now in a way that's probably not going to work for other people, but I had to stop shaving at some point. Anyway, the idea is that ALL function extension for bash will be done in files in that directory[1]. Any links to any relevant tickets, articles or other resources? --------------------------------------------------------------- 1 - https://3.basecamp.com/3093825/buckets/28354346/messages/5105707052#__recording_5163658670 --- .bashrc | 4 ++++ bash_functions/ask_chatgpt.sh | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 bash_functions/ask_chatgpt.sh diff --git a/.bashrc b/.bashrc index 5f1c031..0f3b516 100644 --- a/.bashrc +++ b/.bashrc @@ -4,6 +4,10 @@ [[ -f /etc/bash_completion ]] && source /etc/bash_completion +for file in ~/Dropbox/code/common-files/bash_functions/*.sh; do + source "$file" +done + export INPUTRC="$HOME/.inputrc" export EDITOR="emacsclient --alternate-editor=emacs" export GLOBIGNORE='.:..' diff --git a/bash_functions/ask_chatgpt.sh b/bash_functions/ask_chatgpt.sh new file mode 100644 index 0000000..cc7c687 --- /dev/null +++ b/bash_functions/ask_chatgpt.sh @@ -0,0 +1,23 @@ +function get_chatgpt_api_key { + # Your code to retrieve the API key goes here + # echo "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" + onepassword_uuid=$(op item list | grep -i 'chatgpt' | head | cut -d ' ' -f 1) + api_key=$(op item get $onepassword_uuid --fields api_key) + echo $api_key +} + +function ask_chatgpt { + local prompt="$1" + local api_key="$(get_chatgpt_api_key)" + local gpt="$(curl https://api.openai.com/v1/chat/completions -s \ + -H 'Content-Type: application/json' \ + -H "Authorization: Bearer $api_key" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{"role": "user", "content": "'"$prompt"'"}], + "temperature": 0.7 + }')" + echo "$gpt" | jq -r '.choices[0].message.content' +} + +alias h="ask_chatgpt" From 45966955f8f51bf87083b939e01dbc39ead6e4ab Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 1 Apr 2023 20:11:24 -0400 Subject: [PATCH 2/4] Add some error handling Why is this change needed? -------------------------- I got tired of it printing "null" whenever there was a problem with my request. How does it address the issue? ------------------------------ Check to see if they API returns an error message. Print that message if there is one. Any links to any relevant tickets, articles or other resources? --------------------------------------------------------------- --- bash_functions/ask_chatgpt.sh | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/bash_functions/ask_chatgpt.sh b/bash_functions/ask_chatgpt.sh index cc7c687..d90ee35 100644 --- a/bash_functions/ask_chatgpt.sh +++ b/bash_functions/ask_chatgpt.sh @@ -1,6 +1,4 @@ function get_chatgpt_api_key { - # Your code to retrieve the API key goes here - # echo "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" onepassword_uuid=$(op item list | grep -i 'chatgpt' | head | cut -d ' ' -f 1) api_key=$(op item get $onepassword_uuid --fields api_key) echo $api_key @@ -16,8 +14,15 @@ function ask_chatgpt { "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "'"$prompt"'"}], "temperature": 0.7 - }')" - echo "$gpt" | jq -r '.choices[0].message.content' -} + }' 2>&1)" # Redirect stderr to stdout + local error_message="$(echo "$gpt" | jq -r '.error.message')" + if [ "$error_message" != "null" ]; then + # If the API returned an error message, print it + echo "Error: $error_message" + else + # Otherwise, print the response + echo "$gpt" | jq -r '.choices[0].message.content' + fi +} alias h="ask_chatgpt" From 3020df13bfeb432d04fcc4d74c9294ee1fac00ef Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 1 Apr 2023 22:05:12 -0400 Subject: [PATCH 3/4] Allow the chatgpt function to take a data argument Why is this change needed? -------------------------- A more interesting use for command line chatgpt is the ability to write a file that provides significant context for a question and supply the entire thing. Some possibilities include - easily providing some code to optimize - accumulate a shared/sophisticated context for a question How does it address the issue? ------------------------------ If there are two arguments, append the second to the first. If there's only one argument, just use that as the prompt. Any links to any relevant tickets, articles or other resources? --------------------------------------------------------------- --- bash_functions/ask_chatgpt.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bash_functions/ask_chatgpt.sh b/bash_functions/ask_chatgpt.sh index d90ee35..a17fc1a 100644 --- a/bash_functions/ask_chatgpt.sh +++ b/bash_functions/ask_chatgpt.sh @@ -6,13 +6,22 @@ function get_chatgpt_api_key { function ask_chatgpt { local prompt="$1" + local data="$2" local api_key="$(get_chatgpt_api_key)" + local prompt_input="" + + if [ $# -eq 2 ]; then + prompt_input="$prompt: $data" + else + prompt_input="$prompt" + fi + local gpt="$(curl https://api.openai.com/v1/chat/completions -s \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer $api_key" \ -d '{ "model": "gpt-3.5-turbo", - "messages": [{"role": "user", "content": "'"$prompt"'"}], + "messages": [{"role": "user", "content": "'"$prompt_input"'"}], "temperature": 0.7 }' 2>&1)" # Redirect stderr to stdout local error_message="$(echo "$gpt" | jq -r '.error.message')" From 232c33d16319b7422b66298f8b83f942f8ed7437 Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 1 Apr 2023 22:29:22 -0400 Subject: [PATCH 4/4] Add a function generating images from OpenAI Why is this change needed? -------------------------- I want to put weird things in channel. How does it address the issue? ------------------------------ There's now a function called img_gpt in the terminal that takes a text description and provides a url for the created image. I aliased it to "hi", since the basic chat function is "h". And I think "hi" is funny. Any links to any relevant tickets, articles or other resources? --------------------------------------------------------------- https://kadekillary.work/posts/1000x-eng/ --- bash_functions/ask_chatgpt.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/bash_functions/ask_chatgpt.sh b/bash_functions/ask_chatgpt.sh index a17fc1a..f6484f1 100644 --- a/bash_functions/ask_chatgpt.sh +++ b/bash_functions/ask_chatgpt.sh @@ -34,4 +34,25 @@ function ask_chatgpt { fi } +function img_gpt { + local prompt="$1" + local api_key="$(get_chatgpt_api_key)" + + local create_img=$(curl https://api.openai.com/v1/images/generations -s \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $api_key" \ + -d '{ + "prompt": "'"$prompt"'", + "n": 1, + "size": "1024x1024" + }') + + echo "$create_img" | jq + + url=$(echo "$create_img" | jq -r '.data[0].url') + rand_num=$((1 + RANDOM % 1000000)) + curl -s "$url" -o "img-$rand_num.png" +} + +alias hi="img_gpt" alias h="ask_chatgpt"