{"id":13346956,"url":"https://github.com/alfianlosari/ChatGPTSwift","last_synced_at":"2025-03-12T08:31:05.322Z","repository":{"id":65695072,"uuid":"597351744","full_name":"alfianlosari/ChatGPTSwift","owner":"alfianlosari","description":"Access ChatGPT API using Swift","archived":false,"fork":false,"pushed_at":"2024-08-28T06:29:26.000Z","size":94,"stargazers_count":643,"open_issues_count":20,"forks_count":129,"subscribers_count":14,"default_branch":"main","last_synced_at":"2024-10-24T18:36:06.595Z","etag":null,"topics":["chatgpt","chatgpt-api","chatgpt-api-wrapper","chatgpt3","gpt4","ios","linux","macos","swift","swift-library","swift-package-manager","tvos","watchos"],"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/alfianlosari.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-02-04T09:21:36.000Z","updated_at":"2024-10-23T09:06:58.000Z","dependencies_parsed_at":"2024-01-14T10:00:35.963Z","dependency_job_id":"cb098afc-9e03-47cc-8efa-45d3df60d8cd","html_url":"https://github.com/alfianlosari/ChatGPTSwift","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alfianlosari%2FChatGPTSwift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alfianlosari%2FChatGPTSwift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alfianlosari%2FChatGPTSwift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alfianlosari%2FChatGPTSwift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alfianlosari","download_url":"https://codeload.github.com/alfianlosari/ChatGPTSwift/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243184441,"owners_count":20249976,"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":["chatgpt","chatgpt-api","chatgpt-api-wrapper","chatgpt3","gpt4","ios","linux","macos","swift","swift-library","swift-package-manager","tvos","watchos"],"created_at":"2024-07-29T20:01:30.330Z","updated_at":"2025-03-12T08:31:04.889Z","avatar_url":"https://github.com/alfianlosari.png","language":"Swift","funding_links":[],"categories":["A01_文本生成_文本对话","SDK, Libraries, Frameworks","HarmonyOS"],"sub_categories":["大语言对话模型及数据","Swift","Windows Manager"],"readme":"# ChatGPTSwift API\n\n![Alt text](https://imagizer.imageshack.com/v2/640x480q90/923/c9MPBA.png \"image\")\n\nAccess OpenAI ChatGPT Official API using Swift. Works on all Apple platforms.\n\n## Supported Platforms\n\n- iOS/tvOS 15 and above\n- macOS 12 and above\n- watchOS 8 and above\n- Linux\n\n## Installation\n\n### Swift Package Manager\n- File \u003e Swift Packages \u003e Add Package Dependency\n- Add https://github.com/alfianlosari/ChatGPTSwift.git\n\n### Cocoapods\n```ruby\nplatform :ios, '15.0'\nuse_frameworks!\n\ntarget 'MyApp' do\n  pod 'ChatGPTSwift', '~\u003e 1.3.1'\nend\n```\n\n## Requirement\n\nRegister for API key from [OpenAI](https://openai.com/api). Initialize with api key\n\n```swift\nlet api = ChatGPTAPI(apiKey: \"API_KEY\")\n```\n\n## Usage\n\nThere are 2 APIs: stream and normal\n\n### Stream\n\nThe server will stream chunks of data until complete, the method `AsyncThrowingStream` which you can loop using For-Loop like so:\n\n```swift\nTask {\n    do {\n        let stream = try await api.sendMessageStream(text: \"What is ChatGPT?\")\n        for try await line in stream {\n            print(line)\n        }\n    } catch {\n        print(error.localizedDescription)\n    }\n}\n```\n\n### Normal\nA normal HTTP request and response lifecycle. Server will send the complete text (it will take more time to response)\n\n```swift\nTask {\n    do {\n        let response = try await api.sendMessage(text: \"What is ChatGPT?\")\n        print(response)\n    } catch {\n        print(error.localizedDescription)\n    }\n}\n        \n```\n\n### Providing extra parameters\n\nOptionally, you can provide the model, system prompt, temperature, and model like so.\n\n```swift\nlet response = try await api.sendMessage(text: \"What is ChatGPT?\",\n                                         model: \"gpt-4\",\n                                         systemText: \"You are a CS Professor\",\n                                         temperature: 0.5)\n```\n\nDefault values for these parameters are:\n- model: `gpt-3.5-turbo`\n- systemText: `You're a helpful assistant`\n- temperature: `0.5`\n\nTo learn more about those parameters, you can visit the official [ChatGPT API documentation](https://platform.openai.com/docs/guides/chat/introduction) and [ChatGPT API Introduction Page](https://openai.com/blog/introducing-chatgpt-and-whisper-apis)\n\n## History List\n\nThe client stores the history list of the conversation that will be included in the new prompt so ChatGPT aware of the previous context of conversation. When sending new prompt, the client will make sure the token count is not exceeding 4096 using [GPTEncoder library](https://github.com/alfianlosari/GPTEncoder) to calculate tokens in string, in case it exceeded the token, some of previous conversations will be truncated. In future i will provide an API to specify the token threshold as new gpt-4 model accept much bigger 8k tokens in a prompt.\n\n\n### View Current History List\n\nYou can view current history list from the `historyList` property.\n\n```swift\nprint(api.historyList)\n```\n\n### Delete History List\n\nYou can also delete the history list by invoking\n\n```swift\napi.deleteHistoryList()\n```\n\n### Replace History List\n\nYou can provide your own History List, this will replace the stored history list. Remember not to pass the 4096 tokens threshold.\n\n```swift\nlet myHistoryList = [\n    Message(role: \"user\", content: \"who is james bond?\")\n    Message(role: \"assistant\", content: \"secret british agent with codename 007\"),\n    Message(role: \"user\", content: \"which one is the latest movie?\"),\n    Message(role: \"assistant\", content: \"It's No Time to Die played by Daniel Craig\")\n]\n\napi.replaceHistoryList(with: myHistoryList)\n```\n\n## GPT Encoder Lib\nI've also created [GPTEncoder](https://github.com/alfianlosari/GPTEncoder) Swift BPE Encoder/Decoder for OpenAI GPT Models. A programmatic interface for tokenizing text for OpenAI GPT API.\n\n## GPT Tokenizer UI Lib\nI've also created [GPTTokenizerUI](https://github.com/alfianlosari/GPTTokenizerUI), a SPM lib you can integrate in your app for providing GUI to input text and show the tokenization results used by GPT API.\n\n![Alt text](https://imagizer.imageshack.com/v2/640x480q70/922/CEVvrE.png \"image\")\n\n## Demo Apps\nYou can check the demo apps for iOS and macOS from the [SwiftUIChatGPT repo](https://github.com/alfianlosari/ChatGPTSwiftUI)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falfianlosari%2FChatGPTSwift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falfianlosari%2FChatGPTSwift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falfianlosari%2FChatGPTSwift/lists"}