Skip to content

Instantly share code, notes, and snippets.

@marcodulog
Created January 11, 2023 02:41
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 marcodulog/fc431f9a5fa7b861b69c60e2d56753b7 to your computer and use it in GitHub Desktop.
Save marcodulog/fc431f9a5fa7b861b69c60e2d56753b7 to your computer and use it in GitHub Desktop.
Google App Script functions to call openai
function sendEdit(strInput, strInstruction, bearerToken) {
/*
Function: sendEdit
Parms:
string strInput - get input data
string strInstruction - based upon the input data, perform the instruction
string bearerToken - OpenAI token
Purpose:
call the edit api
*/
const url = "https://api.openai.com/v1/edits"
const params = {
method: "post",
headers: {
Authorization: "Bearer " + bearerToken
},
contentType: "application/json",
payload: JSON.stringify({
"model": "text-davinci-edit-001",
"input": strInput,
"instruction": strInstruction
})
}
const res = UrlFetchApp.fetch(url,params);
const resp = JSON.parse(res.getContentText());
return resp.choices[0].text;
}
function sendCompletion(strInput, bearerToken){
/*
Function: sendCompletion
Parms:
string strInput - get input data
string bearerToken - OpenAI token
Purpose:
call the completion api
*/
const url = "https://api.openai.com/v1/completions"
const params = {
method: "post",
headers: {
Authorization: "Bearer " + bearerToken
},
contentType: "application/json",
payload: JSON.stringify({
"model": "text-davinci-003",
"prompt": strInput,
"max_tokens": 2048,
"temperature": 0
})
}
const res = UrlFetchApp.fetch(url,params);
const resp = JSON.parse(res.getContentText());
return resp.choices[0].text;
}
function setBearerToken(bearerToken){
/*
Function: setBearerToken
Parms:
string bearerToken - OpenAI token
Purpose:
set the bearerToken user property
*/
const userProperties = PropertiesService.getUserProperties();
userProperties.setProperty("bearerToken",bearerToken);
}
function getBearerToken(){
/*
Function: getBearerToken
Parms:
Purpose:
get the bearerToken user property
*/ const userProperties = PropertiesService.getUserProperties();
var retval = userProperties.getProperty("bearerToken");
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment