Created
February 12, 2024 18:46
-
-
Save jebberjeb/61cbbb771ae5bbd53991292c78117bab to your computer and use it in GitHub Desktop.
OpenAISheets.gs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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