Skip to content

Instantly share code, notes, and snippets.

@SourcingDenis
Created March 14, 2023 09:21
Show Gist options
  • Save SourcingDenis/8166e1ade3d51881ec0811ff49010bb9 to your computer and use it in GitHub Desktop.
Save SourcingDenis/8166e1ade3d51881ec0811ff49010bb9 to your computer and use it in GitHub Desktop.
var API_KEY = 'YOUR_API_KEY';
function runGPT() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange('A1:B1').getValues();
var cell1 = range[0][0];
var cell2 = range[0][1];
var prompt = cell1 + ' space ' + cell2;
var API_URL = 'https://api.openai.com/v1/completions';
var payload = {
'prompt': prompt,
'model': 'text-davinci-003',
'max_tokens': 300,
'top_p': 1,
'frequency_penalty': 0,
'presence_penalty': 0
};
var response = UrlFetchApp.fetch(API_URL, {
'method': 'post',
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + API_KEY
},
'payload': JSON.stringify(payload)
});
var responseText = response.getContentText();
var responseJson = JSON.parse(responseText);
var output = responseJson['choices'][0]['text'];
sheet.getRange('C1').setValue(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment