Skip to content

Instantly share code, notes, and snippets.

@Webmasterei
Created January 23, 2023 19:59
Show Gist options
  • Select an option

  • Save Webmasterei/6019ac9fb4dab2231c82d8d09cab2036 to your computer and use it in GitHub Desktop.

Select an option

Save Webmasterei/6019ac9fb4dab2231c82d8d09cab2036 to your computer and use it in GitHub Desktop.
/**
* Returns a text Choice from Chat GPT.
*
* @param {string} instruction of what to do.
* @param {url} url for what page.
* @param {max_tokens} max_tokens how many API tokens to use max.
* @param {temperature} temperature set the temperature.
* @return Returns a text Choice from Chat GPT
* @customfunction
*/
function CHATGPT(prompt,max_tokens,temperature,apikey) {
// add your apiKey from OpenAi here. Found at: https://beta.openai.com/account/api-keys
// increase temperature for more randomness, and variations of the output. Max tokens set at 240 characters (could be lower).
// using Keyword: as a stop sequence so that it only generates one result.
const parameters = {
'prompt': prompt,
'max_tokens': max_tokens,
'temperature': temperature,
'stop': '["Keyword:"]',
'model':'text-davinci-003'
};
Logger.log(parameters)
const options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(parameters),
'headers': {
Authorization: 'Bearer ' + apikey,
},
};
// fetch from GPT-3 - Engines can be changed. eg. https://api.openai.com/v1/engines/curie/completions
// worth exploring each individual Engine in more detail here:https://beta.openai.com/docs/engines
const response = UrlFetchApp.fetch('https://api.openai.com/v1/completions', options,);
Logger.log(response)
var responseText = JSON.parse(response.getContentText())['choices'][0]['text']
responseText = responseText.replace("$.*\n","").replace("$\n","")
Logger.log(responseText)
return responseText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment