Created
January 11, 2023 02:41
-
-
Save marcodulog/fc431f9a5fa7b861b69c60e2d56753b7 to your computer and use it in GitHub Desktop.
Google App Script functions to call openai
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
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