Swiftpack.co - randombitsco/swift-openai-bits as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by randombitsco.
randombitsco/swift-openai-bits v0.0.8
A Swift package for the OpenAI GPT-3 APIs.
⭐️ 2
🕓 2 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/randombitsco/swift-openai-bits.git", from: "v0.0.8")

swift-openai-bits

Provides a Swift library to interact with the OpenAI GPT-3 service.

This library provides an async/await API for access, so requires being compiled with Swift 5.5+.

Requirements

Tools:

  • Swift 5.5+
  • Xcode 13+

Operating Systems

  • macOS 12 or later
  • iOS 15 or later
  • tvOS 15 or later
  • watchOS 8 or later

Installation

Add this to your project with Swift Package Manager.

Xcode

  1. Add a package via the Project > [Your Project] > Package Dependencies
  2. Enter https://github.com/randombitsco/swift-openai-bits for the URL
  3. Select Up to next major version with the current version value, or select Branch: and specify main.
  4. When prompted, add the OpenAIBits target to your project.

Swift Package Manager

  1. Open the Package.swift file.
  2. Add the following to in the Package:
dependencies: [
  .package(url: "https://github.com/randombitsco/swift-openai-bits", from: "0.0.8"),
],
  1. In targets, add a dependency on the library to the relevant target:
.target(
  name: "MyTarget",
  dependencies: [
    .product(name: "OpenAIBits", package: "swift-openai-bits"),
  ]),

Usage

Basic useage requires setting up a Client instance, with an OpenAI API Key (and Organization key, if relevant):

import OpenAIBits

let apiKey: String // your API key. Don't store this in code!
let client = Client(apiKey: apiKey)

// send a request to the API
let result = try await client.call(...)

Completions

Completions are the core text generation call. Keep in mind it will use tokens from your account on every call.

let result = try await client.call(Completions(
  model: .text_davinci_002,
  prompt: "You complete",
  maxTokens: 50,
  temperature: 0.8
))

let choice = result.choices.first!
print("Completion: \(choice.text)")
print("[Used \(result.usage.totalTokens") tokens]")

Edits

Edits allow you to provide a starting input and a an instruction, and it will return a new result based on the input, modified according to the instruction.

let result = try await client.call(Edits(
  model: .text_davinci_002,
  input: """
  We is going to the market.
  """,
  instruction: "Fix the grammar."
))

let choice = result.choices.first!
print("Edit: \(choice.text)")
print("[Used \(result.usage.totalTokens") tokens]")

Tokens

Along side the Client is the TokenEncoder. It is a struct that can be used and reused, with two methods: encode(text:) and decode(tokens:).

let encoder = TokenEncoder()
let tokens: [Int] = encoder.encoder(text: "A sentence.")
let text: String = encoder.decode(tokens: tokens)

print("Tokens: \(tokens)") // Tokens: [32, 6827, 13]
print("Text: \(text)")     // Text: A sentence.

GitHub

link
Stars: 2
Last commit: 5 hours ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

10 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics