/ChatGPTSwift

Access ChatGPT API using Swift

Primary LanguageSwiftMIT LicenseMIT

ChatGPTSwift API

Alt text

Access OpenAI ChatGPT Official API using Swift. Works on all Apple platforms.

Supported Platforms

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

Installation

Swift Package Manager

Requirement

Register for API key from OpenAI. Initialize with api key

let api = ChatGPTAPI(apiKey: "API_KEY")

optionally, you can provide the system prompt, temperature, and model. Default values for these parameters are:

public init(apiKey: String,
        model: String = "gpt-3.5-turbo",
        systemPrompt: String = "You are a helpful assistant",
        temperature: Double = 0.5)

To learn more about those parameters, you can visit the official ChatGPT API documentation and ChatGPT API Introduction Page

Usage

There are 2 APIs: stream and normal

Stream

The server will stream chunks of data 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)
    }
}
        

History List

The client stores the history list of the conversation that will be included in the new prompt so ChatGPT aware of the previous context of conversation. When sending new prompt, the client will make sure the token is not exceeding 4000 (using calculation of 1 token=4chars), in case it exceeded the token, some of previous conversations will be truncated

You can also delete the history list by invoking

api.deleteHistoryList()

You should not call this, while waiting for the response from ChatGPT. I'll need to handle this properly in later release/

Demo Apps

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