Home

Awesome

<a href="https://exyte.com/"><picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/exyte/media/master/common/header-dark.png"><img src="https://raw.githubusercontent.com/exyte/media/master/common/header-light.png"></picture></a>

<a href="https://exyte.com/"><picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/exyte/media/master/common/our-site-dark.png" width="80" height="16"><img src="https://raw.githubusercontent.com/exyte/media/master/common/our-site-light.png" width="80" height="16"></picture></a>     <a href="https://twitter.com/exyteHQ"><picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/exyte/media/master/common/twitter-dark.png" width="74" height="16"><img src="https://raw.githubusercontent.com/exyte/media/master/common/twitter-light.png" width="74" height="16"> </picture></a> <a href="https://exyte.com/contacts"><picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/exyte/media/master/common/get-in-touch-dark.png" width="128" height="24" align="right"><img src="https://raw.githubusercontent.com/exyte/media/master/common/get-in-touch-light.png" width="128" height="24" align="right"></picture></a>

<p><h1 align="left">OpenAI</h1></p> <p><h4>This community-maintained library, written in Swift, provides an easy way to use the <a href="https://platform.openai.com/docs/api-reference/introduction">OpenAI REST API</a>.</h4></p>

SPM Compatible Cocoapods Compatible Carthage Compatible License: MIT

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/exyte/OpenAI")
]

CocoaPods

To install OpenAI, simply add the following line to your Podfile:

pod 'ExyteOpenAI'

Carthage

To integrate OpenAI into your Xcode project using Carthage, specify it in your Cartfile

github "exyte/OpenAI"

Requirements

Development Roadmap

Assistants API usage

  1. 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.

  1. Create a client instance.
let client = OpenAI(apiKey: "YOUR_API_KEY_HERE")
  1. 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) <...>
  1. Create a Thread to start the conversation.
let threadPayload = CreateThreadPayload(messages: [...], metadata: [...])
client.createThread(from: threadPayload) <...>
  1. Add Messages to the Thread from the user.
let messagePayload = CreateMessagePayload(role: .user, content: "Hello!")
client.createMessage(in: threadId, payload: messagePayload) <...>
  1. Run the Assistant on the Thread to generate a response.
client.createRun(in: threadId, payload: CreateRunPayload(assistantId: assistantId)) <...>
  1. Check the Run status until it is completed or failed.
client.retrieveRun(id: runId, from: threadId)
  1. 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.

Create chat completion

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.

List models

listModels()

Returns ObjectList<Model>

Retreive model

retrieveModel(with: modelId)

Returns Model

Delete a fine-tuned model

deleteModel(with: modelId)

Returns DeletionStatus

Files

Files are used to upload documents that can be used with features like Assistants.

Upload file

uploadFile(payload: FilePayload(purpose: filePurpose, fileURL: fileURL))

Returns File

List files

listFiles()

Returns ObjectList<File>

Retreive file

retrieveFile(id: fileId)

Returns File

Delete file

deleteFile(id: fileId)

Returns DeletionStatus

Retrieve file content

retrieveFileContent(id: fileId, destinationURL: destinationURL)

Returns URL

Assistants

Build assistants that can call models and use tools to perform tasks.

Create assistant

createAssistant(from: CreateAssistantPayload(model: model, name: name, ...))

Returns Assistant

List assistants

listAssistants(payload: ListPayload(limit: limit, ...))

Returns ObjectList<Assistant>

Retrieve assistant

retrieveAssistant(id: assistantId)

Returns Assistant

Modify assistant

modifyAssistant(id: assistandId, payload: CreateAssistantPayload(model: updatedModel, name: updatedName, ...))

Returns Assistant

Delete assistant

deleteAssistant(id: assistantId)

Returns DeletionStatus

Create thread

createThread(
    from: CreateThreadPayload(
	messages: [CreateMessagePayload(role: .user, content: "Hello"), ...],
	metadata: ["key1": "value1", ...]
    )
)

Returns Thread

Retrieve thread

retrieveThread(id: threadId)

Returns Thread

Modify thread

modifyThread(id: threadId, payload: ModifyPayload(metadata: ["key1": "value1", ...]))

Returns Thread

Delete thread

deleteThread(id: threadId)

Returns DeletionStatus

Create message

createMessage(in: threadId, payload: CreateMessagePayload(role: .user, content: "Hello"))

Returns Message

List messages

listMessages(from: threadId, payload: ListPayload(limit: limit))

Returns ObjectList<Message>

Retrieve message

retrieveMessage(id: messageId, from: threadId)

Returns Message

Modify message

modifyMessage(id: messageId, from: threadId, payload: ModifyPayload(metadata: ["key1": "value1", ...]))

Returns Message

Create run

createRun(in: threadId, payload: CreateRunPayload(assistantId: assistantId, ...))

Returns Run

Create run with streaming

createStreamRun(in: threadId, payload: CreateStreamRunPayload(assistantId: assistantId))

Returns StreamEvent sequence

Create thread and run

createThreadAndRun(
    from: CreateThreadAndRunPayload(
	assistantId: assistantId,
	thread: CreateThreadPayload(
	    messages: [CreateMessagePayload(role: .user, content: "Hello"), ...],
	    metadata: ["key1": "value1", ...]
	)
    )
)

Returns Run

List runs

listRuns(from: threadId, payload: ListPayload(limit: limit, ...))

Returns ObjectList<Run>

Retrieve run

retrieveRun(id: runId, from: threadId)

Returns Run

Modify run

modifyRun(id: runId, from: threadId, payload: ModifyPayload(metadata: ["key1": "value1", ...]))

Returns Run

Cancel run

cancelRun(id: runId, from: threadId)

Returns Run

List run steps

listRunSteps(from: runId, in: threadId, payload: ListPayload(limit: limit, ...))

Returns ObjectList<RunStep>

Retrieve run step

retrieveRunStep(id: runStepId, from: runId, in: threadId)

Returns RunStep

Examples

To try the OpenAIAssistants examples:

Our other open source SwiftUI libraries

PopupView - Toasts and popups library
Grid - The most powerful Grid container
ScalingHeaderScrollView - A scroll view with a sticky header which shrinks as you scroll
AnimatedTabBar - A tabbar with a number of preset animations
MediaPicker - Customizable media picker
Chat - Chat UI framework with fully customizable message cells, input view, and a built-in media picker
AnimatedGradient - Animated linear gradient
ConcentricOnboarding - Animated onboarding flow
FloatingButton - Floating button menu
ActivityIndicatorView - A number of animated loading indicators
ProgressIndicatorView - A number of animated progress indicators
FlagAndCountryCode - Phone codes and flags for every country
SVGView - SVG parser
LiquidSwipe - Liquid navigation animation