Swiftpack.co - chrszlz/cool-openai-kit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by chrszlz.
chrszlz/cool-openai-kit 1.0.1
A Swift package for the OpenAI API, but cool.
⭐️ 1
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/chrszlz/cool-openai-kit.git", from: "1.0.1")

CoolOpenAIKit

Swift

Because the name OpenAIKit was already taken...

A cool Swift package and set of utilities for OpenAI's API.

Installation

Add the dependency to Package.swift:

dependencies: [
    ...
    .package(url: "https://github.com/chrszlz/cool-openai-kit", from: "1.0.0")
],
...
targets: [
	...
    .target(
    	name: "App", 
    	dependencies: [
        	.product(name: "CoolOpenAIKit", package: "cool-openai-kit"),
    	]
    ),

Setup

⚠️ OpenAI recommendeds adding your API key to your environment, rather than hard-coding the value in code. This is very easy and ensures your keys stay safe.

Add API Key and Organization as Environment Variables from Xcode's Scheme Editor

  • OPENAI_API_KEY: your OpenAI API key
  • OPENAI_ORGANIZATION: YOUR OpenAI organization id (optional)

OPENAI_ORGANIZATION is for users who belong to multiple organizations, you can pass a header to specify which organization is used for an API request

Add API Keys

Now, upon initialization, the OpenAI.Client will automatically grab OPENAI_API_KEY and OPENAI_ORGANIZATION from your env.

let client = OpenAI.Client() // *magic*

More about Environment Variables in Swift

(Not recommended) Manually add API Key and Organiation

let configuration: OpenAI.Configuration = .init(
    apiKey: "<OPENAI_API_KEY>",
    organization: "<OPENAI_ORGANIZATION>" // optional
)
let client = OpenAI.Client(config: configuration)

Usage

The primary way to interact with the library is the OpenAI.client() object.

This client contains the properties that let you quickly access the API handlers.

Models

import CoolOpenAIKit

let client = OpenAI.Client()

// List all models
let models: [Model]? = try await client.models.list()
print(models)

// Retrieve single model
let model = try await client.models.retrieve(model: "text-davinci-003")
print(model)

Learn more about models.

Completions

let choices = try await client.completions.create(
	.davinci, 
	prompt: .basic(prompt: "What color are your eyes?"), 
	maxTokens: 100, 
	temperature: 0.5, 
	topP: 1, 
	n: 1, 
	presencePenalty: 0, 
	frequencyPenalty: 0
)
print(choices)


// Or create Completion Request separately
let prompt: Completions.Prompt = .basic(prompt: "What color are your eyes?")
let request = Completions.Request(
    model: .davinci,
    prompt: prompt,
    maxTokens: 100,
    temperature: 0.5,
    topP: 1,
    n: 1,
    presencePenalty: 0,
    frequencyPenalty: 0
)
let choices = try await client.completions.create(request)
print(choices)

Learn more about text completion.

Completion Handler Alternatives

// List all models
client.models.list { models in
    print(models)
}


// Text Completion
let prompt: Completions.Prompt = .basic(prompt: "What color are your eyes?")
let request = Completions.Request(
    model: .davinci,
    prompt: prompt,
    maxTokens: 100,
    temperature: 0.5,
    topP: 1,
    n: 1,
    presencePenalty: 0,
    frequencyPenalty: 0
)
client.completions.create(request) { choices in
    print(choices)
}

Error handling

Simply ensure you catch errors thrown like any other throwing function

do {
   let models = try await client.models.list()
   print(models)
} catch {
    print(error)
}

Supported Endpoints

Function Description Endpoint
.models.list() Lists the currently available models, and provides basic information about each one such as the owner and availability. GET /models
.models.retrieve(:) Retrieves a model instance, providing basic information about the model such as the owner and permissioning. GET /models/{model}
.completions.create(:) Creates a completion for the provided prompt and parameters POST /completions

Completeness

  • ☑ Models
  • ☑ Completions
  • ☐ Edits
  • ☐ Images
  • ☐ Embeddings
  • ☐ Files
  • ☐ Fine-tunes
  • ☐ Moderations
  • ☐ Test cases

Tests

Basic, but working.

Contributing

Make a PR, but make it cool

License

This software is copyright (c) 2023 Chris Zelazo.

For copyright and license information, please view the LICENSE file.

GitHub

link
Stars: 1
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

1.0.1: Access Control Modifiers
1 year ago
  • Add missing access control modifiers to /Source files
  • Update .gitignore

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