Using OpenAI-ChatGPT with Drafts

While Drafts does not have any specific features built on integration with OpenAI’s artificial intelligence platform, it does provide scripting tools to make it easier to integrate with the OpenAI API. This provides a flexible way to incorporate ChatGPT and other OpenAI tools, like text completions, into your Drafts actions.

This article covers the set required to use such actions, as well as providing useful example actions which may provide value out-of-the-box for your workflows, or be a starting point for your own variations.

Setting Up OpenAI API Access

In order to use any of these integrations, you will need to create your own OpenAI account and generate an API key to use with Drafts. If you already have an OpenAI account (needed to use their own web-based tools), then great! If not, start by signing up for a free account.

OpenAI API access can be used as a free trial, but it is extremely limited. We recommend you visit your platform account’s billing page and configure paid account information if you plan to use these actions, or you will quickly run into rate limits of the free trial. You will also find that it is extremely inexpensive for an individual user, and you are only charged for usage; there is no repeating monthly cost.

Note about API cost: If you are worrying about racking up API costs, know that we have been developing and testing these features for weeks and have only run up a bill for a few cents in our OpenAI account

Lastly, visit your account’s API Keys page and “Create a new secret key” for use with Drafts. When you create this key, copy it to the clipboard, then return to Drafts. The first time you run an action that uses OpenAI, you will be prompted to enter this key.

Drafts will remember the API key in its Credentials system, so you will only need to complete this step once. If you need to change the API key used, you can forget it in the Credentials pane in Drafts settings, and the next time you use an OpenAI action, you will be prompted to enter a new key.

Example Actions

Below are a few example use-case actions that are meant as a starting point to demonstrate some ways OpenAI integration can be used in Drafts:

  • Ask ChatGPT: This action will ask you to enter a text prompt for ChatGPT, and insert the result in the current draft.
  • ChatGPT: Converse: Allows for ongoing conversation in a single draft, by taking lines without a prefix as prompts and Markdown quotes are responses from the AI.
  • OpenAI: Translate Selection: Take the selected text in the editor, and ask ChatGPT to translate it into another language. You will be prompted to select from a list of languages.
  • ChatGPT: Modify Selection: Select text in a draft, then run this action and you will be prompted for an instruction to transform the text. Drafts will package that up in a prompt and replace the selected text with the result. You can do simple things like “uppercase”, but also combine commands for things like “uppercase and insert a :tada: emoji between each word”.

Scripting with OpenAI

If you wish to build your own more advanced integrations, the OpenAI script object is your starting point.

This object provides a convenience wrapper for making API calls to the OpenAI API. When making requests with this object, Drafts will take care of requesting and storing a user’s API key, providing the appropriate authentication headers, and parsing results into Javascript objects.

The object provides several additional simple request functions, like quickChatResponse that abstract details about the API for simple use cases, but also the request function to build more detailed requests with all the API options. Refer to OpenAI API documentation on request parameters and return values.

The below action is meant as a starting point and demonstrates the use of the request function. Get more examples in the scripting reference:

Alternate OpenAI Hosts, Like Perplexity.ai

The OpenAI scripting object defaults to working with the OpenAI API hosted at api.openai.com. It is possible to override the host information, and work with other API endpoints that are OpenAI API-compatible. Notably, Perplexity.ai, which provides a number of other large language model options. We have posted an example to get you started:

  • Ask Perplexity.ai: Similar to “Ask ChatGPT” example above, but using Perplexity’s API. On first run, you will be prompted for your Perplexity.ai API key.

Using these features simply requires overriding the host property of the OpenAI object, and either passing a valid API key for the service, or setting a unique credentialIdentifier to allow Drafts to request an API key from the user. See the example and OpenAI object documentation for details.

Troubleshooting

If you run into issues running these actions, be sure to check the Action Log for detailed error messages. Common problems include rate limits, or improperly configured API keys.

Conclusion

If you create new and interesting actions with this functionality, we hope you’ll share them in the directory and forums!

9 Likes

This is awesome, thank you!

Is there a way to ask OpenAI to act on or analyze text that’s currently in a draft you’re working on? For example if you wrote something out then wanted OpenAI to summarize it for you or something, is that possible? Or would you just need to copy+paste the text you’re wanting it to analyze into the prompt window?

You can submit any prompt you want. An action could be written to work on text in the draft (as the translate/spelling examples above), or based on input from elsewhere - like the “Ask” example, which prompts you for the input. It’s just a matter of writing a script for it. Drafts is pretty flexible in this manner.

But, yes, just with what’s available in the example, you could paste into the “Ask ChatGPT” example something like Summarize the text "-paste your content-" and get a result.

Okay cool, sounds great. Thanks!

This doesn’t seem to work for me. I generated a new API Key, installed the Ask ChatGPT action and ran it. But instead of asking for the API key it just generates a script error. Any thoughts?

Did you install the v39 update?

1 Like

Dammit! The app store showed me as up to date. But I force quit and pulled up Drafts again and now it let me update. Dumb. Thanks.

1 Like

This is super cool! I was using the “ChatGPT Conversation” action that’s working well. I patched it to be able to change the base model to gpt-4 because I have access to it. Was wondering if/how it would be possible to expose the same level of settings for the whole OpenAI integration or should I instead write my own action to use the full API instead of the short one?

You can pass options to the convenience quickChatResponse function, including a model value. Here’s an example targeting gpt-4. I didn’t publish that as an example because most people do not have access to that model at this time.

You can, of course, also use the request function to make calls to /chat/conversation as in the other example above passing any options supported by the API.

2 Likes

Can I customize Openai domain URL. Because the Openai official domain is blocked in China. I have use Cloudflare’s service to proxy it.

I suppose this is an API issue, but the Ask ChatGPT action only accepts prompts in English (but you can ask for the answer to be in Spanish). If you write the prompt in Spanish you get a “no reply received” answer.

I have been able to use Spanish in testing, and get a response in Spanish - it may just not be able to answer your specific prompt, or another error is occurring, which might be logged in the action log.

I had the same d’oh! moment!

Thanks! I’ve tried again and I’m receiving results. Perhaps it was a temporary glitch.

What would be a better approach to passing text from another action to OpenAI? Within the same script, part of Include Action steps (separate script) within an action, or as clipboard?

Scenario: RegEx based Action set parses text and then needs to feed it to OpenAi to insert the final output into drafts.

The scripting interfaces are pretty straightforward. If you dig in the example actions above or sample code on the scripting reference. If you are already scripting a regex-based action, then you just need to construct your prompt in a Javascript string and send it to OpenAI.

The translate and check spelling examples above demonstrate taking text from the editor and replacing it with the result.

1 Like

The OpenAI object does not support customizing the domain. You could use HTTP requests and make whatever calls you want to connect to the API, nothing preventing that.

Thank you, awesome! Can the AI access multiple drafts? I’d like to have a crack at creating a script that can help tidy, tag and organise drafts and tags etc…

Not sure exactly what you have in mind. Actions can query and work with any of your drafts, however, so you can certainly process in bulk.

This works great! Is there a way can I create a keyboard shortcut to run the Ask ChatGPT script in the Drafts app on my Mac, even when I’m using another application like Microsoft Word? I’d like to be able to trigger the script with a hotkey while working in other apps. Maybe like using an AppleScript to run the action?