Skip to content

Instantly share code, notes, and snippets.

@iosifnicolae2
Created May 17, 2023 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iosifnicolae2/ec6eda8dfcc36e969bfbb8fa5e42133e to your computer and use it in GitHub Desktop.
Save iosifnicolae2/ec6eda8dfcc36e969bfbb8fa5e42133e to your computer and use it in GitHub Desktop.
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