Skip to content

Instantly share code, notes, and snippets.

@nimeshkasun
Last active December 27, 2023 22:48
Show Gist options
  • Save nimeshkasun/2b3aca02c748f73da4bf3d63f5aa5fd9 to your computer and use it in GitHub Desktop.
Save nimeshkasun/2b3aca02c748f73da4bf3d63f5aa5fd9 to your computer and use it in GitHub Desktop.
This scripts allows to integrate OpenAI GPT-3 into Google Spreadsheets.
const SECRET_KEY = "ENTER YOUR SECRET KEY HERE TAKEN FROM OPEN_AI";
// Follow on LinkedIn: https://www.linkedin.com/in/nimeshkasun
/**
* Completes your prompt with GPT-3
*
* @param {string} prompt Your prompt for OpenAI
* @param {number} precision (Optional) Precision. 1 is super creative while 0 is very exact and precise. Defaults to 0.4.
* @param {string} model (Optional) GPT-3 Model to use. Defaults to "text-ada-001".
* Models:
* * Ada model (Fastest) - text-ada-001
* * Babbafe model - text-babbage-001
* * Curie model - text-curie-001
* * Davinci (Most powerful) - text-davinci-003
* @param {string} max_tokens Defines number of maximun tokens to be user per request. Defaults to 200.
* @return Completion returned by GPT-3
* @customfunction
*/
function OPEN_AI(prompt, precision = 0.4, model = "text-ada-001", max_tokens = 200) {
const url = "https://api.openai.com/v1/completions";
const payload = {
model: model,
prompt: prompt,
temperature: precision,
max_tokens: max_tokens,
};
const options = {
contentType: "application/json",
headers: { Authorization: "Bearer " + SECRET_KEY },
payload: JSON.stringify(payload),
};
const res = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
return res.choices[0].text.trim();
}
/**
* Classifies an item into a fixed set of categories
* @param {range} category_list Set of categories
* @param {range} category_value_list Set of respective category values
* @param {string} input Category to classify
* @param {range} rules (Optional) Set of rules written in plain text
* @return Completion returned by GPT-3
* @customfunction
*/
function OPEN_AI_CATEGORIZE(category_list, category_value_list, input, rules = []) {
const prompt = "The available categories are " + category_list.map((c) => `"${c}"`).join(", ") + ". The respective values are " + category_value_list.map((c) => `"${c}"`).join(", ") + ". " + rules.join(". ") + "The value for the category '" + input + "' is ";
console.log(prompt);
const completion = OPEN_AI(prompt, 0, "text-ada-001", 200);
// Replace "s and .s at the start and end of the string
return completion.replace(/^"/g, '').replace(/["|.]{0,2}$/, '');
}
@nimeshkasun
Copy link
Author

@vaniljs
Copy link

vaniljs commented Dec 27, 2023

Tell me, how can I solve the error of limiting the response rate?
Exception: Request failed for https://api.openai.com returned code 429. Truncated server response: { "error": { "message": "Rate limit reached for text-davinci-003 in organization ... on requests per min (RPM)... (use muteHttpExceptions option to examine full response) (line 33).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment