Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

colealanroberts/OpenAI

Repository files navigation

OpenAI

A Swift package for interacting with OpenAI

Quick Start

Authentication

Import OpenAI and then add your API key from OpenAI. The API can be initialized directly or accessed using the static shared property.

import OpenAI
OpenAI.shared.connect(with: "your-key")
// or
let openai = OpenAI(credentials: "your-key")

Chats

An example using several of the available roles (assistant, system and user) with content.

let chats = try await openai.chats(
 for: .init(
 model: .gpt3(.turbo),
 messages: [
 .system("You are a helpful assistant."),
 .user("Who won the world series in 2020?"),
 .assistant("The Los Angeles Dodgers won the World Series in 2020."),
 .user("Where was it played?")
 ]
 )
)
print(chats)

This request supplies nil values by default for many of the available parameters, which can be supplied for added flexibility.

Completions

A simple request where the prompt is echoed back.

let completions = try await openai.completions(
 for: .init(
 model: .gpt3(.davinci),
 prompt: "Say this is a test"
 )
)
print(completions)

This request supplies nil values by default for many of the available parameters, which can be supplied for added flexibility.

Images

A simple request for creating an image of a cat.

let images = try await openai.images(for: "A white siamese cat")
print(images) // images[0].url

This request supplies nil values by default for many of the available parameters, which can be supplied for added flexibility.

License

License: MIT

Contributing

Please feel free to open a PR with desired changes and a brief description.

Disclaimer

This package is not endorsed by, directly affiliated with, maintained, authorized, or sponsored by OpenAI.

Contributors 2

Languages

AltStyle によって変換されたページ (->オリジナル) /