Last active
February 26, 2024 05:43
-
-
Save shikaan/b978c5553a5545f5a48e2b8a6f4296a2 to your computer and use it in GitHub Desktop.
bash GPT
This file contains 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
#!/bin/bash | |
OPENAI_API_KEY="YOUR_API_KEY" | |
CONVERSATION="/tmp/$(cat /proc/sys/kernel/random/uuid)" | |
query_open_ai() { | |
curl -s https://api.openai.com/v1/chat/completions \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $OPENAI_API_KEY" \ | |
-d "{ | |
\"model\": \"gpt-3.5-turbo\", | |
\"messages\": $1 | |
}" | |
} | |
echo "Start chatting with ChatGPT below (press Ctrl+C to exit)" | |
echo '[]' >> "$CONVERSATION" | |
while true; do | |
read -r -p "You: " prompt | |
messages=$(jq ". += [{\"role\":\"user\", \"content\": \"$prompt\"}]" "$CONVERSATION") | |
result=$(query_open_ai "${messages}") | |
message=$(echo "$result" | jq .choices[0].message) | |
content=$(echo "$message" | jq .content | cut -d'"' -f2) | |
echo -e "ChatGPT: \e[32m${content}\e[0m" | |
new_conversation=$(jq ". += [$message]" "$CONVERSATION") | |
echo "${new_conversation}" > "$CONVERSATION" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment