Skip to content

wrkstrm/generative-ai-swift

 
 

Repository files navigation

Generative AI (Swift) — Slimmed Model SDK

This repository is a slimmed-down fork focused on model access only. UI layers have been removed and now live under our cross-app Gen UI package. This package exposes the Google Generative AI client (models) without UI, and bundled chat demos have been decommissioned to keep the surface area headless.

[!CAUTION] The Google AI SDK for Swift is recommended for prototyping only. If you plan to enable billing, we strongly recommend that you use a backend SDK to access the Google AI Gemini API. You risk potentially exposing your API key to malicious actors if you embed your API key directly in your Swift app or fetch it remotely at runtime.

Get started with the Gemini API

  1. Go to Google AI Studio.
  2. Login with your Google account.
  3. Create an API key. Note that in Europe the free tier is not available.
  4. Check out this repository.
    git clone https://github.com/google/generative-ai-swift
  5. Open and build the sample app in the Examples folder of this repo.
  6. Run the app once to ensure the build script generates an empty GenerativeAI-Info.plist file
  7. Paste your API key into the API_KEY property in the GenerativeAI-Info.plist file.
  8. Run the app
  9. For detailed instructions, try the Swift SDK tutorial on ai.google.dev.

Usage

import GoogleGenerativeAI

let model = GenerativeModel(name: "gemini-1.5-flash-latest", apiKey: "<API_KEY>")
let response = try await model.generateContent("Hello!")
print(response.text ?? "")

Logging

To enable additional logging in the Xcode console, including a cURL command and raw stream response for each model request, add -GoogleGenerativeAIDebugLogEnabled as Arguments Passed On Launch in the Xcode scheme.

Documentation

See the Gemini API Cookbook or ai.google.dev for complete documentation.

Testing

Tests use the Swift Testing library (import Testing) instead of XCTest. Run with swift test from the package root.

Scope

  • Models and APIs only (GoogleGenerativeAI). No UI or provider-neutral layers here.

Contributing

See Contributing for more information on contributing to the Google AI SDK for Swift.

License

The contents of this repository are licensed under the Apache License, version 2.0.

About

A fork of the Swift library for the Google Gemini API

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 91.1%
  • Shell 5.3%
  • Python 3.6%