Created
May 17, 2023 07:36
-
-
Save iosifnicolae2/ec6eda8dfcc36e969bfbb8fa5e42133e to your computer and use it in GitHub Desktop.
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
const apiUrl = 'https://api.openai.com/v1/chat/completions'; | |
function CHATGPT(prompt, model, apiKey) { | |
if(!prompt || prompt === "") { | |
return "No prompt provided"; | |
} | |
if(!model || model === "") { | |
return "No model provided"; | |
} | |
if(!apiKey || apiKey === "" || apiKey === "XXXXXXXXXXXXXXXXXXXX") { | |
return "No apiKey provided, check ChatGPT configuration sheet."; | |
} | |
var payload = { | |
messages: [{"role": "user", "content": prompt}], | |
model: model, | |
temperature: 0.7, | |
}; | |
var options = { | |
method: 'POST', | |
headers: { | |
'Authorization': 'Bearer ' + apiKey, | |
'Content-Type': 'application/json', | |
}, | |
payload: JSON.stringify(payload), | |
}; | |
var response = UrlFetchApp.fetch(apiUrl, options); | |
if (response.getResponseCode() == 200) { | |
var jsonResponse = JSON.parse(response.getContentText()); | |
var result = jsonResponse.choices[0].message.content.trim(); | |
return result; | |
} else { | |
return 'Error: ' + response.getContentText(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment