Exploring GPT3 and training ChatGPT with JSON in SwiftUI App.

Jasmine Elamblakatt
3 min readMar 14, 2023

ChatGPT is a truly remarkable creation that possesses an incredible capacity for natural language processing and communication.

It can understand and answer almost anything easily and accurately. It is just like you have a robot with you which can provide insights, answers, and recommendations easily to your specific needs and preferences.

Integrate basic ChatGPT in iOS SwiftUI App:

To integrate ChatGPT in the App, we need to use GPT3 Api which is a web-based service provided by OpenAI to integrate GPT-3’s language processing capabilities into the applications.

GPT-3 API offers a range of features, including the ability to generate grammatically correct sentences, complete text prompts with contextually appropriate content, and translate between languages.

Below is some of important parameters in JSON body sent to GPT3 Api:

 let jsonBody: [String: Any] = [
"model": "gpt-3.5-turbo",
"temperature": 1,
"max_tokens": 1024,
"prompt": generateChatGPTPrompt(from: text),
"stop": [
"\n\n\n",
"<|im_end|>"
],
"stream": stream
]

“gpt-3.5-turbo” model is the latest one developed by OpenAI

Temperature parameter is set between 0 and 1, with 0 being the most predictable and 1 being the most random. With a temperature of 0, GPT-3 will select the highest probable response each time.

prompt is the question which is asked by user in natural language and receive a text-based response that will been generated by the model.

I referred below the awesome swiftUI project to integrate GPT 3 inside the App. It’s pretty clear and easy to integrate, just need to change the Api key.

https://github.com/alfianlosari/ChatGPTSwiftUI

Once integrated, ChatGPT screen worked smoothly and was able to easily ask and get any answers on the go.

Pass App specific data to ChatGPT:

My next objective was to train and get customised information from ChatGPT. So the easiest and quickest way to get it was to pass JSON response from my external Apis and pass the data to GPT 3 as history at the initial state. So below is the code to pass JSON data as history to GPT 3.

So in ChatGPTAPI class modify the history function

if historyList.isEmpty {
self.historyList.append(orderListString)
}

So below is the JSON response passed to ChatGPT

[{“OrderID”:”89245542",
“OrderCode”:”9989",
“OrderStatus”:”In-Process”,
“PartNumber”:”8–372647328",
“category”:”Child Care”,
“subcategory”:”Bottles”,
“producttype”:”Kids Level5",
“customerpartnumber”:”18274" },
{
“OrderID”:”315423513",
“OrderCode”:”6788",
“OrderStatus”:”Shipped”,
“PartNumber”:”8–907785786",
“category”:”Cosmetics”,
“subcategory”:”Perfumes”,
“producttype”:”Top Models”,
“customerpartnumber”:””
},
{“OrderID”:”905613548",
“OrderCode”:”6788",
“OrderStatus”:”Pending”,
“PartNumber”:”8–907785786",
“category”:”Electronics”,
“subcategory”:”Laptop Latest”,
“producttype”:”Laptop Level1",
“customerpartnumber”:””
}]

So now when I ask ChatGPT any question related to the passed data it can answer easily. Check my implementation in below video;

https://video.wixstatic.com/video/51c7ca_a2293690c3874a04b14b16c3535c82e5/1080p/mp4/file.mp4

So by passing the data as history in ChatGPT request, it trains itself with the passed data and alters its learning and updates its knowledge based on new information it receives. So if we want the bot to respond based on specific way we can fine tune it with specific documentations or records.

Also it’s important to note that no system is completely foolproof, and there is always a risk of data breaches or other security incidents. As such, it’s important to take appropriate precautions when passing it with sensitive or confidential data, and to follow established best practices for data security and privacy.

--

--