Skip to content

Instantly share code, notes, and snippets.

@abi
Last active December 30, 2024 18:54
Show Gist options
  • Save abi/94ffd06cf654038c75f56ff59ef2f086 to your computer and use it in GitHub Desktop.
Save abi/94ffd06cf654038c75f56ff59ef2f086 to your computer and use it in GitHub Desktop.

Revisions

  1. abi revised this gist Dec 5, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gpt3-in-your-sheets.js
    Original file line number Diff line number Diff line change
    @@ -8,11 +8,11 @@ const MAX_TOKENS = 200;
    *
    * @param {string} prompt Prompt
    * @param {number} temperature (Optional) Temperature. 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-davinci-002".
    * @param {string} model (Optional) GPT-3 Model to use. Defaults to "text-davinci-003".
    * @return Completion returned by GPT-3
    * @customfunction
    */
    function AI(prompt, temperature = 0.4, model = "text-davinci-002") {
    function AI(prompt, temperature = 0.4, model = "text-davinci-003") {
    const url = "https://api.openai.com/v1/completions";
    const payload = {
    model: model,
    @@ -40,7 +40,7 @@ function AI(prompt, temperature = 0.4, model = "text-davinci-002") {
    function CATEGORIZE(categories, input, rules=[]) {
    const prompt = "The available categories are " + categories.map((c) => `"${c}"`).join(", ") + ". " + rules.join(". ") + "The category for '" + input + "' is ";
    console.log(prompt);
    const completion = AI(prompt, 0, "text-davinci-002");
    const completion = AI(prompt, 0, "text-davinci-003");
    // Replace "s and .s at the start and end of the string
    return completion.replace(/^"/g, '').replace(/["|.]{0,2}$/, '');
    }
  2. abi revised this gist Nov 9, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gpt3-in-your-sheets.js
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,6 @@ function AI(prompt, temperature = 0.4, model = "text-davinci-002") {
    * @customfunction
    */
    function CATEGORIZE(categories, input, rules=[]) {
    // map((r) => r ? r.replace(/.$/, '') : '')
    const prompt = "The available categories are " + categories.map((c) => `"${c}"`).join(", ") + ". " + rules.join(". ") + "The category for '" + input + "' is ";
    console.log(prompt);
    const completion = AI(prompt, 0, "text-davinci-002");
  3. abi revised this gist Nov 9, 2022. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions gpt3-in-your-sheets.js
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@ const MAX_TOKENS = 200;
    * Completes your prompt with GPT-3
    *
    * @param {string} prompt Prompt
    * @param {number} temperature Temperature: 1 is super creative while 0 is very exact and precise. Defaults to 0.4.
    * @param {string} model GPT-3 Model to use. Defaults to "text-davinci-002".
    * @param {number} temperature (Optional) Temperature. 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-davinci-002".
    * @return Completion returned by GPT-3
    * @customfunction
    */
    @@ -31,10 +31,14 @@ function AI(prompt, temperature = 0.4, model = "text-davinci-002") {

    /**
    * Classifies an item into a fixed set of categories
    *
    * @param {range} categories Set of categories
    * @param {string} item Item to classify
    * @param {range} rules (Optional) Set of rules written in plain text
    * @return Completion returned by GPT-3
    * @customfunction
    */
    function CATEGORIZE(categories, input, rules=[]) {
    // map((r) => r ? r.replace(/.$/, '') : '')
    const prompt = "The available categories are " + categories.map((c) => `"${c}"`).join(", ") + ". " + rules.join(". ") + "The category for '" + input + "' is ";
    console.log(prompt);
    const completion = AI(prompt, 0, "text-davinci-002");
  4. abi revised this gist Nov 8, 2022. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gpt3-in-your-sheets.js
    Original file line number Diff line number Diff line change
    @@ -40,4 +40,6 @@ function CATEGORIZE(categories, input, rules=[]) {
    const completion = AI(prompt, 0, "text-davinci-002");
    // Replace "s and .s at the start and end of the string
    return completion.replace(/^"/g, '').replace(/["|.]{0,2}$/, '');
    }
    }

    // For more cool AI snippets and demos, follow me on Twitter: https://twitter.com/_abi_
  5. abi revised this gist Nov 8, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gpt3-in-your-sheets.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    const SECRET_KEY = ENTER YOUR SECRET KEY HERE;
    const MAX_TOKENS = 200;

    // For more cool AI snippets and demos, follow me on Twitter: https://twitter.com/_abi_

    /**
    * Completes your prompt with GPT-3
    *
  6. abi revised this gist Nov 5, 2022. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions gpt3-in-your-sheets.js
    Original file line number Diff line number Diff line change
    @@ -33,8 +33,9 @@ function AI(prompt, temperature = 0.4, model = "text-davinci-002") {
    * @customfunction
    */
    function CATEGORIZE(categories, input, rules=[]) {
    const prompt = "The available categories are " + categories.map((c) => `"${c}"`).join(", ") + "." + rules.join(".") + ". The category for '" + input + "' is ";
    const prompt = "The available categories are " + categories.map((c) => `"${c}"`).join(", ") + ". " + rules.join(". ") + "The category for '" + input + "' is ";
    console.log(prompt);
    const completion = AI(prompt, 0, "text-davinci-002");
    // Replace "s and .s at the start and end of the string
    return completion.replace(/^"/g, '').replace(/["|.]{0,2}$/, '');
    }
    }
  7. abi renamed this gist Nov 4, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. abi created this gist Nov 4, 2022.
    40 changes: 40 additions & 0 deletions gpt3-in-your-sheets
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const SECRET_KEY = ENTER YOUR SECRET KEY HERE;
    const MAX_TOKENS = 200;

    /**
    * Completes your prompt with GPT-3
    *
    * @param {string} prompt Prompt
    * @param {number} temperature Temperature: 1 is super creative while 0 is very exact and precise. Defaults to 0.4.
    * @param {string} model GPT-3 Model to use. Defaults to "text-davinci-002".
    * @return Completion returned by GPT-3
    * @customfunction
    */
    function AI(prompt, temperature = 0.4, model = "text-davinci-002") {
    const url = "https://api.openai.com/v1/completions";
    const payload = {
    model: model,
    prompt: prompt,
    temperature: temperature,
    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
    *
    * @customfunction
    */
    function CATEGORIZE(categories, input, rules=[]) {
    const prompt = "The available categories are " + categories.map((c) => `"${c}"`).join(", ") + "." + rules.join(".") + ". The category for '" + input + "' is ";
    const completion = AI(prompt, 0, "text-davinci-002");
    // Replace "s and .s at the start and end of the string
    return completion.replace(/^"/g, '').replace(/["|.]{0,2}$/, '');
    }