Google Generative AI SDK for Swift
The Google Generative AI SDK for Swift allows developers to use state-of-the-art Large Language Models (LLMs) to build language applications.
Once you’ve added the Swift package to your Swift application, you can call the API as follows:
import GoogleGenerativeAI
let palmClient = GenerativeLanguage(apiKey: "YOUR API KEY")
response = try await palmClient.chat(message: "Hello")
Getting Started
This repository contains a few sample apps. To try them out, follow these steps:
- Check out this repository.
git clone https://github.com/google/generative-ai-swift
- Follow the instructions on the setup page to obtain an API key.
- Open and build one of the examples in the
Examples
folder. - Paste the API key into the
API_KEY
property in thePaLM-Info.plist
file. - Run the app.
Using the PaLM SDK in your own app
To use the Swift SDK for the PaLM API in your own apps, follow these steps:
- Create a new Swift app (or use your existing app).
- Right-click on your project in the project navigator.
- Select Add Packages from the context menu.
- In the Add Packages dialog, paste the package URL into the search bar: https://github.com/google/generative-ai-swift
- Click on Add Package. Xcode will now add the GoogleGenerativeAI to your project.
Initializing the API client
Before you can make any API calls, you need to import and initialize the API client.
- Import the
GoogleGenerativeAI
module:import GoogleGenerativeAI
- Initialize the API client:
let palmClient = GenerativeLanguage(apiKey: "YOUR API KEY")
Calling the API
Now you’re ready to call the PaLM API’s methods.
Note: All API methods are asynchronous, so you need to call them using Swift’s async/await.
For example, here is how you can call the generateText
method to summarize a Wikipedia article:
let prompt = "Summarise the following text: https://wikipedia.org/..."
let response = try await palmClient.generateText(with: prompt)
if let candidate = response?.candidates?.first, let text = candidate.output {
print(text)
}
Documentation
You can find the documentation for the PaLM API at https://developers.generativeai.google.com/guide
Contributing
See Contributing for more information on contributing to the Generative AI SDK for Swift.
License
The contents of this repository are licensed under the Apache License, version 2.0.