SwiftOpenAI
is a powerful and easy-to-use Swift SDK designed to seamlessly integrate with OpenAI's API
. The main goal of this SDK is to simplify the process of accessing and interacting with OpenAI's cutting-edge AI models, such as GPT-4, GPT-3, and future models, all within your Swift applications.
Open Xcode and open the Swift Package Manager
section, then paste the Github URL from this repository (Copy -> https://github.com/SwiftBeta/SwiftOpenAI.git) to install the Package in your project.
![]() | ![]() |
Using SwiftOpenAI` is simple and straightforward. Follow these steps to start interacting with OpenAI's API in your Swift project:
First, import the SwiftOpenAI module in the file where you want to use it:
import SwiftOpenAI
Next, create an instance of the SwiftOpenAI class, passing your OpenAI API key as a parameter. Make sure you have already obtained an API key from your OpenAI account.
var openAI = SwiftOpenAI(apiKey: "YOUR-API-KEY")
The createChatCompletionsStream
method allows you to interact with the OpenAI API by generating chat-based completions. Provide the necessary parameters to customize the completions, such as model, messages, and other optional settings.
do { for try await newMessage in try await openAI.createChatCompletionsStream(model: .gpt3_5(.turbo), messages: [.init(text: "Generate the Hello World in Swift for me", role: "user")], optionalParameters: .init(stream: true)) { print("New Message Received: \(newMessage) ") } } catch { print(error) }
Here you have a full example using SwiftUI:
import SwiftUI import SwiftOpenAI struct ContentView: View { var openAI = SwiftOpenAI(apiKey: "YOUR-API-KEY") var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundColor(.accentColor) Text("Hello, world!") } .padding() .onAppear { Task { do { for try await newMessage in try await openAI.createChatCompletionsStream(model: .gpt3_5(.turbo), messages: [.init(text: "Generate the Hello World in Swift for me", role: "user")], optionalParameters: .init(stream: true)) { print("New Message Received: \(newMessage) ") } } catch { print(error) } } } } }
The createChatCompletions
method allows you to interact with the OpenAI API by generating chat-based completions. Provide the necessary parameters to customize the completions, such as model, messages, and other optional settings.
do { let result = try await openAI.createChatCompletions(model: .gpt3_5(.turbo), messages: [.init(text: "Generate the Hello World in Swift for me", role: "user")]) print(result) } catch { print(error) }
Here you have a full example using SwiftUI:
import SwiftUI import SwiftOpenAI struct ContentView: View { var openAI = SwiftOpenAI(apiKey: "YOUR-API-KEY") var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundColor(.accentColor) Text("Hello, world!") } .padding() .onAppear { Task { do { let result = try await openAI.createChatCompletions(model: .gpt3_5(.turbo), messages: [.init(text: "Generate the Hello World in Swift for me", role: "user")]) print(result) } catch { print(error) } } } } }
MIT License
Copyright 2023 SwiftBeta
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.