ChatGPTSwift

0.0.3

Access ChatGPT API using Swift
alfianlosari/ChatGPTSwift

What's New

0.0.3

2023-02-06T06:33:28Z
  • Update Base Prompt

ChatGPTSwift API

Alt text

Access ChatGPT "Official" API from Swift. Works on all Apple platforms.

NOTE

This use a leaked model with special prompt to trigger. Conversation history is stored on client. Its an unpatched bug that might be taken down in future.

Supported Platforms

  • iOS/tvOS 15 and above
  • macOS 12 and above
  • watchOS 8 and above

Installation

Swift Package Manager

Requirement

Register for API key from OpenAI. Initialize with api key

let api = ChatGPTAPI(apiKey: "API_KEY")

Usage

There are 2 APIs: stream and normal

Stream

The server will stream data one by one until complete, the method AsyncThrowingStream which you can loop using For-Loop like so:

Task {
    do {
        let stream = try await api.sendMessageStream(text: "What is ChatGPT?")
        for try await line in stream {
            print(line)
        }
    } catch {
        print(error.localizedDescription)
    }
}

Normal

A normal HTTP request and response lifecycle. Server will send the complete text (it will take more time to response)

Task {
    do {
        let response = try await api.sendMessage(text: "What is ChatGPT?")
        print(response)
    } catch {
        print(error.localizedDescription)
    }
}
        

Demo Apps

You can check the demo apps for iOS and macOS from the SwiftUIChatGPT repo

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Dec 21 2024 15:27:52 GMT-1000 (Hawaii-Aleutian Standard Time)