OpenAI Project Introduction
Overview
OpenAI provides a versatile API designed for tasks involving the understanding or generation of natural language and code. The project offers a suite of models that vary in power and are tailored for different applications. Users can even fine-tune their own models. These models are applicable across a wide range of uses, from content generation to semantic search and classification.
Installation
OpenAI's Swift implementation can be easily integrated using either CocoaPods or Swift Package Manager.
CocoaPods
To include OpenAI in a project using CocoaPods, simply add the following line to your Podfile
:
pod 'OpenAIKit'
Swift Package Manager
To add OpenAI as a dependency through Swift Package Manager, insert the following into the dependencies
section of your Package.swift
file:
dependencies: [
.package(url: "https://github.com/FuturraGroup/OpenAI.git", .branch("main"))
]
Usage
Initialization
It's advisable to use environment variables to set the OpenAI API key to avoid hardcoding it. For example:
let apiToken: String = "<Your OpenAI API Token here>"
let organizationName: String = "<Your OpenAI Organization name>"
Initialize OpenAIKit
with your API Token, and optionally, your organization name:
import OpenAIKit
public let openAI = OpenAIKit(apiToken: apiToken, organization: organizationName)
Additional initialization parameters include timeout settings and options for custom API endpoints and SSL certificates.
SSL Handshake
SSL Handshake is a security feature to protect against MITM (Man-in-the-Middle) attacks by validating SSL certificates, ensuring secure communications with the OpenAI server.
Completions
OpenAI supports various completion methods to generate text based on a prompt. For instance:
openAI.sendCompletion(prompt: "Hello!", model: .gptV3_5(.davinciText003), maxTokens: 2048) { result in
switch result {
case .success(let aiResult):
if let text = aiResult.choices.first?.text {
print("response text: \(text)")
}
case .failure(let error):
print("Error: \(error.localizedDescription)")
}
}
This API also supports asynchronous usage and includes features like chat completions and streaming responses.
Chat Completions
Chat completions allow the API to respond to text prompts by considering the context of previous chat messages, specifically supporting models like gpt-3.5-turbo.
Stream
Stream mode enables real-time response retrieval during completion requests, allowing for immediate result processing without having to wait for a complete response.
JSON Mode
For enhanced performance and error reduction in certain models, JSON mode can ensure responses are formatted as valid JSON objects.
Generate Image
Using DALL·E, OpenAI can generate, edit, and create image variations based on a given description:
openAI.sendImagesRequest(prompt: "bird", size: .size512, n: 1) { result in
switch result {
case .success(let aiResult):
if let urlString = aiResult.data.first?.url {
print("generated image url: \(urlString)")
}
case .failure(let error):
print("Error: \(error.localizedDescription)")
}
}
Contribute
OpenAI welcomes contributions to improve the library. Feedback and suggestions can be submitted via the project's GitHub issues.
License
OpenAI is distributed under the MIT License, allowing for broad freedom in its use and modification, with the condition of including the license in all copies or significant portions of the software.