You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Google AI Swift SDK is the easiest way for Swift developers to build with the Gemini API. The Gemini API gives you access to Gemini [models](https://ai.google.dev/models/gemini) created by [Google DeepMind](https://deepmind.google/technologies/gemini/#introduction). Gemini models are built from the ground up to be multimodal, so you can reason seamlessly across text, images, and code.
5
7
6
8
> [!CAUTION]
7
9
> **The Google AI SDK for Swift is recommended for prototyping only.** If you plan to enable
8
10
> billing, we strongly recommend that you use a backend SDK to access the Google AI Gemini API. You
9
11
> risk potentially exposing your API key to malicious actors if you embed your API key directly in
10
12
> your Swift app or fetch it remotely at runtime.
11
13
12
-
The Google AI SDK for Swift enables developers to use Google's state-of-the-art generative AI models
13
-
(like Gemini) to build AI-powered features and applications. This SDK supports use cases like:
14
-
- Generate text from text-only input
15
-
- Generate text from text-and-images input (multimodal)
16
-
- Build multi-turn conversations (chat)
17
-
18
14
For example, with just a few lines of code, you can access Gemini's multimodal capabilities to
19
15
generate text from text-and-image input:
20
16
21
-
```swift
22
-
let model =GenerativeModel(name: "gemini-1.5-flash-latest", apiKey: "YOUR_API_KEY")
23
-
let cookieImage =UIImage(...)
24
-
let prompt ="Do these look store-bought or homemade?"
25
17
26
-
let response =tryawait model.generateContent(prompt, cookieImage)
27
-
```
28
-
29
-
## Try out the sample Swift app
30
-
31
-
This repository contains a sample app demonstrating how the SDK can access and utilize the Gemini
32
-
model for various use cases.
18
+
## Get started with the Gemini API
19
+
1. Go to [Google AI Studio](https://aistudio.google.com/).
20
+
2. Login with your Google account.
21
+
3.[Create](https://aistudio.google.com/app/apikey) an API key. Note that in Europe the free tier is not available.
5. Open and build the sample app in the `Examples` folder of this repo.
25
+
6. Run the app once to ensure the build script generates an empty `GenerativeAI-Info.plist` file
26
+
7. Paste your API key into the `API_KEY` property in the `GenerativeAI-Info.plist` file.
27
+
8. Run the app
28
+
9. For detailed instructions, try the [Swift SDK tutorial](https://ai.google.dev/tutorials/swift_quickstart) on [ai.google.dev](https://ai.google.dev).
Add [`generative-ai-swift`](https://github.com/google/generative-ai-swift) to your Xcode project
33
+
using Swift Package Manager.
38
34
39
-
1.[Obtain an API key](https://makersuite.google.com/app/apikey) to use with the Google AI SDKs.
35
+
Import the `GoogleGenerativeAI` module
40
36
41
-
1. Open and build the sample app in the `Examples` folder of this repo.
37
+
```swift
38
+
importGoogleGenerativeAI
39
+
```
42
40
43
-
1. Run the app once to ensure the build script generates an empty `GenerativeAI-Info.plist` file
41
+
Initialize the model
44
42
45
-
1. Paste your API key into the `API_KEY` property in the `GenerativeAI-Info.plist` file.
43
+
```swift
44
+
let model =GenerativeModel(name: "gemini-1.5-flash-latest", apiKey: "YOUR_API_KEY")
45
+
```
46
46
47
-
1.Run the app.
47
+
Run a prompt
48
48
49
-
## Use the SDK in your app
49
+
```swift
50
+
let cookieImage =UIImage(...)
51
+
let prompt ="Do these look store-bought or homemade?"
50
52
51
-
Add [`generative-ai-swift`](https://github.com/google/generative-ai-swift) to your Xcode project
52
-
using Swift Package Manager.
53
+
let response =tryawait model.generateContent(prompt, cookieImage)
54
+
```
53
55
54
56
For detailed instructions, you can find a
55
57
[quickstart](https://ai.google.dev/tutorials/swift_quickstart) for the Google AI SDK for Swift in the
@@ -76,14 +78,12 @@ line:
76
78
77
79
## Documentation
78
80
79
-
Find complete documentation for the Google AI SDKs and the Gemini model in the Google
80
-
documentation: https://ai.google.dev/docs
81
+
See the [Gemini API Cookbook](https://github.com/google-gemini/gemini-api-cookbook/) or [ai.google.dev](https://ai.google.dev) for complete documentation.
81
82
82
83
## Contributing
83
84
84
85
See [Contributing](https://github.com/google/generative-ai-swift/blob/main/docs/CONTRIBUTING.md)
85
-
for more information on
86
-
contributing to the Google AI SDK for Swift.
86
+
for more information on contributing to the Google AI SDK for Swift.
87
87
88
88
89
89
## Developers who use the PaLM SDK for Swift (Deprecated)
0 commit comments