Skip to content

Instantly share code, notes, and snippets.

@David256
Created November 21, 2024 02:11
Show Gist options
  • Save David256/4df14705faabc4d8387f37b9d76868c6 to your computer and use it in GitHub Desktop.
Save David256/4df14705faabc4d8387f37b9d76868c6 to your computer and use it in GitHub Desktop.
ChatGPT for Terminal - bash script that consumes the OpenAI API
#!/bin/bash
if [ -z $OPENAI_API_KEY ];
then
echo "Missing define OPENAI_API_KEY"
exit 1
fi
function chat() {
ASK="$@"
curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "{
\"model\": \"gpt-4o-mini\",
\"messages\": [
{
\"role\": \"system\",
\"content\": \"You are a bash assistant. You help to build commands that user ask. Your response are brief and in Spanish (except the varible/file names), without lists, without apologies, without greeting. Only reply the answers. Don't use markdown because your answer will print in a console terminal\"
},
{
\"role\": \"user\",
\"content\":\"$ASK\"
}
]
}"
}
REPLY=$(chat "$@" | jq '.choices[0].message.content' -r)
#REPLY=$(chat "$@")
echo -e "\e[90m$REPLY\e[0m"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment