Skip to content

A Swift client library for interacting with OpenAI's API.

License

Notifications You must be signed in to change notification settings

nathanborror/swift-openai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift OpenAI

An unofficial Swift client library for interacting with OpenAI's API.

Requirements

  • Swift 5.9+
  • iOS 16+
  • macOS 13+
  • watchOS 9+
  • tvOS 16+

Installation

Add the following to your Package.swift file:

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

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

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 <subcommand>

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 <subcommand>' for detailed help.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy