https://github.com/nathanborror/swift-openai
A Swift client library for interacting with OpenAI's API.
https://github.com/nathanborror/swift-openai
gpt llm openai swift
Last synced: about 1 month ago
JSON representation
A Swift client library for interacting with OpenAI's API.
- Host: GitHub
- URL: https://github.com/nathanborror/swift-openai
- Owner: nathanborror
- License: mit
- Created: 2024-12-06T17:11:01.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-08-08T20:02:37.000Z (10 months ago)
- Last Synced: 2025-08-08T20:29:20.833Z (10 months ago)
- Topics: gpt, llm, openai, swift
- Language: Swift
- Homepage: https://platform.openai.com
- Size: 2.4 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Swift OpenAI
An unofficial Swift client library for interacting with [OpenAI's API](https://platform.openai.com).
## Requirements
- Swift 5.9+
- iOS 16+
- macOS 13+
- watchOS 9+
- tvOS 16+
## Installation
Add the following to your `Package.swift` file:
```swift
Package(
dependencies: [
.package(url: "https://github.com/nathanborror/swift-openai", branch: "main"),
],
targets: [
.target(
name: "YourApp",
dependencies: [
.product(name: "OpenAI", package: "swift-openai"),
]
),
]
)
```
## Usage
### Chat Completion
```swift
import OpenAI
let client = Client(apiKey: OPENAI_API_KEY)
let req = ChatRequest(
messages: [.init(text: "Hello, OpenAI!", role: .user)],
model: "gpt-4o"
)
do {
let response = try await client.chatCompletions(request)
print(response)
} catch {
print(error)
}
```
### List models
```swift
import OpenAI
let client = Client(apiKey: OPENAI_API_KEY)
do {
let response = try await client.models()
print(response.data.map { $0.id }.joined(separator: "\n"))
} catch {
print(error)
}
```
### Command Line Interface
```
$ make
$ ./openai
OVERVIEW: A utility for interacting with the OpenAI API.
USAGE: openai
OPTIONS:
--version Show the version.
-h, --help Show help information.
SUBCOMMANDS:
models Returns available models.
chat Returns a reponse to a chat prompt.
transcribe Returns an audio transcription.
See 'cli help ' for detailed help.
```