Getting started with the Gemini API and Swift

Learn how to use the Gemini API and the Google AI Swift SDK to prototype generative AI with Swift. Use the Google AI Swift SDK to make your first generative AI call using the Gemini API in your application. Explore a sample application and learn how to make multimodal prompts (that combine image and text).

 

Introduction to the Gemini API and prompt engineering

Pathway

Explore Google AI Studio and the capabilities of the Gemini generative AI model. Learn how to design and test the different types of prompts (freeform, structured, and chat) and get an API key for the Gemini API.

Note that the Google AI Swift SDK is only for prototyping and exploring the Gemini generative AI models. For use cases beyond prototyping (especially production or enterprise-scale apps), use Vertex AI in Firebase instead. It offers an SDK for Swift that has additional security features, support for large media file uploads, and streamlined integrations into the Firebase and Google Cloud ecosystem.

This pathway can be useful for further experimentation with the Gemini API and lays the groundwork for integrating its features into your application. Optionally, you can also try out the Gemini API using a simple NodeJS web application. If you don't already have NodeJS and NPM on your machine, feel free to skip this step and return back to Swift in this pathway.

Introduction to the Google AI Swift SDK

Article

The Google AI Swift SDK is a Swift-first, cross-platform SDK that gives you access to the Google AI Gemini API for prototyping your generative AI integration. This SDK supports Swift on most platforms.

To get started with the Google AI Swift SDK, set up a project in Google AI Studio, which includes obtaining an API key for the Gemini API. Next, add the GoogleGenerativeAI package to your app's project configuration. Then, you can initialize the generative model and make your first API call.

The Google AI Swift SDK is only for prototyping. There are additional security considerations for using the Gemini API key in your mobile client applications since you're risking exposing this API key to malicious actors if it's embedded or retrieved by your client application. So, for use cases beyond prototyping (especially production and enterprise-scale apps), migrate to Vertex AI in Firebase to call the Gemini API directly from your client app. Alternatively, you can access the Gemini models server-side through Vertex AI.

Walkthrough: Build LLM-powered apps with Swift

Video

Learn how to use multi-turn conversations with the Gemini model to build an interactive text adventure in Swift.

This practical example walks through building a game with Swift UI and the Google AI Swift SDK to call the Gemini API. Learn more about designing prompts for interactive experiences and building mobile applications that seamlessly integrate generative AI.

Explore the Swift SDK and sample apps

Code sample

Explore more advanced use cases for the Google AI Swift SDK with code samples and demo apps on GitHub.

The Swift code samples demonstrate three key use cases: generating text , photo reasoning (using multimodal inputs) , and multi-turn conversations (chat) . They also show how to use content streaming to improve response time by displaying partial results.

Follow the steps in the README to get started, which includes setting up your Gemini API Key and providing it in a .plist file.

Walkthrough: Building an LLM-powered Text Adventure with Swift

Video Optional

Learn how to use multi-turn conversations with the Gemini model to build an interactive text adventure in Swift.

This practical example walks through building a game with Swift UI and the Google AI Swift SDK to call the Gemini API. Learn more about designing prompts for interactive experiences and building responsive mobile applications that seamlessly integrate generative AI.

Multimodal prompting using the Google AI Swift SDK

Article

Multimodal prompts combine different types of media together, such as text, images, and audio. For example, you could create prompts that identify objects in an image, extract text from a photo, or reference a picture.

To get started, read this guide about file prompting strategies and multimodal concepts, which includes best practices for designing multimodal prompts.

Next, explore the multimodal capabilities of the Gemini models in Google AI Studio by uploading or selecting a file as part of your prompt.

Learn how to use multimodal inputs using the Google AI Swift SDK, find image requirements for prompts , and explore the multimodal image demo in the Swift sample app .

For further reading, see the solution Leveraging the Gemini Pro Vision model for image understanding, multimodal prompts and accessibility .

Prepare for production by migrating to Vertex AI in Firebase

Article

Using the Google AI Swift SDK to call the Gemini API directly from a mobile client is only for prototyping and experimentation. When you start to seriously develop your app beyong prototyping (especially as you prepare for production), transition to use Vertex AI in Firebase and its SDK for Apple platforms.

For calling the Gemini API directly from your Swift app, we strongly recommend using the Vertex AI in Firebase client SDK for Apple platforms. This SDK offers enhanced security features for mobile apps, including Firebase App Check to help protect your app from unauthorized client access. When you use this SDK, you can include large media files in your requests by using Cloud Storage for Firebase. Vertex AI in Firebase also integrates with other products in Google's Firebase developer platform (like Cloud Firestore and Firebase Remote Config), while also giving you streamlined access to the tools, workflows, and scale offered through Google Cloud. Among other differences , Vertex AI also supports increased request quotas and enterprise features.

Follow this guide to migrate to the Vertex AI in Firebase client SDK by updating your package dependencies and imports, as well as changing how the AI model is initialized.

Quiz

Test your knowledge and earn your 'Getting started with Gemini and Swift' badge.