How to use OpenAI with Apple Shortcuts

In this post I'll explain how to create an Apple Shortcut integrated with OpenAI's GTP-3. If you don't want to read the entire post and just want to use the shortcut, you can click here to download it.

The goal of the shortcut will be: I want to ask a question and get an answer, which will be a basic http request-response.

đŸŽĨ Here is a short demonstration of how to use the shortcut on an iPhone:

  • Apple Shortcuts app installed on your iPad, iPhone or Mac.
  • An OpenAI API Secret Key

To create an API Secret, just go to: beta.openai.com/account/api-keys and then click on + Create new secret key. Copy the secret and paste it somewhere safe for now.

A quick overview on the Apple Shortcut - what do we need / the steps:

1. On Setup

ℹī¸ Questions added on the setup step will be asked when the shortcut is imported on a different device

Setup questions are only if you want to share the shortcut with someone else. Skip this step if you don't have any plans to share your shortcut.

2. Ask for input - the question we want to ask.

3. Build the prompt text

4. Request OpenAI API

5. Parse the response.

6. Show the response.

The first thing we need is to define the API Token. For that we'll use a text and set variable components.

After that, we'll need to request user input. This will be the prompt - the question or message we'll send to OpenAI API. Apple Shortcut automatically creates a "variable" for this kind of input, but I prefer to create my own variables to avoid confusion. Just drag and drop a "set variable", define the name and set the value as Provided Input.

Now that we have the input, let's include the request part. In this step, first we need to define which URL are we going to request, the method, headers and JSON body.

The base URL that we are going to use is https://api.openai.com/v1/completions.

🧐
More info: beta.openai.com/docs/api-reference/completions

Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.

Include the two required headers: Content-type: application/json and Authorization: Bearer $token, where $token is the variable value we defined on the first step.

We also need to prepare the JSON body and for that we'll include the following parameters:

  • model, as Text/String: "text-davinci-003".
  • prompt, as Text/String: the provided input variable
  • max_tokens, as Number: 3000
  • temperature, as Number: 0
  • top_p, as Number: 1
❗ī¸
If you get "api secret is not defined" error, make sure there are no spaces around your header key definition.

If you don't want to create it, you can easly download the shortcut by clicking here.

Apple Shortcuts is a powerful tool, but it is still quite buggy. It crashes frequently, especially when working on more complex shortcuts. Additionally, some simple tasks that can be easily done on mobile require searching through documentation and tutorials to figure out how to do them on MacOS, such as inserting a variable into text (the answer is simply a right-click).

You might need some coding skills (specially if are working with http requests or the scripting part (conditionals, loops)) - but nothing advanced.