{"id":13572476,"url":"https://github.com/dylanshine/openai-kit","last_synced_at":"2025-05-16T09:04:43.174Z","repository":{"id":71612431,"uuid":"584502403","full_name":"dylanshine/openai-kit","owner":"dylanshine","description":"A community Swift package used to interact with the OpenAI API","archived":false,"fork":false,"pushed_at":"2024-08-26T14:15:19.000Z","size":1256,"stargazers_count":736,"open_issues_count":11,"forks_count":121,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-05-12T02:05:42.930Z","etag":null,"topics":["ai","chatgpt","dall-e","gpt-3","openai","swift"],"latest_commit_sha":null,"homepage":"https://platform.openai.com/docs/api-reference","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dylanshine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-02T18:47:37.000Z","updated_at":"2025-05-03T09:47:30.000Z","dependencies_parsed_at":"2024-06-19T17:37:10.371Z","dependency_job_id":"187cade1-aac1-425a-ba32-b40d22c14505","html_url":"https://github.com/dylanshine/openai-kit","commit_stats":{"total_commits":49,"total_committers":12,"mean_commits":4.083333333333333,"dds":0.5918367346938775,"last_synced_commit":"1ce94bbf8f0f9ebd84483dcdc42b1fda8e44d04e"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanshine%2Fopenai-kit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanshine%2Fopenai-kit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanshine%2Fopenai-kit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanshine%2Fopenai-kit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dylanshine","download_url":"https://codeload.github.com/dylanshine/openai-kit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254501557,"owners_count":22081528,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","chatgpt","dall-e","gpt-3","openai","swift"],"created_at":"2024-08-01T14:01:24.259Z","updated_at":"2025-05-16T09:04:38.161Z","avatar_url":"https://github.com/dylanshine.png","language":"Swift","funding_links":[],"categories":["Swift","SDK, Libraries, Frameworks","Unity"],"sub_categories":["Swift"],"readme":"# OpenAIKit\n\n![Swift](http://img.shields.io/badge/swift-5.7-brightgreen.svg)\n\nOpenAIKit is a Swift package used to communicate with the [OpenAI API](https://beta.openai.com/docs/api-reference/introduction).\n\n## Setup\nAdd the dependency to Package.swift:\n\n~~~~swift\ndependencies: [\n    ...\n    .package(url: \"https://github.com/dylanshine/openai-kit.git\", from: \"1.0.0\")\n],\ntargets: [\n    .target(name: \"App\", dependencies: [\n        .product(name: \"OpenAIKit\", package: \"openai-kit\"),\n    ]),\n~~~~\n\nIt is encouraged to use environment variables to inject the OpenAI API key, instead of hardcoding it in the source code.\n\n~~~~bash\n# .env\n\nOPENAI_API_KEY=\"YOUR-API-KEY\"\nOPENAI_ORGANIZATION=\"YOUR-ORGANIZATION\"\n~~~~\n⚠️ 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](https://nshipster.com/secrets/) them.\n\nCreate a `OpenAIKit.Client` by passing a configuration.\n\n~~~~swift\n\nvar apiKey: String {\n    ProcessInfo.processInfo.environment[\"OPENAI_API_KEY\"]!\n}\n\nvar organization: String {\n    ProcessInfo.processInfo.environment[\"OPENAI_ORGANIZATION\"]!\n}\n\n...\n\n// Generally we would advise on creating a single HTTPClient for the lifecycle of your application and recommend shutting it down on application close.\n\nlet eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)\n\nlet httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup))\n\ndefer {\n    // 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\n    try? httpClient.syncShutdown()\n}\n\nlet configuration = Configuration(apiKey: apiKey, organization: organization)\n\nlet openAIClient = OpenAIKit.Client(httpClient: httpClient, configuration: configuration)\n\n~~~~\n\nIf you don't want to use SwiftNIO you can use URLSession.\n\n~~~~swift\nlet urlSession = URLSession(configuration: .default)\nlet configuration = Configuration(apiKey: apiKey, organization: organization)\nlet openAIClient = OpenAIKit.Client(session: urlSession, configuration: configuration)\n~~~~\n\n## Using the API\n\nThe OpenAIKit.Client implements a handful of methods to interact with the OpenAI API:\n\n~~~~swift\nimport OpenAIKit\n\nlet completion = try await openAIClient.completions.create(\n    model: Model.GPT3.davinci,\n    prompts: [\"Write a haiku\"]\n)\n~~~~\n\n### What's Implemented\n* [x] [Chat](https://platform.openai.com/docs/api-reference/chat)\n* [x] [Models](https://beta.openai.com/docs/api-reference/models)\n* [x] [Completions](https://beta.openai.com/docs/api-reference/completions)\n* [x] [Edits](https://beta.openai.com/docs/api-reference/edits)\n* [x] [Images](https://beta.openai.com/docs/api-reference/images)\n* [x] [Embeddings](https://beta.openai.com/docs/api-reference/embeddings)\n* [x] [Files](https://beta.openai.com/docs/api-reference/files)\n* [x] [Moderations](https://beta.openai.com/docs/api-reference/moderations)\n* [ ] [Fine-tunes](https://beta.openai.com/docs/api-reference/fine-tunes)\n* [x] [Speech to text](https://platform.openai.com/docs/guides/speech-to-text)\n* [ ] [Function calling](https://platform.openai.com/docs/guides/gpt/function-calling)\n\n\n## Error handling\nIf the request to the API failed for any reason an `OpenAIKit.APIErrorResponse` is `thrown`.\nSimply ensure you catch errors thrown like any other throwing function\n\n~~~~swift\ndo {\n   ...\n} catch let error as APIErrorResponse {\n    print(error)\n}\n~~~~\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylanshine%2Fopenai-kit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdylanshine%2Fopenai-kit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylanshine%2Fopenai-kit/lists"}