#!/usr/bin/env bash
# License: GPLv3
# Credits: Felipe Facundes

: <<'DOCUMENTATION'
This Bash script creates an interactive chatbot using OpenAI's GPT-3.5-turbo model. 
It initializes several parameters, including the API key, endpoint URL, prompt, 
maximum tokens, temperature, top-p, frequency penalty, and presence penalty. 
The script enters an infinite loop, prompting the user for input and appending it to the conversation history. 
It constructs a JSON payload for the API request and sends it to the OpenAI endpoint using 'curl'. 
The generated response from the model is extracted and displayed as the chatbot's reply. 
The loop continues until the user inputs "sair" or leaves the input empty, at which point the script terminates with a farewell message. 
Overall, it provides a simple interface for engaging with the GPT model in a conversational manner.
DOCUMENTATION

# Define the API_KEY variable with the value of your access key to the gpt-3.5-turbo model
API_KEY="OpenAI API key Here"

# Define the ENDPOINT variable with the value of the chat gpt service URL
ENDPOINT="https://api.openai.com/v1/chat/completions"

# Define the PROMPT variable with the value of the initial chat text
PROMPT="Hello, I am a chatbot based on the gpt-3.5-turbo model."

# Define the MAX_TOKENS variable with the value of the maximum number of tokens that the model can generate per response
MAX_TOKENS=100

# Define the TEMPERATURE variable with the value of the temperature parameter that controls the randomness of generation
TEMPERATURE=0.9

# Define the TOP_P variable with the value of the top-p parameter that controls the cumulative probability of candidate tokens
TOP_P=0.95

# Define the FREQUENCY_PENALTY variable with the value of the frequency_penalty parameter that penalizes repeated tokens
FREQUENCY_PENALTY=0.5

# Define the PRESENCE_PENALTY variable with the value of the presence_penalty parameter that penalizes new tokens that have already appeared
PRESENCE_PENALTY=0.5

# Define the STOP variable with the value of the tokens that indicate the end of the response
STOP="\n\n"

# Start an infinite loop to interact with the chat gpt
while true; do

  # Print the prompt on the screen
  echo -e "$PROMPT"

  # Read the user's input and store it in the USER_INPUT variable
  read -p "You: " USER_INPUT

  # Check if the user's input is empty or equal to "exit"
  if [ -z "$USER_INPUT" ] || [ "$USER_INPUT" == "exit" ]; then
    # Exit the loop and terminate the script
    break
  fi

  # Add the user's input to the prompt, followed by a newline
  PROMPT="$PROMPT\nYou: $USER_INPUT\n"

  # Create a JSON_DATA variable with the request data to the chat gpt service
  JSON_DATA=$(jq -n \
    --arg prompt "$PROMPT" \
    --argjson max_tokens "$MAX_TOKENS" \
    --argjson temperature "$TEMPERATURE" \
    --argjson top_p "$TOP_P" \
    --argjson frequency_penalty "$FREQUENCY_PENALTY" \
    --argjson presence_penalty "$PRESENCE_PENALTY" \
    --arg stop "$STOP" \
    '{prompt: $prompt, max_tokens: $max_tokens, temperature: $temperature, top_p: $top_p, frequency_penalty: $frequency_penalty, presence_penalty: $presence_penalty, stop: $stop}')

  # Make a POST request to the chat gpt service using the API_KEY and JSON_DATA and store the response in the RESPONSE variable
  RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $API_KEY" -d "$JSON_DATA" "$ENDPOINT")

  # Extract the text generated by the model from the response and store it in the GENERATED_TEXT variable
  GENERATED_TEXT=$(echo "$RESPONSE" | jq -r '.choices[0].text')

  # Add the generated text to the prompt, preceded by "Chatbot: "
  PROMPT="$PROMPT\nChatbot: $GENERATED_TEXT\n"
done

# Print a farewell message on the screen
echo "See you later!"