Posted by pborenstein, Last update over 1 year ago


over 1 year ago

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

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.


  • 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
    const credential = Credential.create('GPTKey', "Credentials for a GPT toy");
          credential.addPasswordField('gptKey', "GPT API key");
    const gptKey = credential.getValue('gptKey');
     *          OpenAI API
    const gptURL = ''
    const http = HTTP.create(); // create HTTP object
        //     POST
    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
        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
        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) {


  • 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.