Integrating Google Gemini AI with Swift and SwiftUI


On the upcoming WWDC, Apple is predicted to announce an on-device massive language mannequin (LLM). The subsequent model of the iOS SDK will seemingly make it simpler for builders to combine AI options into their apps. Whereas we await Apple’s debut of its personal Generative AI fashions, firms like OpenAI and Google already present SDKs for iOS builders to include AI options into cell apps. On this tutorial, we are going to discover Google Gemini, previously generally known as Bard, and display easy methods to use its API to construct a easy SwiftUI app.

We’re set to construct a Q&A app that makes use of the Gemini API. The app incorporates a easy UI with a textual content subject for customers to enter their questions. Behind the scenes, we are going to ship the person’s query to Google Gemini to retrieve the reply.

swiftui-gemini-app-demo

Please word that you must use Xcode 15 (or up) to observe this tutorial.

Getting Began with Google Gemini APIs

Assuming that you just haven’t labored with Gemini, the very very first thing is to go as much as get an API key for utilizing the Gemini APIs. To create one, you’ll be able to go as much as Google AI Studio and click on the Create API key button.

swiftui-google-gemini-api-key

Utilizing Gemini APIs in Swift Apps

You must now have created the API key. We’ll use this in our Xcode undertaking. Open Xcode and create a brand new SwiftUI undertaking, which I’ll name GeminiDemo. To retailer the API key, create a property file named GeneratedAI-Information.plist. On this file, create a key named API_KEY and enter your API key as the worth.

swiftui-gemini-property-list

To learn the API key from the property file, create one other Swift file named APIKey.swift. Add the next code to this file:

When you determine to make use of a special title for the property file as an alternative of the unique ā€˜GenerativeAI-Information.plist’, you have to to switch the code in your ā€˜APIKey.swift’ file. This modification is important as a result of the code references the precise filename when fetching the API key. So, any change within the property file title ought to be mirrored within the code to make sure the profitable retrieval of the API key.

Including the SDK Utilizing Swift Bundle

The Google Gemini SDK is well accessible as a Swift Bundle, making it easy so as to add to your Xcode undertaking. To do that, right-click the undertaking folder within the undertaking navigator and choose Add Bundle Dependencies. Within the dialog, enter the next bundle URL:

You may then click on on the Add Bundle button to obtain and incorporate theĀ GoogleGenerativeAIĀ bundle into the undertaking.

Constructing the App UI

Let’s begin with the UI. It’s easy, with solely a textual content subject for person enter and a label to show responses from Google Gemini.

Open ContentView.swift and declare the next properties:

The textInput variable is used to seize person enter from the textual content subject. The response variable shows the API’s returned response. Given the API’s response time, we embody an isThinking variable to watch the standing and present animated results.

For the physique variable, substitute it with the next code to create the person interface:

The code is sort of easy, particularly when you have some expertise with SwiftUI. After making the modifications, you must see the next person interface within the preview.

swiftui-gemini-app-ui

Integrating with Google Gemini

Earlier than you should use the Google Gemini APIs, you first have to import the GoogleGenerativeAI module:

Subsequent, declare a mannequin variable and initialize the Generative mannequin like this:

Right here, we make the most of the gemini-pro mannequin, which is particularly designed to generate textual content from textual content enter.

To ship the textual content to Google Gemini, let’s create a brand new operate referred to as sendMessage():

As you’ll be able to see from the code above, you solely have to name the generateContent methodology of the mannequin to enter textual content and obtain the generated response. The result’s in Markdown format, so we use LocalizedStringKey to wrap the returned textual content.

To name the sendMessage() operate, replace the TextField view and connect the onSubmit modifier to it:

On this scenario, when the person finishes inputting the textual content and presses the return key, the sendMessage() operate is named to submit the textual content to Google Gemini.

That’s it! Now you can run the app in a simulator or execute it immediately within the preview to check the AI characteristic.

swiftui-generative-ai-demo

Abstract

This tutorial exhibits easy methods to combine Google Gemini AI right into a SwiftUI app. It solely requires a number of strains of code to allow your app with Generative AI options. On this demo, we use the gemini-pro mannequin to generate textual content from text-only enter.

Nevertheless, the capabilities of Gemini AI will not be simply restricted to text-based enter. Gemini additionally affords a multimodal mannequin named gemini-pro-vision that enables builders to enter each textual content and pictures. We encourage you to take full benefit of this tutorial by modifying the offered code and experimenting with it.

When you’ve got any questions concerning the tutorial, please let me know by leaving a remark beneath.



Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles