A Swift package for interacting with OpenAI
- Utilizing ChatGPT using the ChatGPT API
- Request N completions using the Completions API
- Requesting N images using the Image Generation API
- Creating an image mask
Import OpenAI
and then add your API key from OpenAI. The API can be initialized directly or accessed using the static shared
property.
import OpenAI
OpenAI.shared.connect(with: "your-key")
// or
let openai = OpenAI(credentials: "your-key")
An example using several of the available roles (assistant
, system
and user
) with content.
let chats = try await openai.chats(
for: .init(
model: .gpt3(.turbo),
messages: [
.system("You are a helpful assistant."),
.user("Who won the world series in 2020?"),
.assistant("The Los Angeles Dodgers won the World Series in 2020."),
.user("Where was it played?")
]
)
)
print(chats)
This request supplies nil values by default for many of the available parameters, which can be supplied for added flexibility.
A simple request where the prompt is echoed back.
let completions = try await openai.completions(
for: .init(
model: .gpt3(.davinci),
prompt: "Say this is a test"
)
)
print(completions)
This request supplies nil values by default for many of the available parameters, which can be supplied for added flexibility.
A simple request for creating an image of a cat.
let images = try await openai.images(for: "A white siamese cat")
print(images) // images[0].url
This request supplies nil values by default for many of the available parameters, which can be supplied for added flexibility.
Please feel free to open a PR with desired changes and a brief description.
This package is not endorsed by, directly affiliated with, maintained, authorized, or sponsored by OpenAI.