dylanshine/openai-kit

A community Swift package used to interact with the OpenAI API

Swiftswiftaiopenaigpt-3dall-echatgpt
This is stars and forks stats for /dylanshine/openai-kit repository. As of 29 Apr, 2024 this repository has 629 stars and 94 forks.

OpenAIKit OpenAIKit is a Swift package used to communicate with the OpenAI API. Setup Add the dependency to Package.swift: dependencies: [ ... .package(url: "https://github.com/dylanshine/openai-kit.git", from: "1.0.0") ], targets: [ .target(name: "App", dependencies: [ .product(name: "OpenAIKit", package: "openai-kit"), ]), It is encouraged to use environment variables to inject the OpenAI API key, instead of hardcoding it in the source code. # .env OPENAI_API_KEY="YOUR-API-KEY" OPENAI_ORGANIZATION="YOUR-ORGANIZATION" ⚠️ OpenAI strongly recommends developers of client-side applications proxy requests through a separate backend service to keep their API key safe. API keys can access and manipulate customer billing, usage, and organizational data, so it's a significant risk to expose them. Create a OpenAIKit.Client by passing a configuration. var apiKey: String { ProcessInfo.processInfo.environment["OPENAI_API_KEY"]! } var organization: String { ProcessInfo.processInfo.environment["OPENAI_ORGANIZATION"]! } ... // Generally we would advise on creating a single HTTPClient for the lifecycle of your application and recommend shutting it down on application close. let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup)) defer { // it's important to shutdown the httpClient after all requests are done, even if one failed. See: https://github.com/swift-server/async-http-client try? httpClient.syncShutdown() } let configuration = Configuration(apiKey: apiKey, organization: organization) let openAIClient = OpenAIKit.Client(httpClient: httpClient, configuration: configuration) If you don't want to use SwiftNIO you can use URLSession. let urlSession = URLSession(configuration: .default) let configuration = Configuration(apiKey: apiKey, organization: organization) let openAIClient = OpenAIKit.Client(session: urlSession, configuration: configuration) Using the API The OpenAIKit.Client implements a handful of methods to interact with the OpenAI API: import OpenAIKit let completion = try await openAIClient.completions.create( model: Model.GPT3.davinci, prompts: ["Write a haiku"] ) What's Implemented Chat Models Completions Edits Images Embeddings Files Moderations Fine-tunes Speech to text Function calling Error handling If the request to the API failed for any reason an OpenAIKit.APIErrorResponse is thrown. Simply ensure you catch errors thrown like any other throwing function do { ... } catch let error as APIErrorResponse { print(error) }
Read on GithubGithub Stats Page
repotechsstarsweeklyforksweekly
analyticsinmotion/add-chatgpt-to-microsoft-wordVBA690180
mic1on/chatGPT-webVuePythonTypeScript647+3250+1
hadikachmar3/ChatGPT_flutter_courseDartC++CMake1760870
VB10/sesimiduyDartC++CMake720150
whitead/paper-qaPython3.1k02970
cequence-io/openai-scala-clientScalaJava122090
wkok/openai-clojureClojure1430150
Nikhil636/Attendance-appDartC++CMake30140
SEAbdulbasit/TravelApp-KMPKotlinSwiftRuby3660430
azbuky/kaspium_walletDartCMakeC++360110