

OpenAI
This community-maintained library, written in Swift, provides an easy way to use the OpenAI REST API.
Installation
dependencies: [
.package(url: "https://github.com/exyte/OpenAI")
]
To install OpenAI, simply add the following line to your Podfile:
To integrate OpenAI into your Xcode project using Carthage, specify it in your Cartfile
Requirements
- iOS 16+, tvOS 16+, macOS 13+, watchOS 8+
- Xcode 15+
Development Roadmap
- Models
- Files
- Assistants
- Run streaming
- Chat
- Swift Concurrency support
- Audio
- Images
- Moderations
- Fine-tuning
- Vector Store Files
Assistants API usage
- Obtain your API key. Do not share this with others or expose it in any client-side code.
⚠️ OpenAI strongly recommends developers of client-side applications proxy requests through a separate backend service to keep their API key safe. API keys can access and manipulate customer billing, usage, and organizational data, so it's a significant risk to expose them.
- Create a client instance.
let client = OpenAI(apiKey: "YOUR_API_KEY_HERE")
- Create an Assistant by defining its instructions and model.
let assistantPayload = CreateAssistantPayload(model: .gpt_4o, name: "My Assistant", instructions: "Be funny")
client.createAssistant(from: assistantPayload) <...>
- Create a Thread to start the conversation.
let threadPayload = CreateThreadPayload(messages: [...], metadata: [...])
client.createThread(from: threadPayload) <...>
- Add Messages to the Thread from the user.
let messagePayload = CreateMessagePayload(role: .user, content: "Hello!")
client.createMessage(in: threadId, payload: messagePayload) <...>
- Run the Assistant on the Thread to generate a response.
client.createRun(in: threadId, payload: CreateRunPayload(assistantId: assistantId)) <...>
- Check the Run status until it is completed or failed.
client.retrieveRun(id: runId, from: threadId)
- Retrieve the Messages from the Assistant.
let listPayload = ListPayload(after: lastMessageId)
client.listMessages(from: threadId, payload: listPayload) <...>
For more detailed information about OpenAI Assistants API usage, please refer to platform.openai.com and our Examples section.
Available endpoints
Chat
Creates a model response for the given chat conversation.
createChatCompletion(
from: CreateChatCompletionPayload(
model: model,
messages: [ChatCompletionMessage(role: .user, content: "Hello")]
)
)
Returns ChatCompletion
Models
List and describe the various models available in the API. A list of models and their differences can be found on platform.openai.com.
Returns ObjectList<Model>
retrieveModel(with: modelId)
Returns Model
deleteModel(with: modelId)
Returns DeletionStatus
Files
Files are used to upload documents that can be used with features like Assistants.
uploadFile(payload: FilePayload(purpose: filePurpose, fileURL: fileURL))
Returns File
Returns ObjectList<File>
Returns File
Returns DeletionStatus
retrieveFileContent(id: fileId, destinationURL: destinationURL)
Returns URL
Assistants
Build assistants that can call models and use tools to perform tasks.
createAssistant(from: CreateAssistantPayload(model: model, name: name, ...))
Returns Assistant
listAssistants(payload: ListPayload(limit: limit, ...))
Returns ObjectList<Assistant>
retrieveAssistant(id: assistantId)
Returns Assistant
modifyAssistant(id: assistandId, payload: CreateAssistantPayload(model: updatedModel, name: updatedName, ...))
Returns Assistant
deleteAssistant(id: assistantId)
Returns DeletionStatus
createThread(
from: CreateThreadPayload(
messages: [CreateMessagePayload(role: .user, content: "Hello"), ...],
metadata: ["key1": "value1", ...]
)
)
Returns Thread
retrieveThread(id: threadId)
Returns Thread
modifyThread(id: threadId, payload: ModifyPayload(metadata: ["key1": "value1", ...]))
Returns Thread