{"id":14006737,"url":"https://github.com/mattt/OpenAI","last_synced_at":"2025-07-24T00:31:57.489Z","repository":{"id":40401729,"uuid":"362101475","full_name":"mattt/OpenAI","owner":"mattt","description":"A Swift client for the OpenAI API.","archived":true,"fork":false,"pushed_at":"2022-12-03T13:10:40.000Z","size":67,"stargazers_count":185,"open_issues_count":0,"forks_count":15,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-11-30T17:39:02.243Z","etag":null,"topics":["openai","openai-api"],"latest_commit_sha":null,"homepage":"","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/mattt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-27T12:18:39.000Z","updated_at":"2024-05-29T01:17:05.000Z","dependencies_parsed_at":"2023-01-23T00:00:29.964Z","dependency_job_id":null,"html_url":"https://github.com/mattt/OpenAI","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/mattt/OpenAI","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2FOpenAI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2FOpenAI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2FOpenAI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2FOpenAI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattt","download_url":"https://codeload.github.com/mattt/OpenAI/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2FOpenAI/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266774745,"owners_count":23982246,"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","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["openai","openai-api"],"created_at":"2024-08-10T10:01:36.595Z","updated_at":"2025-07-24T00:31:57.231Z","avatar_url":"https://github.com/mattt.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# OpenAI\n\n![CI][ci badge]\n\n\u003e **Warning**\n\u003e This project is no longer being maintained.\n\u003e \n\u003e The project was originally developed for a version of the OpenAI API\n\u003e that was deprecated on June 3, 2022 and removed on December 3, 2022. \n\u003e See [the announcement][deprecation-announcement] for more information.\n\u003e\n\u003e The current code requires significant updates in order to \n\u003e work with the current OpenAI API,\n\u003e but there are no plans to make the necessary changes at this time.\n\nA Swift client for the [OpenAI API](https://beta.openai.com/).\n\n## Requirements\n\n- Swift 5.3+\n- An OpenAI API Key\n\n## Example Usage\n\n### Base Series\n\n\u003e Our [base GPT-3 models] can understand and generate natural language.\n\u003e We offer four base models called `davinci`, `curie`, `babbage`, and `ada`\n\u003e with different levels of power suitable for different tasks.\n\n#### Completions\n\n```swift\nimport OpenAI\n\nlet apiKey: String // required\nlet client = Client(apiKey: apiKey)\n\nlet prompt = \"Once upon a time\"\n\nclient.completions(engine: .davinci,\n                   prompt: prompt,\n                   numberOfTokens: ...5,\n                   numberOfCompletions: 1) { result in\n    guard case .success(let completions) = result else { return }\n\n    completions.first?.choices.first?.text // \" there was a girl who\"\n}\n```\n\n#### Searches\n\n```swift\nimport OpenAI\n\nlet apiKey: String // required\nlet client = Client(apiKey: apiKey)\n\nlet documents: [String] = [\n    \"White House\",\n    \"hospital\",\n    \"school\"\n]\n\nlet query = \"president\"\n\nclient.search(engine: .davinci,\n              documents: documents,\n              query: query) { result in\n    guard case .success(let searchResults) = result else { return }\n    searchResults.max()?.document // 0 (for \"White House\")\n}\n```\n\n#### Classifications\n\n```swift\nimport OpenAI\n\nlet apiKey: String // required\nlet client = Client(apiKey: apiKey)\n\nlet query = \"It is a raining day :(\"\n\nlet examples: [(String, label: String)] = [\n    (\"A happy moment\", label: \"Positive\"),\n    (\"I am sad.\", label: \"Negative\"),\n    (\"I am feeling awesome\", label: \"Positive\")\n]\n\nlet labels = [\"Positive\", \"Negative\", \"Neutral\"]\n\nclient.classify(engine: .curie,\n                query: query,\n                examples: examples,\n                labels: labels,\n                searchEngine: .ada) { result in\n    guard case .success(let classification) = result else { return }\n\n    classification.label // \"Negative\"\n}\n```\n\n#### Answers\n\n```swift\nimport OpenAI\n\nlet apiKey: String // required\nlet client = Client(apiKey: apiKey)\n\nlet documents: [String] = [\n    \"Puppy A is happy.\",\n    \"Puppy B is sad.\"\n]\n\nlet question = \"which puppy is happy?\"\n\nlet examples: (context: String, [(question: String, answer: String)]) = (\n    context: \"In 2017, U.S. life expectancy was 78.6 years.\",\n    [\n        (question: \"What is human life expectancy in the United States?\", answer: \"78 years.\")\n    ]\n)\n\nclient.answer(engine: .curie,\n              question: question,\n              examples: examples,\n              documents: documents,\n              searchEngine: .ada,\n              stop: [\"\\n\", \"\u003c|endoftext|\u003e\"]) { result in\n    guard case .success(let response) = result else { return }\n\n    response.answers.first // \"puppy A.\"\n}\n```\n\n### Codex\n\n\u003e The [Codex] models are descendants of our base GPT-3 models\n\u003e that can understand and generate code.\n\u003e Their training data contains both natural language and\n\u003e billions of lines of public code from GitHub.\n\n```swift\nimport OpenAI\n\n// `Engine.ID` provides cases for the\n// `ada`, `babbage`, `curie`, and `davinci` engines.\n// You can add convenience APIs for other engines\n// by defining computed type properties in an extension.\nextension Engine.ID {\n    static var davinciCodex: Self = \"code-davinci-002\"\n}\n\nlet apiKey: String // required\nlet client = Client(apiKey: apiKey)\n\nlet prompt = #\"\"\"\n// Translate this function from Swift into Objective-C\n// Swift\n\nlet numbers = [Int](1...10)\nlet evens = numbers.filter { $0 % 2 == 0 }\nlet sumOfEvens = evens.reduce(0, +)\n\n// Objective-C\n\n\"\"\"#\n\nclient.completions(engine: .davinciCodex,\n                   prompt: prompt,\n                   sampling: .temperature(0.0),\n                   numberOfTokens: ...256,\n                   numberOfCompletions: 1,\n                   echo: false,\n                   stop: [\"//\"],\n                   presencePenalty: 0.0,\n                   frequencyPenalty: 0.0,\n                   bestOf: 1) { result in\n    guard case .success(let completions) = result else { fatalError(\"\\(result)\") }\n\n    for choice in completions.flatMap(\\.choices) {\n        print(\"\\(choice.text)\")\n    }\n}\n// Prints the following code:\n// ```\n// NSArray *numbers = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10];\n// NSArray *evens = [numbers filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@\"self % 2 == 0\"]];\n// NSInteger sumOfEvens = [[evens valueForKeyPath:@\"@sum.self\"] integerValue];\n// ```\n```\n\n### Instruct Series\n\n\u003e The [Instruct] models share our base GPT-3 models’ ability to\n\u003e understand and generate natural language,\n\u003e but they’re better at understanding and following your instructions.\n\u003e You simply tell the model what you want it to do,\n\u003e and it will do its best to fulfill your instructions.\n\n```swift\nimport OpenAI\n\nlet apiKey: String // required\nlet client = Client(apiKey: apiKey)\n\nlet prompt = \"Describe the Swift programming language in a few sentences.\"\n\nclient.completions(engine: \"davinci-instruct-beta\",\n                   prompt: prompt,\n                   sampling: .temperature(0.0),\n                   numberOfTokens: ...100,\n                   numberOfCompletions: 1,\n                   stop: [\"\\n\\n\"],\n                   presencePenalty: 0.0,\n                   frequencyPenalty: 0.0,\n                   bestOf: 1) { result in\n    guard case .success(let completions) = result else { fatalError(\"\\(result)\") }\n\n    for choice in completions.flatMap(\\.choices) {\n        print(\"\\(choice.text)\")\n    }\n}\n// Prints the following:\n// \"Swift is a general-purpose programming language that was developed by Apple Inc. for iOS and OS X development. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. Swift is intended to be more resilient to erroneous code (such as buffer overflow errors) and better support concurrency (such as multi-threading) than Objective-C.\"\n```\n\n### Content Filter\n\n\u003e The [content filter] aims to detect generated text that could be\n\u003e sensitive or unsafe coming from the API.\n\u003e It's currently in beta mode and has three ways of classifying text —\n\u003e as safe, sensitive, or unsafe.\n\u003e The filter will make mistakes and we have currently built it to\n\u003e err on the side of caution, thus, resulting in higher false positives.\n\n```swift\nimport OpenAI\n\nlet apiKey: String // required\nlet client = Client(apiKey: apiKey)\n\nlet prompt = \"I know it's an unpopular political opinion to hold, but I think that...\"\n\nclient.completions(engine: \"content-filter-alpha-c4\",\n                   prompt: \"\u003c|endoftext|\u003e\\(prompt)\\n--\\nLabel:\",\n                   sampling: .temperature(0.0),\n                   numberOfTokens: ...1,\n                   numberOfCompletions: 1,\n                   echo: false,\n                   stop: [\"\u003c|endoftext|\u003e[prompt]\\n--\\nLabel:\"],\n                   presencePenalty: 0.0,\n                   frequencyPenalty: 0.0,\n                   bestOf: 1) { result in\n    guard case .success(let completions) = result else { fatalError(\"\\(result)\") }\n\n    if let text = completions.flatMap(\\.choices).first?.text.trimmingCharacters(in: .whitespacesAndNewlines) {\n        switch Int(text) {\n        case 0:\n            print(\"Safe\")\n        case 1:\n            print(\"Sensitive\")\n            // This means that the text could be talking about a sensitive topic, something political, religious, or talking about a protected class such as race or nationality.\n        case 2:\n            print(\"Unsafe\")\n            // This means that the text contains profane language, prejudiced or hateful language, something that could be NSFW, or text that portrays certain groups/people in a harmful manner.\n        default:\n            print(\"unexpected token: \\(text)\")\n        }\n    }\n}\n// Prints \"Sensitive\"\n```\n\n## Installation\n\n### Swift Package Manager\n\nAdd the OpenAI package to your target dependencies in `Package.swift`:\n\n```swift\n// swift-tools-version:5.3\nimport PackageDescription\n\nlet package = Package(\n  name: \"YourProject\",\n  dependencies: [\n    .package(\n        url: \"https://github.com/mattt/OpenAI\",\n        from: \"0.1.3\"\n    ),\n  ]\n)\n```\n\nThen run the `swift build` command to build your project.\n\n## License\n\nMIT\n\n## Contact\n\nMattt ([@mattt](https://twitter.com/mattt))\n\n[ci badge]: https://github.com/mattt/OpenAI/workflows/CI/badge.svg\n[documentation badge]: https://github.com/mattt/OpenAI/workflows/Documentation/badge.svg\n[documentation]: https://github.com/mattt/OpenAI/wiki\n\n[base GPT-3 models]: https://beta.openai.com/docs/engines/base-series\n[Codex]: https://beta.openai.com/docs/engines/codex-series-private-beta\n[Instruct]: https://beta.openai.com/docs/engines/instruct-series-beta\n[content filter]: https://beta.openai.com/docs/engines/content-filter\n\n[deprecation-announcement]: https://community.openai.com/t/answers-classification-search-endpoint-deprecation/18532\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattt%2FOpenAI","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattt%2FOpenAI","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattt%2FOpenAI/lists"}