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.
- Go to Google AI Studio.
- Login with your Google account.
- Create an API key. Note that in Europe the free tier is not available.
- Check out this repository.
git clone https://github.com/google/generative-ai-swift - Open and build the sample app in the
Examplesfolder of this repo. - Run the app once to ensure the build script generates an empty
GenerativeAI-Info.plistfile - Paste your API key into the
API_KEYproperty in theGenerativeAI-Info.plistfile. - Run the app
- For detailed instructions, try the Swift SDK tutorial on ai.google.dev.
import GoogleGenerativeAI
let model = GenerativeModel(name: "gemini-1.5-flash-latest", apiKey: "<API_KEY>")
let response = try await model.generateContent("Hello!")
print(response.text ?? "")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.
See the Gemini API Cookbook or ai.google.dev for complete documentation.
Tests use the Swift Testing library (import Testing) instead of XCTest. Run with swift test from
the package root.
- Models and APIs only (GoogleGenerativeAI). No UI or provider-neutral layers here.
See Contributing for more information on contributing to the Google AI SDK for Swift.
The contents of this repository are licensed under the Apache License, version 2.0.