Skip to content

ChatGPT #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

[[ -f /etc/bash_completion ]] && source /etc/bash_completion

for file in ~/Dropbox/code/common-files/bash_functions/*.sh; do
Copy link
Contributor

@mediocretes mediocretes May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path is not a good path for me.

Also, maybe everyone should have an env variable with their root code path in it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. I think the path could be ~/bash_functions/*.sh which should be symlinked because of stow. Though, I think we probably want to move that folder to ~/.common_files/bash_functions or actually I think ~/.common_files/bin and adding that to the PATH is probably a more common pattern in dotfiles.

source "$file"
done

export INPUTRC="$HOME/.inputrc"
export EDITOR="emacsclient --alternate-editor=emacs"
export GLOBIGNORE='.:..'
Expand Down
58 changes: 58 additions & 0 deletions bash_functions/ask_chatgpt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function get_chatgpt_api_key {
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 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_input"'"}],
"temperature": 0.7
}' 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
}

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"