An unofficial Swift SDK for the new GPT Image Generation API.
Swift Package Manager
Add the following to your Package.swift:
dependencies: [
.package(url: "https://github.com/m1guelpf/swift-openai-images.git", .branch("main"))
]Installing through XCode
- File > Swift Packages > Add Package Dependency
- Add https://github.com/m1guelpf/swift-openai-images.git
- Select "Branch" with "main"
CocoaPods
Ask ChatGPT to help you migrate away from CocoaPods.
import OpenAIImages
let client = ImagesAPI(authToken: OPENAI_API_KEY)
let response = try await client.create(prompt: "A cat playing chess")
let response = try await client.edit(
image: .url(Bundle.main.url(forResource: "myImage", withExtension: "png"), format: .png),
prompt: "Change the color of the sky"
)To interact with the Images API, create a new instance of ImagesAPI with your API key:
let client = ImagesAPI(authToken: YOUR_OPENAI_API_TOKEN)Optionally, you can provide an Organization ID and/or project ID:
let client = ImagesAPI(
authToken: YOUR_OPENAI_API_KEY,
organizationId: YOUR_ORGANIZATION_ID,
projectId: YOUR_PROJECT_ID
)For more advanced use cases like connecting to a custom server, you can customize the URLRequest used to connect to the API:
let urlRequest = URLRequest(url: MY_CUSTOM_ENDPOINT)
urlRequest.addValue("Bearer \(YOUR_API_KEY)", forHTTPHeaderField: "Authorization")
let client = ImagesAPI(connectingTo: urlRequest)To generate new images, call the create method with a CreateImageRequest instance:
let response = try await client.create(CreateImageRequest(
prompt: "A children's book drawing of a veterinarian using a stethoscope to listen to the heartbeat of a baby otter.",
))Alternatively, you can pass the parameters directly to the create method:
let response = try await client.create(
prompt: "A children's book drawing of a veterinarian using a stethoscope to listen to the heartbeat of a baby otter."
size: .landscape
)To edit an existing image, call the edit method with an EditImageRequest instance:
let response = try await client.edit(EditImageRequest(
images: [.image(name: "image.png", data: imageData, format: .png)],
prompt: "Change the background to a beach."
)Alternatively, you can pass the parameters directly to the edit method:
let response = try await client.edit(
images: [.url(URL(string: "https://example.com/image.webp"), format: .webp)],
prompt: "Change the background to a beach."
)This project is licensed under the MIT License - see the LICENSE file for details.