Evergreen Note: If you have not reviewed the Google Apps Script primer, then do that now. We will not cover the basics for accessing the service etc.
Take Me to The Primer Baby! ~ G. Constanza
Google Apps Script | Protect Global Variables and API Keys with Script Properties
We are going to look at adding global variables, and functions to call those variables, without exposing them directly in the script. This is very helpful for working with API keys etc.
After you follow the guide above, you need to build a function to grab the value.
In the editor, add a new file. I usually call this file “Constructors.gs”, but you can do whatever you want. I keep all these functions in one place, but you are free to place them anywhere in the environment.
Paste this code in, and repalce it with your Script Property Name. My script propery name is “AI_MODEL_TWO”.
function getAIModel() {
try {
const scriptProperties = PropertiesService.getScriptProperties();
const sheetId = scriptProperties.getProperty('AI_MODEL_TWO');
return sheetId;
} catch (err) {
console.log(`Failed getting property "AI_MODEL": ${err.message}`);
return null;
}
}
Now, to use the value within your program, you can try and declare it directly, but I normally assign it to a variable. I am using “const” to keep the value secure and locked.
const myAITemp = getAITemp();
const myAIModel = getAIModel();
const myAITokens = getAITokens();
const myPropValue = myKey();
var prompt = position + "\n" + myData;
var model = myAIModel;
var temperature = myAITemp;
var maxTokens = myAITokens;
...
When I want to assign the value, I would use “model”.
var requestBody = {
"model": model,
"temperature": temperature,
"max_tokens": maxTokens,
"messages": [
{
"role": "system",
"content": theControl
},
{"role": "user", "content": prompt}
]
};
I know this seems like too many extra steps, but, once you build a constructor file, you can easily resuse it and the other structure.
This is the best way I have found lock in API keys and other values that need to remain static and private.