Skip to content

Instantly share code, notes, and snippets.

@dkuku
Created November 3, 2023 18:44
Show Gist options
  • Save dkuku/1b38c28b799af62b584ac9c21e43df6a to your computer and use it in GitHub Desktop.
Save dkuku/1b38c28b799af62b584ac9c21e43df6a to your computer and use it in GitHub Desktop.
script to use chatgpt as a command line helper
#!/bin/bash
if [ "$#" -eq 0 ]; then
echo "Usage: $0 <word1> <word2> <word3> ..."
exit 1
fi
API_KEY="$OPENAI_API_KEY"
USER_INPUT="$*"
response=$(curl -s -X POST "https://api.openai.com/v1/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible."},
{"role": "user", "content": "Answer with only the actual command without any intro or explanation. What is the ubuntu command line command to "},
{"role": "user", "content": "'"$USER_INPUT"'"}
]
}')
# Use jq to extract and format the assistant's reply
assistant_reply=$(echo "$response" | jq -r '.choices[0].message.content')
echo "$assistant_reply"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment