OpenAI

main

Swift package to interact with OpenAI API
Recouse/OpenAI

OpenAI

This package lets you quickly and easily access a variety of OpenAI API endpoints. It supports completions and chat streaming.

What's Implemented:

Installation

The module name of the package is OpenAI. Choose one of the instructions below to install and add the following import statement to your source code.

import OpenAI

From Xcode menu: File > Swift Packages > Add Package Dependency

https://github.com/Recouse/OpenAI

In your Package.swift file, first add the following to the package dependencies:

.package(url: "https://github.com/Recouse/OpenAI.git"),

And then, include "OpenAI" as a dependency for your target:

.target(name: "<target>", dependencies: [
    .product(name: "OpenAI", package: "OpenAI"),
]),

Usage

import OpenAI

...

let client = OpenAI(apiKey: "*YOUR API KEY*")

let chat = try await client.chat.completions(model: .gpt3_5_turbo, messages: [
    .user("Say This is a test.")
])

// Using streaming

let chat = client.chat.completionsStream(model: .gpt3_5_turbo, messages: [
    .user("Say This is a test in 5 different styles.")
])

for try await chunk in chat {
    print(chunk.choices.first?.delta.content)
}

Compatibility

  • macOS 10.15+
  • iOS 13.0+
  • tvOS 13.0+
  • watchOS 6.0+
  • visionOS 1.0+

Dependencies

Contributing

Contributions to are always welcomed! If you'd like to contribute, please fork this repository and submit a pull request with your changes.

License

OpenAI is released under the MIT license. See LICENSE for more information.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Thu Oct 17 2024 19:44:16 GMT-0900 (Hawaii-Aleutian Daylight Time)