-
Notifications
You must be signed in to change notification settings - Fork 6
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
esquivalient
wants to merge
5
commits into
master
Choose a base branch
from
chatgpt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ChatGPT #29
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb54393
Add a function to communicate with chatgpt on the command line
esquivalient 4596695
Add some error handling
esquivalient 3020df1
Allow the chatgpt function to take a data argument
esquivalient 232c33d
Add a function generating images from OpenAI
esquivalient 70be2a4
Merge remote-tracking branch 'origin' into chatgpt
esquivalient File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.