Using ChatGPT in Google Sheets

A quick easy guide on how to write an App Script function in Google Sheet to call OpenAI’s ChatGPT to return the classification or main reason behind the comments in users’ reviews.

Create a sheet in Google Sheet (as above) with Review Comments in Column A. Cell C1 is a comma separated list of the classification you want ChatGPT to respond with when it reads through all the comments in Column A. Column B hold the cells that call the App Script function ‘callchatGPT’. The formula is as follows:

=callchatGPT("Respond with the main reason from choices in '"&$C$1& "' for this product review: '"&A2&"'")

From the Extensions menu, create an App Script, give it a name, and paste in the function below:

function callChatGPT(input) {
  var response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", {
    "method": "post",
    "headers": {
      "Content-Type": "application/json",
      "Authorization": "Bearer YOUR-SCERET-KEY"
    },
    "payload": JSON.stringify({
      "prompt": input,
      "model": "text-davinci-003",
      "max_tokens": 250,
      "temperature": 0.4
    })
  });
  var json = response.getContentText();
  var data = JSON.parse(json);
  return data.choices[0].text;
}

In Open AI, get a Secret Key from here – https://beta.openai.com/account/api-keys. Copy key to clipboard and insert it into the code after the Bearer as above.

Save the script, and return to your sheet. Wait for ChatGPT to respond …

Note: if you have a free account, you will be limited to around 20 queries/calls per minute.

Watch the guide in action on YouTube here: https://www.youtube.com/watch?v=b3sy01YmRF4

Leave a Reply

Your email address will not be published. Required fields are marked *