OpenAIKit is a community-maintained API for the OpenAI REST endpoint used to get output data from Dall-E 2 and GPT-3 models.
Platform | Minimum Swift Version | Installation | Status |
---|---|---|---|
iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+ | 5.5 | Swift Package Manager | Fully Tested |
The Swift Package Manager allows for developers to easily integrate packages into their Xcode projects and packages; and is also fully integrated into the swift
compiler.
https://github.com/marcodotio/OpenAIKit.git
Once you have your Swift package set up, add the Git link within the dependencies
value of your Package.swift
file.
dependencies: [
.package(url: "https://github.com/marcodotio/OpenAIKit.git", .upToNextMajor(from: "1.1.3"))
]
OpenAIKit is designed to be very easy to integrate into your own projects. The main method of utilizing OpenAIKit is to set a OpenAI
class object:
import OpenAIKit
// An API key and Organization ID is required to use the API library.
// Note: It's recommended to load the API Key through a .plist dictionary, rather than hard coding it in a String.
let config = Configuration(organizationId: "INSERT-ORGANIZATION-ID", apiKey: "INSERT-API-KEY")
// Create an `OpenAI` object using the Configuration object.
let openAI = OpenAI(config)
From there, it's as easy as calling one of the provided function members. The code below demonstrates how you can generate an image using createImage()
:
do {
let imageParam = ImageParameters(
prompt: "a red apple",
resolution: .small,
responseFormat: .base64Json
)
let result = try await openAi.createImage(
parameters: imageParam
)
let b64Image = result.data[0].image
let image = try openAi.decodeBase64Image(b64Image)
} catch {
// Insert your own error handling method here.
}
As well, you are able to generate completions using GPT-3:
do {
let completionParameter = CompletionParameters(
model: "text-davinci-001",
prompt: ["Say this is a test ->"],
maxTokens: 4,
temperature: 0.98
)
let completionResponse = try await openAI.generateCompletion(
parameters: completionParameter
)
let responseText = completionResponse.choices[0].text
} catch {
// Insert your own error handling method here.
}
I welcome anyone to contribute to the project through posting issues, if they encounter any bugs / glitches while using OpenAIKit; and as well with creating pull issues that add any additional features to OpenAIKit.
I would like to personally thank the OpenAI Team for implementing the REST endpoint and implementing the models themselves, as without them, this project wouldn't have been possible.
As well, I would like to personally thank YufeiG for providing troubleshooting help on sending Image data for the Image Edit and Variations endpoints.
OpenAIKit is released under the MIT license, and any use of OpenAI's REST endpoint will be under the Usage policies set by them. See LICENSE for details.
link |
Stars: 58 |
Last commit: 4 days ago |
This adds the Data initializers for the Image Variation and Edit parameters for CloudKit and CoreData support.
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics