Skip to content

Instantly share code, notes, and snippets.

@martingaido
Last active July 20, 2023 23:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martingaido/79a09aa2760ab9d2f5c5135bf9f6cd5a to your computer and use it in GitHub Desktop.
Save martingaido/79a09aa2760ab9d2f5c5135bf9f6cd5a to your computer and use it in GitHub Desktop.
Code Validation for Terminal (bash) using ChatGPT-4
#!/bin/bash
# Replace YOUR_API_KEY with your actual API key
API_KEY="OPEN_AI_API_KEY"
# Clear the screen
clear
# Function to check if the API key is set
check_api_key() {
if [[ -z "$API_KEY" ]]; then
echo "API_KEY is not set. Please set your OpenAI API key before running the script."
exit 1
fi
}
# Function to check if the required utility 'jq' is installed
check_jq_installed() {
if ! command -v jq &>/dev/null; then
echo "'jq' is not installed. Please install 'jq' before running the script."
exit 1
fi
}
# Function to check if a file is provided as an argument
check_input_file() {
if [[ -z "$1" ]]; then
echo "Usage: $0 <path_to_code_file>"
exit 1
fi
if [[ ! -f "$1" ]]; then
echo "Error: The specified file '$1' does not exist."
exit 1
fi
}
# Check if API key is set
check_api_key
# Check if 'jq' is installed
check_jq_installed
# Get the code from the file provided as the argument
CODE_FILE=$1
check_input_file "$CODE_FILE"
MESSAGE=$(cat "$CODE_FILE")
# Set up the API endpoint
API_ENDPOINT="https://api.openai.com/v1/chat/completions"
# Set up the JSON request body
JSON=$(jq -n --arg msg "$MESSAGE" '{"model": "gpt-4", "messages": [{"role": "system", "content": "You are a senior software developer and a hacker."}, {"role": "system", "content": "Find bugs, syntax errors, and critical security issues only in this code and give a clear and simple explanation in spanish using a numbered list. Also explain how would you break this code."}, {"role": "system", "content": "Then categorize the final analysis from 1 to 10 like this: [code_quality=8], where 1 is very unsecure code and 10 is very secure." }, {"role": "system", "content": "The source code: \($msg)"}]}')
echo ""
echo "Parsing file $1, wait a moment ... "
echo ""
# 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 error if exists
RESPONSE_ERROR=$(echo "$RESPONSE" | jq -r '.error.message')
# Extract the text from the response
RESPONSE_TEXT=$(echo "$RESPONSE" | jq -r '.choices[].message.content')
# If RESPONSE_ERROR not equal "null", print the error and exit else continue
if [[ "$RESPONSE_ERROR" != "null" ]]; then
echo ""
echo "Error: $RESPONSE_ERROR"
echo ""
exit 1
fi
# If RESPONSE_TEXT is not empty, print the response text else exit
if [[ ! -z "$RESPONSE_TEXT" ]]; then
# Print the response text to the console
echo ""
echo "----- Response -----"
echo ""
echo "$RESPONSE_TEXT" | fold -w 100 -s
echo ""
echo "----- Response -----"
echo ""
else
echo ""
echo "Error: No response received from the API."
echo ""
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment