This package lets you quickly and easily access a variety of OpenAI API endpoints. It supports completions and chat streaming.
The module name of the package is OpenAI
. Choose one of the instructions below to install and add
the following import statement to your source code.
import OpenAI
From Xcode menu: File
> Swift Packages
> Add Package Dependency
https://github.com/Recouse/OpenAI
In your Package.swift
file, first add the following to the package dependencies
:
.package(url: "https://github.com/Recouse/OpenAI.git"),
And then, include "OpenAI" as a dependency for your target:
.target(name: "<target>", dependencies: [
.product(name: "OpenAI", package: "OpenAI"),
]),
import OpenAI
...
let client = OpenAI(apiKey: "*YOUR API KEY*")
let chat = try await client.chat.completions(model: .gpt3_5_turbo, messages: [
.user("Say This is a test.")
])
// Using streaming
let chat = client.chat.completionsStream(model: .gpt3_5_turbo, messages: [
.user("Say This is a test in 5 different styles.")
])
for try await chunk in chat {
print(chunk.choices.first?.delta.content)
}
- macOS 10.15+
- iOS 13.0+
- tvOS 13.0+
- watchOS 6.0+
- visionOS 1.0+
- EventSource https://github.com/Recouse/EventSource
Contributions to are always welcomed! If you'd like to contribute, please fork this repository and submit a pull request with your changes.
OpenAI is released under the MIT license. See LICENSE for more information.