Skip to content

Instantly share code, notes, and snippets.

@martingaido
Created March 6, 2023 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martingaido/6903257409aacf265695cf6a2cb68d08 to your computer and use it in GitHub Desktop.
Save martingaido/6903257409aacf265695cf6a2cb68d08 to your computer and use it in GitHub Desktop.
TerminalGPT
#!/bin/bash
# Replace YOUR_API_KEY with your actual API key
API_KEY="OPEN_AI_API_KEY"
# Get the message from the command line argument
MESSAGE=$1
# Set up the API endpoint
API_ENDPOINT="https://api.openai.com/v1/chat/completions"
# Set up the JSON request body
JSON="{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"$MESSAGE\"}]}"
# Send the request to the API using curl
RESPONSE=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "$JSON" \
$API_ENDPOINT)
# Extract the response text from the JSON response
RESPONSE_TEXT=$(echo $RESPONSE | jq -r '.choices[].message.content')
# Print the response text to the console
echo "$RESPONSE_TEXT" | fold -w 100 -s
echo ""
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment