To create a shortcut that helps with chatting with an AI, particularly if you're thinking of integrating it with something like ChatGPT, here’s a comprehensive guide. This shortcut will allow you to input a query and receive a response from an AI via a REST API, like the one provided by OpenAI.
Step 1: Create the Basic Structure of the Shortcut
- Open the Shortcuts app on your iOS device.
- Tap the "+" button in the top right corner to create a new shortcut.
- Tap "Add Action" to start building your shortcut.
- Search for "Ask for Input" in the Actions list.
- Add the "Ask for Input" action.
- Set the Prompt to something like "What would you like to ask the AI?".
- Optionally, set the Input Type to "Text".
Step 3: Prepare the API Request
- Add the "Get Contents of URL" action (Search for it if necessary).
- In the URL field, enter the endpoint for the AI’s API (for example,
https://api.openai.com/v1/chat/completions
for OpenAI’s API). - Tap on "Method" and set it to POST since you'll be sending data to the API.
- Tap "Add New Field" under the "Get Contents of URL" action.
- Select "Headers".
- Add a header for "Authorization".
- Set the Value to your API key, formatted as
Bearer YOUR_API_KEY
.
Step 5: Configure the API Body
- Tap "Add New Field" again, this time select "Request Body".
- Change the Body Type to "JSON".
- Set the following keys and values in the JSON body:
"model"
: "gpt-4"
, or whatever model you are using."messages"
: A dictionary containing your input.
"role"
: "user"
."content"
: Magic Variable from Ask for Input (Use this to insert the user’s question into the API request).
- After the "Get Contents of URL" action, add the "Get Dictionary Value" action.
- Set "Get Value for Key" to
"choices"
.
- Add "Get Dictionary Value" again to drill down into the first response choice.
- Set "Get Value for Key" to
"message"
.
- Add "Get Dictionary Value" again to get the actual text.
- Set "Get Value for Key" to
"content"
.
Step 7: Display the AI’s Response
- Add "Show Result" action.
- Set the Text to the Magic Variable representing the AI’s response content.
Step 8: Optional - Logging the Conversation
- If you want to keep a log of all your conversations, add an "Append to Note" or "Save File" action.
- You could format the entry as:
You asked: [Your Question]
AI responded: [AI’s Response]
- Store the log in a specific note or a file in iCloud.
Step 9: Run and Test the Shortcut
- Name your shortcut something intuitive like "Chat with AI".
- Test it by entering a question when prompted.
- The shortcut should display the AI's response and optionally log the interaction.
This shortcut gives you a robust tool for interacting with AI directly from your device, integrating API calls, and handling responses, all within the iOS Shortcuts framework. If desired, you can extend it by adding more options like choosing the AI model, setting temperature parameters, or managing API tokens.