Skip to content

Instantly share code, notes, and snippets.

@stevermeister
Created March 2, 2023 07:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save stevermeister/2da7be53fe8d513c712672dd8e72befe to your computer and use it in GitHub Desktop.
chatGPT API via shell
#!/bin/bash
# This script is an example CLI for OpenAI
# Check for required dependencies
if ! command -v curl > /dev/null; then
echo "Error: curl is not installed. Please install curl and try again."
exit 1
fi
# Define the OpenAI API endpoint
API_ENDPOINT="https://api.openai.com/v1/chat/completions"
# Define the API key for authentication
API_KEY="INPUT_YOUR_KEY_HERE"
# Define the function for making API requests
make_request() {
local endpoint="$1"
local data="$2"
curl -X POST -s -H "Content-Type: application/json" -H "Authorization: Bearer $API_KEY" "$endpoint" -d "$data"
}
# Define the main function for the CLI
main() {
# Check if the required number of arguments have been provided
if [ "$#" -ne 1 ]; then
echo "Error: Please provide a single argument to the script."
exit 1
fi
#code-davinci-002
# Make the API request with the provided argument as the input
local response=$(make_request "$API_ENDPOINT" "{ \"temperature\": 0.9, \"max_tokens\": 60,\"model\": \"gpt-3.5-turbo\",\"messages\": [{\"role\": \"user\", \"content\": \"$1\"}]}")
# Print the response from the API
#echo "$response"
local response_text=$(echo "$response" | jq '.choices[0].message.content')
echo "$response_text"
}
# Call the main function with the arguments provided to the script
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment