Integrating ChatGPT in Your iOS Apps

Sep 19 2024 · Swift 6, iOS 18, Xcode 16

Lesson 02: Using ChatGPT with iOS

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this example, you’ll use your ChatGPTClient in an iOS “Help Desk Chat App.” This app will ultimately allow users to communicate with a friendly AI assistant to get help troubleshooting computer-related problems.

var client = GPTClient(model: .gpt35Turbo)
private func sendMessage() {

  // 1
  isLoading = true

  Task {
    // 2
    let message = GPTMessage(role: .user, content: inputText)
    messages.append(message)

    do {
      // 3
      let response = try await client.sendChats(messages)
      isLoading = false

      // 4
      guard let reply = response.choices.first?.message else {
        print("API error! There weren't any choices despite a
          successful response")
        return
      }

      // 5
      messages.append(reply)
      inputText.removeAll()

    } catch {
      // 6
      isLoading = false
      print("Got an error: \(error)")
    }
  }
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion