Action

GPT Toy

Posted by pborenstein, Last update about 1 year ago

UPDATES

about 1 year ago

Questions? I’m pborenstein pretty much everywhere, including gmail. Find me on Mastodon at @pborenstein@mastodon.social

A toy to play with OpenAI’s GPT-3 in your favorite app.

Takes the selected text (or the entire draft) as a prompt (input) for GPT-3’s completion endpoint and appends the result to the draft.

If there is selected text, the result includes the original prompt.

A video of GPT generating a limerick

I’m pborenstein almost everywhere including gmail.

Steps

  • script

    //  GPT Toy -- a toy for the OpenAI GPT-3 API
    //  If no selection, the toy uses the entire draft.
    //  If there is a selection, the toy uses the selection.
    
    //  Credentials
    //    This script requires a GPT API key.
    //    You can get one at https://beta.openai.com/
    
    const credential = Credential.create('GPTKey', "Credentials for a GPT toy");
          credential.addPasswordField('gptKey', "GPT API key");
          credential.authorize();
    const gptKey = credential.getValue('gptKey');
    
    /****
     *          OpenAI API
     ****/
    
    const gptURL = 'https://api.openai.com/v1/completions'
    const http = HTTP.create(); // create HTTP object
    
        //     POST https://api.openai.com/v1/completions
    
    const getCompletion = (prompt, echo) => {
      const response = http.request({
        "method":       'POST',
        "url":          gptURL,
        "headers":      {
            "Authorization": `Bearer ${gptKey}`
        },
        "data": {
            "model": "text-davinci-003",
            "prompt": prompt,
            "max_tokens": 16,
            "temperature": 0.2,
            "top_p": 1,
            "n": 1,
            "stream": false,
            "logprobs": null,
            "echo": echo
        }
      })
      console.log(`===> ${JSON.stringify(response.responseData, null, 2)}`)
    
      if (response.success)
        return response.responseData.choices[0].text
      else
        return `###### ${response.statusCode} -- ${response.error}`
    }
    
    
    /****
     * getPrompt()
     * 
     *    Get the prompt from the draft or the selection
     *    If there is a selection, then echo the prompt
     *    Returns an array with the prompt and whether to echo it
     ****/ 
    
    const getPrompt = () => {
      let echoPrompt = false
      let p = editor.getSelectedText()
    
      if (p)
        echoPrompt = true
      else
        p = draft.content
    
        p = p.trim()
      return [p, echoPrompt]
    }
    
    
    /****
     *   Main
     ****/
    
    let   [p, echo] = getPrompt()
    const response  = getCompletion(`${p}`, echo)
    
    draft.saveVersion() // save the draft, just in case
    
      //  add a separator between the prompt and
      //  the response when echoing the prompt
    
      if (echo) {
        draft.append(`\n---`)
      }
    
    draft.append(response.trim())
    draft.addTag('gpt')
    draft.update()
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.