Skip to content

SwiftyGPT is an unofficial, lightweight and user-friendly Swift wrapper for accessing OpenAI API

License

Notifications You must be signed in to change notification settings

antonio-war/SwiftyGPT

Repository files navigation

SwiftyGPT is an unofficial, lightweight and user-friendly Swift wrapper for accessing OpenAI API.

  • Simple: Designed with simplicity and ease of use in mind, SwiftyGPT eliminates the need for extensive configuration, making it ready to use right out of the box, all you need is your API Key! Say goodbye to cumbersome coding and hello to effortless interaction with OpenAI's capabilities!
  • Customizable: Whether you want to fine-tune parameters like model selection, adjust temperature, or rely on default values, SwiftyGPT offers flexibility to suit your needs.
  • Modular: SwiftyGPT is divided into modules, utilizing a different target for each feature exposed by the API. Import only the code that you really need, keeping your project lightweight and efficient.
  • Mockable: SwiftyGPT employs protocol-oriented programming to guarantee testability and maintainability. It already exposes some mock objects that can be seamlessly utilized in testing or SwiftUI previews, eliminating the need for actual API calls.

Integration

Integrating SwiftyGPT into your Swift project is straightforward. Follow these steps to get started:

  1. Install SwiftyGPT:
    • If you're using Swift Package Manager (SPM):
      • Open your Xcode project.
      • Navigate to "File" > "Swift Packages" > "Add Package Dependency...".
      • Enter the SwiftyGPT repository URL: https://github.com/antonio-war/SwiftyGPT.
      • Follow the prompts to select the version and add SwiftyGPT to your project.
      • SwiftyGPT exposes multiple targets, import only the ones that you really need in your project.
    • If you're using CocoaPods or Carthage, we're sorry, but they are not currently supported.
  2. Import SwiftyGPT:
    • In the files where you want to use SwiftyGPT features, import its modules at the top of the file:
      import SwiftyGPTChat
  3. Start Using SwiftyGPT:
    • Once SwiftyGPT is imported, you can start using its APIs to interact with GPT models.
    • Refer to the documentation for guidance on how to use its features for text generation, conversation, summarization, and more.
  4. Run Your Project:
    • Build and run your project to ensure that SwiftyGPT has been integrated successfully.
    • Test out the functionality you've implemented using SwiftyGPT to ensure everything works as expected. That's it! You've successfully integrated SwiftyGPT into your project and can now leverage its powerful features.

Usage

The main steps for using SwiftyGPTChat into your project are outlined below, guiding you through the process.

Service definition

First, define a SwiftyGPTChatService. You have three options:

  • Use SwiftyGPTChatNetworkingService if you want to execute API calls to OpenAI (requires an API Key).
  • Use SwiftyGPTChatMockService for mocked responses, ideal for testing or SwiftUI previews.
  • Create your custom instance to suit any of your purposes.
   import SwiftyGPTChat

   // Using SwiftyGPTChatNetworkingService
   let service = SwiftyGPTChatNetworkingService(apiKey: "YOUR_API_KEY")

   // Using SwiftyGPTChatMockService
   let service = SwiftyGPTChatMockService(responseBody: responseBody, duration: 0.5)

Manager creation

Create a SwiftyGPTChatManager instance using the defined service.

   let manager = SwiftyGPTChatManager(service: service)

Prompt engineering

Create your prompt using the various message types available.

   let messages: [any SwiftyGPTChatMessage] = [
       SwiftyGPTChatSystemMessage(content: "You are Victor from Fallout New Vegas"),
       SwiftyGPTChatUserMessage(content: "What's your name ?")
   ]

Request execution

Execute the request using the defined messages and a high degree of customization for all available parameters.

   let response = try await manager.send(messages: messages, model: .gpt3_5_turbo, frequencyPenalty: 0.5)

Response handling

If successful, by default the response message is found within the first choice received. But this may vary based on the type of request you make. In case of failure, however, the response body always contains an error describing what went wrong.

   switch response {
      case .success(let body):
         if let receivedMessage = body.choices.first?.message {
            messages.append(receivedMessage)
         } else {
            print("Oops, there are no available choices!")
         }
       case .failure(let body):
         print(body.error)
    }

Exploration

Uncover the limitless possibilities of SwiftGPT as you embark on a journey of discovery through the innovative Explorer app.
Delve deep into the realm of natural language processing and unleash the full potential of SwiftGPT with these captivating features:

  • Discover the Full Potential: Immerse yourself in the boundless capabilities of SwiftGPT as you navigate through the intuitive interface of the Explorer app. Unveil the power of state-of-the-art GPT models and witness firsthand their transformative impact on text generation, conversation, summarization, and more.
  • Explore Popular Use Cases: Embark on an exploration of the most popular use cases for SwiftGPT, meticulously curated within the Explorer app. From crafting compelling narratives to engaging in dynamic conversational interactions, delve into a myriad of scenarios where SwiftGPT shines brightest.
  • Practical Examples for Seamless Integration: Seamlessly integrate SwiftGPT into your projects with practical examples and step-by-step guidance provided by the Explorer app. Unlock the secrets to effortless integration and harness the unparalleled versatility of SwiftGPT to elevate your applications to new heights of excellence.

Recommendation

To ensure security and flexibility, we recommend loading your OpenAI API key using environment variables instead of hardcoding it directly into your source code. This approach offers several advantages:

  • Security: Storing sensitive information like API keys in environment variables helps prevent accidental exposure of credentials, reducing the risk of unauthorized access to your API resources.
  • Flexibility: By using environment variables, you can easily switch between different API keys or update them without modifying your code. This flexibility is especially useful in development, testing, and deployment scenarios.
  • Best Practice: Utilizing environment variables for configuration is considered a best practice in software development, promoting cleaner and more maintainable codebases.

Here's a simple example of how you can load your OpenAI API key from an environment variable in Swift:

guard let apiKey = ProcessInfo.processInfo.environment["OPEN_AI_API_KEY"] else {
    fatalError("Missing OpenAI API key. Please set the OPEN_AI_API_KEY environment variable.")
}

Support

Your generous donations help sustain and improve this project. Here's why supporting us is important:

  1. Covering API Costs: Accessing certain features or services may require a paid API key. Your donations help cover the cost of maintaining these subscriptions, ensuring uninterrupted access to essential functionalities.
  2. Development and Maintenance: Donations enable us to dedicate more time and resources to developing new features, fixing bugs, and maintaining the project's overall health. Your support directly contributes to the project's ongoing improvement and sustainability.
  3. Community Support: Your contributions show your support for the project and help foster a thriving community around it. Your generosity motivates us to keep pushing the project forward and encourages others to join the cause.
  4. Open Source Sustainability: By supporting open-source projects like ours, you're contributing to the sustainability of the entire open-source ecosystem. Your donations help ensure that valuable projects remain accessible to everyone.

Every donation, no matter how small, makes a big difference. Thank you for considering supporting us!

Buy Me A Coffee


Note

The content of this file has been automatically generated using SwiftyGPT.


License

SwiftyGPT is published under the MIT license.