Introduction to OpenAIKit
OpenAIKit is a powerful Swift package designed to facilitate communication with the OpenAI API. This package simplifies the integration of OpenAI's state-of-the-art artificial intelligence capabilities into Swift applications, allowing developers to harness the power of AI in their projects.
Getting Started with OpenAIKit
To begin using OpenAIKit, you need to add it as a dependency in your Swift project's Package.swift
file. Here's a simple example to guide you through:
dependencies: [
.package(url: "https://github.com/dylanshine/openai-kit.git", from: "1.0.0")
]
In addition, you should configure your project to safely manage your OpenAI API credentials. Instead of embedding your API key directly in the source code, you are encouraged to use environment variables, as shown below:
# .env
OPENAI_API_KEY="YOUR-API-KEY"
OPENAI_ORGANIZATION="YOUR-ORGANIZATION"
Using environment variables minimizes the risk of exposing sensitive information such as API keys, which can allow unauthorized access to customer billing, usage, and organizational data. Thus, OpenAI advises developers to route client-side requests through a backend service to safeguard their API keys.
Setting Up the OpenAIKit Client
After setting up your environment variables, you can create an OpenAIKit.Client
to start interacting with the OpenAI API. Here's a basic setup using Swift's HTTPClient
:
let apiKey = ProcessInfo.processInfo.environment["OPENAI_API_KEY"]!
let organization = ProcessInfo.processInfo.environment["OPENAI_ORGANIZATION"]!
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup))
defer {
try? httpClient.syncShutdown()
}
let configuration = Configuration(apiKey: apiKey, organization: organization)
let openAIClient = OpenAIKit.Client(httpClient: httpClient, configuration: configuration)
For those preferring not to use SwiftNIO, URLSession
is a viable alternative:
let urlSession = URLSession(configuration: .default)
let configuration = Configuration(apiKey: apiKey, organization: organization)
let openAIClient = OpenAIKit.Client(session: urlSession, configuration: configuration)
Interacting with the OpenAI API
The OpenAIKit.Client provides methods to interact seamlessly with the OpenAI API. This includes creating text completions, generating images, managing files, and more. Here's a simple example demonstrating how to create a text completion request:
import OpenAIKit
let completion = try await openAIClient.completions.create(
model: Model.GPT3.davinci,
prompts: ["Write a haiku"]
)
Features Implemented in OpenAIKit
OpenAIKit supports a broad array of features, allowing developers to leverage different aspects of the OpenAI API:
- Chat: Engage in interactive conversations.
- Models: Access various pre-trained AI models.
- Completions: Generate text based on input prompts.
- Edits: Make improvements or changes to text.
- Images: Create images from textual descriptions.
- Embeddings: Generate vector representations of text.
- Files: Manage file operations within the OpenAI platform.
- Moderations: Implement content moderation strategies.
- Speech to Text: Convert spoken language into text.
Currently, the package does not support fine-tuning models or function calling, but it covers a wide range of other applications.
Handling Errors
Error handling in OpenAIKit is straightforward. If a request to the API fails for any reason, an OpenAIKit.APIErrorResponse
is thrown. You can manage these errors using Swift's standard error-handling techniques:
do {
// Your OpenAI API call
} catch let error as APIErrorResponse {
print(error)
}
With OpenAIKit, developers have a comprehensive toolkit for embedding advanced AI functionalities into their Swift applications, tapping into OpenAI's powerful technologies.