Fine-tune gpt model for Swift (iOS)

I’m trying to create a page on my iOS app that lets me talk to the gpt-3.5-turbo model. I want to give the model some background info about a specific topic and have it answer using that info. I tried this by just talking to the public ChatGPT bot, and had to talk back-and-forth and correct it on a few things to make sure it was accurate. I’ve heard that I can use fine-tuning to do this for OpenAI models but that it isn’t available on gpt-3.5 yet, and that the models it is available on will lose support soon. Is there any other way to accomplish this fine-tuning for gpt-3.5/gpt-3.5-turbo? Attached is a screenshot of how I’m currently calling the API in Swift.

private func sendMessage() {
    chatHistory.append("You: \(userInput)\n")
    chatHistory.append("Asking bot...\n")
    guard !userInput.isEmpty else { return }
    // Replace 'YOUR_API_ENDPOINT' with the actual ChatGPT API endpoint URL
    let apiEndpoint = ""
    // Replace 'YOUR_API_KEY' with the actual API key (if required by the API)
    let apiKey = ""
    // Rest of the code to make API request and update chatHistory with the response
    guard let url = URL(string: apiEndpoint) else {
        print("Invalid API endpoint URL.")
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
    let requestBody = ["prompt": userInput, "max_tokens": 1000] as [String : Any]
    userInput = ""
    guard let jsonData = try? requestBody) else {
        print("Error serializing request body.")
    request.httpBody = jsonData
    let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
        if let error = error {
            print("Error: \(error.localizedDescription)")

   print("Before parsing data...")
   if let data = data {
       do {
           let jsonResponse = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
           let choicesArray = jsonResponse?["choices"] as? [Any]
           let choicesDic = choicesArray?[0] as? [String: Any]
           if let text = choicesDic?["text"]{
               print("Value is: \(text)")
               //outputText = (text as? String ?? "")!
               var outputText =  text as? String ?? ""
               outputText = outputText.trimmingCharacters(in: .whitespacesAndNewlines)
               // Clear the userInput field after sending the message
               chatHistory.append("\nBot: \(outputText)\n\n")
       } catch {
           print("Error parsing JSON: \(error)")

Fine-tuning isn’t used as a way to “teach” the models. For that, the information should be included in the prompt (usually in the system prompt). If there is a lot of information, then you use embeddings/vector databases to do a similarity search based on the users query to only include snippets of relevant info in the prompt. See OpenAI’s cookbook for more details.

(Also, you should not include your API key in your application, it can be exposed and captured by your users and they can run up a huge bill on your account. You should have your iOS app call your own backend service where the API key is private)

1 Like