Integrating the OpenAI REST API into your iOS Swift application opens up a world of possibilities for natural language processing and AI-powered functionalities. In this step-by-step guide, we will walk you through the process of integrating the OpenAI REST API into your iOS Swift project. By following these steps, you’ll be able to harness the power of OpenAI’s advanced language models and create intelligent applications.

We will extract values from the below raw text using OpenAI’s API in iOS Application using Swift.

Below is an example of raw data:

Hi, I would like to order vegetables for four persons. Ryan wants three kilos of onions, half kilo tomatoes, a three-kilo pumpkin, a two-kilo watermelon, half a dozen lemons, and bananas. James wants five kilos of onions, two dozen bananas, a box of strawberries, and a big sized pumpkin. Sam wants half kilo detergent, two kilos of onions, four kilos of mango. Kevin wants three pieces of coriander, half kilo chilies and a packet of yogurt.

To obtain the desired output in JSON format, which includes the information about who wants what vegetables, follow these steps:

{
"Ryan": {

"onions": "3 kilos",

"tomatoes": "0.5 kilo",

"pumpkin": "3 kilos",

"watermelon": "2 kilos",

"lemons": "0.5 dozen",

"bananas": "0.5 dozen"

},

"James": {

"onions": "5 kilos",

"bananas": "2 dozen",

"strawberries": "1 box",

"pumpkin": "1 big sized"

},

"Sam": {

"detergent": "0.5 kilo",

"onions": "2 kilos",

"mango": "4 kilos"

},

"Kevin": {

"coriander": "3 pieces",

"chilies": "0.5 kilo",

"yogurt": "1 packet"

}

Prerequisites:

Make sure that you have completed the necessary prerequisites before proceeding:

  1. An OpenAI account: Sign up for an account at the OpenAI website (https://www.openai.com) and obtain your API key.
  2. Xcode: Install the latest version of Xcode, Apple’s integrated development environment for iOS app development.
  3. Basic knowledge of iOS Swift: Familiarity with iOS app development using Swift is beneficial for following the integration process.

Let’s follow the following steps to integrate the OpenAI’s API into a Xcode project:

Create a New iOS Project
Launch Xcode and create a new iOS project. Choose the appropriate template for your application and provide a name for your project.

Add the OpenAI API Client Library

To integrate the OpenAI API Client Library for iOS Swift using CocoaPods, follow these step-by-step instructions:

Ensure that you have CocoaPods installed on your system. If you don’t have it installed, open Terminal and run the following command:

sudo gem install cocoapods

At this point, you must have the cocoapods installed on your system. Create a pod file by navigating to your project’s root directory using Terminal and create a Podfile by running the following command:

pod init

Edit the pod file by opening the Podfile using a text editor. Include the following line in your Podfile:

pod 'OpenAI'

This specifies that you want to integrate the OpenAI API Client Library into your project.

Save the Podfile and run the following command in the Terminal:

pod install

This specifies that you want to integrate the OpenAI API Client Library into your project.

Save the Podfile and run the following command in the Terminal:

pod install

CocoaPods will download the OpenAI API Client Library and set up your project with the necessary dependencies.

From now on, open your project using the newly created workspace file (with a .xcworkspace extension) instead of the Xcode project file. This ensures that Xcode incorporates the dependencies added via CocoaPods.

Import the OpenAI Client Library

In the Swift file where you plan to use the OpenAI API, import the OpenAI client library by adding the following line at the top: In your app’s entry point, such as the AppDelegate, configure the OpenAI client with your API key. You can obtain your API key from the OpenAI website.

import OpenAI

Configure the OpenAI Client

In your app’s entry point, such as the AppDelegate.swift, configure the OpenAI client with your API key. You can acquire your API key by visiting the OpenAI website.

OpenAI.configure(apiKey: "YOUR_API_KEY")

You can obtain your API key from the OpenAI website. Please refer to the following links to know more about obtaining an API Key from the OpenAI website.

Make API Requests

Now, you’re ready to make API requests to OpenAI. There are various endpoints available, each serving different purposes. For example, let’s consider the “Completion” endpoint for generating text based on prompt input.

let rawData = ” Hi, I would like to order vegetables for four persons. Ryan wants three kilos of onions, half kilo tomatoes, a three-kilo pumpkin, a two-kilo watermelon, half a dozen lemons, and bananas. James wants five kilos of onions, two dozen bananas, a box of strawberries, and a big sized pumpkin. Sam wants half kilo detergent, two kilos of onions, four kilos of mango. Kevin wants three pieces of coriander, half kilo chilies and a packet of yogurt. ”

let query = "Extract who wants which items with their quantity in JSON format."
let prompt = rawData + query
let maxTokens = 100
var generatedText = ""

OpenAI.shared.completions.create(
engine: .davinci,
prompt: prompt,
maxTokens: maxTokens
) { result in
switch result {
case .success(let completion):
// Handle the generated text
generatedText = completion.choices[0].text
print(generatedText)
case .failure(let error):
// Handle the error
print(error.localizedDescription)
}
}

The above call should print the below generated text:

{
"Ryan": {
"onions": "3 kilos",
"tomatoes": "0.5 kilo",
"pumpkin": "3 kilos",
"watermelon": "2 kilos",
"lemons": "0.5 dozen",
"bananas": "0.5 dozen"
},
"James": {
"onions": "5 kilos",
"bananas": "2 dozen",
"strawberries": "1 box",
"pumpkin": "1 big sized"
},
"Sam": {
"detergent": "0.5 kilo",
"onions": "2 kilos",
"mango": "4 kilos"
},
"Kevin": {
"coriander": "3 pieces",
"chilies": "0.5 kilo",
"yogurt": "1 packet"
}
}

At this point, you should be able to convert the generated string object into JSON and parse the values using native JSON serialization.

Here are a few references from the OpenAI documentation that gives more insights on the API parameters. These references include more details about various types like Completions, Chat Completion, and other types:

  • https://platform.openai.com/docs/api-reference/completions
  • https://platform.openai.com/docs/api-reference/introduction
  • https://platform.openai.com/docs/models/overview
  • https://platform.openai.com/docs/introduction

Want to know more about Evoke’s expertise in application Development? Talk to Us

Goutham-Devaraju Goutham Devaraju occupies the role of Technical Lead at Evoke Technologies, demonstrating both extensive technical acumen and practical experience in the realm of mobile technology development. He has applied his expertise across a multitude of sectors, ranging from the Internet of Things (IoT) and Telematics to Social Networking and Insurance. His professional contributions have consistently expanded the horizons of technology’s influence and effectiveness within these industries.
Please follow and share

1 Comment

Leave a comment