Skip to content

Instantly share code, notes, and snippets.

@jebberjeb
Created February 12, 2024 18:46
Show Gist options
  • Save jebberjeb/61cbbb771ae5bbd53991292c78117bab to your computer and use it in GitHub Desktop.
Save jebberjeb/61cbbb771ae5bbd53991292c78117bab to your computer and use it in GitHub Desktop.
OpenAISheets.gs
var API_KEY = "<your api key>"
function NUGPT(instructions, prompt, model) {
var payload = {
'model': (model || "gpt-3.5-turbo-0125"),
'messages': [
{
'role': 'system',
'content': instructions
},
{
'role': 'user',
'content': prompt
}
]
};
var options = {
"method": "post",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer " + API_KEY
},
"payload": JSON.stringify(payload)
};
var response = UrlFetchApp.fetch("https://api.openai.com/v1/chat/completions", options);
// TODO handle situation where multiple choices aren't silently discarded
var responseData = JSON.parse(response.getContentText());
return responseData.choices[0].message.content;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment