Skip to content

Instantly share code, notes, and snippets.

@shikaan
Last active February 26, 2024 05:43
Show Gist options
  • Save shikaan/b978c5553a5545f5a48e2b8a6f4296a2 to your computer and use it in GitHub Desktop.
Save shikaan/b978c5553a5545f5a48e2b8a6f4296a2 to your computer and use it in GitHub Desktop.
bash GPT
#!/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