For software development and maintenance, contact me at contact@appsoftware.com or via appsoftware.com


Demonstrating ChatGPT data mining and querying capabilities via the OpenAI API and bash scripts

Wed, 29 Mar 2023 by garethbrown

Inspired by the post become a 1000x engineer or die tryin', this bash script which you can add to your .bashrc file creates a wrapper around the OpenAI API to facilitate fast querying of ChatGPT, and showing how you can have ChatGPT both produce and query data sets.

The original post is the most impressive use of OpenAI's technologies to date, and using simple wrapper scripts to make using them in day to day workflows simple and quick.

The following scripts issue queries against ChatGPT 3.5. ChatGPT 4 has a waitlist for API access at present.

First here is the .bashrc script.

# Set OpenAI key

export OPENAI_API_KEY="your-open-api-key-goes-here"

gpt_query() {

  # Check for an argument	

  if [ -z "$1" ]; then
    echo "Please provide a query."
    return 1
  fi

  # Store the argument in variables

  query="$1"

  # JSON escape the query argument and strip leading / trailing quotes
  # as will may otherwise break JSON string when inserted into curl command.

  query=$(echo $query | jq -Rsa .)
  query=$(echo ${query:1:-1})

  # Issue curl command to Chat GPT and save in response

  response=$(curl https://api.openai.com/v1/chat/completions -s -X POST \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -d "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"$query\"}], \"temperature\": 0.7}")

  # Output the response. In case of need to debug, echo out $response instead of $answer

  answer=$(echo "$response" | jq -r '.choices[0].message.content')

  if [ -z "$answer" ]; then
    echo "An error occurred. Please try again."
    return 1
  else
    echo "$answer"
  fi
}

# Usage:

# gpt_query "your query here"

# Example: 

# gpt_query "How far is the sun from the earth?"

gpt_data_query() {

    # Check for an argument	

    if [ -z "$1" ]; then
      echo "Please provide a query."
      return 1
    fi	

    # Store the argument in variables	

    prompt="$1"
    data="$2"

    # JSON escape the prompt and data arguments and strip leading / trailing quotes
    # as will likely break JSON string when inserted into curl command.

    prompt=$(echo $prompt | jq -Rsa .)
    prompt=$(echo ${prompt:1:-1})	

    data=$(echo $data | jq -Rsa .)
    data=$(echo ${data:1:-1})
    
    # Format full query
    
    query=$(echo "$prompt: $data")
    
    response=$(curl https://api.openai.com/v1/chat/completions -s -X POST \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -d "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"$query\"}], \"temperature\": 0.7}")
    
    answer=$(echo "$response" | jq -r '.choices[0].message.content')
    
    if [ -z "$answer" ]; then
      echo "An error occurred. Please try again."
      return 1
    else
      echo "$answer"
    fi
}

# Usage:

# gpt_data_query "your prompt here" "your data here"

# Example using gpt_query to request data generation and gpt_data_query for querying the data:

# gpt_query "Create a 10 row csv of NBA player data with headers - please only include the data, nothing else" > nba.csv

# gpt_data_query "Please calculate the average weight of all players with a position of SF from the following csv data" "$(< nba.csv)"

gpt_image() {

  # Check for an argument	

  if [ -z "$1" ]; then
    echo "Please provide a prompt."
    return 1
  fi

  # Store the argument in variables

  prompt="$1"

  # JSON escape the prompt argument and strip leading / trailing quotes
  # as will may otherwise break JSON string when inserted into curl command.

  prompt=$(echo $prompt | jq -Rsa .)
  prompt=$(echo ${prompt:1:-1})

  # Issue curl command to Chat GPT and save in response

  response=$(curl https://api.openai.com/v1/images/generations -s -X POST \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -d "{\"prompt\": \"$prompt\", \"n\": 1, \"size\": \"1024x1024\"}")

  # Output the response. In case of need to debug, echo out $response instead of $answer

  answer=$(echo $response | jq -r '.data[0].url')

  if [ -z "$answer" ]; then
    echo "An error occurred. Please try again."
    return 1
  else
    echo "$answer"
  fi
}

# Usage:

# gpt_image "your image prompt here"

# Example: 

# gpt_image "Roccoco painting of a dog sitting on a throne"

# Aliases for each of the above

alias gq="gpt_query"
alias gdq="gpt_data_query"
alias gi="gpt_image"

Using examples as described in the original article, we'll ask ChatGPT top create a small dataset around NBA player stats and write the output to a CSV file:

$ gpt_query "Create a 10 row csv of NBA player data with headers - please only include the data, nothing else" > nba.csv

Next we'll use that same data set that ChatGPT produced and write a natural language query to request the average weight of the players at the Small Forward position.

$ gpt_data_query "Please calculate the average weight of all players with a position of SF from the following csv data" "$(< nba.csv)"

Below is the terminal output for the above queries issued against ChatGPT 3.5. ChatGPT has accurately produced this data set and identified the two players at the small forward position (SF) and averaged their weight as requested.

Demonstration of terminal usage for outputting and inputting data for ChatGPT

Testing out the DALLĀ·E image generation API, I used the gpt_image bash function created above:

Demonstration of terminal usage for outputting images from DALL E

This responds with a URL that you can visit to view the image. This did not come out as brilliantly as I might of hoped, but it was better than I could do in the one or two seconds that it took to complete!

A pirate dancing on a diving board generated by DALL E


The information provided on this Website is for general informational and educational purposes only. While we strive to provide accurate and up-to-date information, we make no warranties or representations, express or implied, as to the accuracy, completeness, reliability, or suitability of the content, including code samples and product recommendations, presented on this Website.

The use of any information, code samples, or product recommendations on this Website is entirely at your own risk, and we shall not be held liable for any loss or damage, direct or indirect, arising from or in connection with the use of this Website or the information provided herein.
UI block loader
One moment please ...