// Replace YOUR_API_KEY with your actual API key
var API_KEY = 'YOUR_API_KEY';

/**
* Returns the response from the GPT-3 API for the given prompt
* Full credit to ChatGPT that created this script on my behalf ;-)
*  
*/
function GPT(prompt,max_tokens = 300) {
 // Set up the API URL and payload
 var API_URL = 'https://api.openai.com/v1/completions';
 var payload = {
   'prompt': prompt,
   'model': 'text-davinci-003',
   'max_tokens': max_tokens,
   'top_p': 1,
   'frequency_penalty': 0,
   'presence_penalty': 0
 };

// Make the API request
 var response = UrlFetchApp.fetch(API_URL, {
   'method': 'post',
   'headers': {
     'Content-Type': 'application/json',
     'Authorization': 'Bearer ' + API_KEY
   },
   'payload': JSON.stringify(payload)
 });

// Parse the response and return the generated text
 var responseText = response.getContentText();
 var responseJson = JSON.parse(responseText);
 return responseJson['choices'][0]['text'];
}

function getAvailableModels() {
 // Set up the API URL
 var API_URL = 'https://api.openai.com/v1/models';

// Send the request to the API
 var response = UrlFetchApp.fetch(API_URL, {
   method: 'GET',
   headers: {
     'Content-Type': 'application/json',
     'Authorization': 'Bearer ' + API_KEY
   }
 });

// Parse the response and return the list of available models
 var data = JSON.parse(response.getContentText());
 var models = data.data.map(model => model.id);
 Logger.log('Available models: ' + models.join(', '));
 return models;
}