|
| 1 | +# Google AI SDK for Swift |
| 2 | + |
1 | 3 | [](https://swiftpackageindex.com/google-gemini/generative-ai-swift)
|
2 | 4 | [](https://swiftpackageindex.com/google-gemini/generative-ai-swift)
|
3 | 5 |
|
4 |
| -# Google AI SDK for Swift |
5 |
| - |
6 |
| -> [!CAUTION] |
7 |
| -> **The Google AI SDK for Swift is recommended for prototyping only.** If you plan to enable |
8 |
| -> billing, we strongly recommend that you use a backend SDK to access the Google AI Gemini API. You |
9 |
| -> risk potentially exposing your API key to malicious actors if you embed your API key directly in |
10 |
| -> your Swift app or fetch it remotely at runtime. |
| 6 | +The Google AI Swift SDK is the easiest way for Swift developers to build with |
| 7 | +the Gemini API. The Gemini API gives you access to Gemini |
| 8 | +[models](https://ai.google.dev/models/gemini) created by |
| 9 | +[Google DeepMind](https://deepmind.google/technologies/gemini/#introduction). |
| 10 | +Gemini models are built from the ground up to be multimodal, so you can reason |
| 11 | +seamlessly across text, images, and code. |
| 12 | + |
| 13 | +> [!CAUTION] **The Google AI SDK for Swift is recommended for prototyping |
| 14 | +> only.** If you plan to enable billing, we strongly recommend that you use a |
| 15 | +> backend SDK to access the Google AI Gemini API. You risk potentially exposing |
| 16 | +> your API key to malicious actors if you embed your API key directly in your |
| 17 | +> Swift app or fetch it remotely at runtime. |
| 18 | +
|
| 19 | +## Get started with the Gemini API |
| 20 | + |
| 21 | +1. Go to [Google AI Studio](https://aistudio.google.com/). |
| 22 | +2. Login with your Google account. |
| 23 | +3. [Create an API key](https://aistudio.google.com/app/apikey). Note that in |
| 24 | + Europe the free tier is not available. |
| 25 | +4. Check out this repository. \ |
| 26 | + `git clone https://github.com/google/generative-ai-swift` |
| 27 | +5. Open and build the sample app in the `Examples` folder of this repo. |
| 28 | +6. Run the app once to ensure the build script generates an empty |
| 29 | + `GenerativeAI-Info.plist` file |
| 30 | +7. Paste your API key into the `API_KEY` property in the |
| 31 | + `GenerativeAI-Info.plist` file. |
| 32 | +8. Run the app |
| 33 | +9. For detailed instructions, try the |
| 34 | + [Swift SDK tutorial](https://ai.google.dev/tutorials/swift_quickstart) on |
| 35 | + [ai.google.dev](https://ai.google.dev). |
| 36 | + |
| 37 | +## Usage example |
| 38 | + |
| 39 | +1. Add [`generative-ai-swift`](https://github.com/google/generative-ai-swift) |
| 40 | + to your Xcode project using Swift Package Manager. |
| 41 | + |
| 42 | +2. Import the `GoogleGenerativeAI` module |
11 | 43 |
|
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) |
| 44 | +```swift |
| 45 | +import GoogleGenerativeAI |
| 46 | +``` |
17 | 47 |
|
18 |
| -For example, with just a few lines of code, you can access Gemini's multimodal capabilities to |
19 |
| -generate text from text-and-image input: |
| 48 | +1. Initialize the model |
20 | 49 |
|
21 | 50 | ```swift
|
22 | 51 | let model = GenerativeModel(name: "gemini-1.5-flash-latest", apiKey: "YOUR_API_KEY")
|
| 52 | +``` |
| 53 | + |
| 54 | +1. Run a prompt |
| 55 | + |
| 56 | +```swift |
23 | 57 | let cookieImage = UIImage(...)
|
24 | 58 | let prompt = "Do these look store-bought or homemade?"
|
25 | 59 |
|
26 | 60 | let response = try await model.generateContent(prompt, cookieImage)
|
27 | 61 | ```
|
28 | 62 |
|
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. |
33 |
| - |
34 |
| -To try out the sample app, follow these steps: |
35 |
| - |
36 |
| -1. Check out this repository.\ |
37 |
| -`git clone https://github.com/google/generative-ai-swift` |
38 |
| - |
39 |
| -1. [Obtain an API key](https://makersuite.google.com/app/apikey) to use with the Google AI SDKs. |
40 |
| - |
41 |
| -1. Open and build the sample app in the `Examples` folder of this repo. |
42 |
| - |
43 |
| -1. Run the app once to ensure the build script generates an empty `GenerativeAI-Info.plist` file |
44 |
| - |
45 |
| -1. Paste your API key into the `API_KEY` property in the `GenerativeAI-Info.plist` file. |
46 |
| - |
47 |
| -1. Run the app. |
48 |
| - |
49 |
| -## Use the SDK in your app |
50 |
| - |
51 |
| -Add [`generative-ai-swift`](https://github.com/google/generative-ai-swift) to your Xcode project |
52 |
| -using Swift Package Manager. |
53 |
| - |
54 | 63 | For detailed instructions, you can find a
|
55 |
| -[quickstart](https://ai.google.dev/tutorials/swift_quickstart) for the Google AI SDK for Swift in the |
56 |
| -Google documentation. |
| 64 | +[quickstart](https://ai.google.dev/tutorials/swift_quickstart) for the Google AI |
| 65 | +SDK for Swift in the Google documentation. |
57 | 66 |
|
58 |
| -This quickstart describes how to add your API key and the Swift package to your app, initialize the |
59 |
| -model, and then call the API to access the model. It also describes some additional use cases and |
60 |
| -features, like streaming, counting tokens, and controlling responses. |
| 67 | +This quickstart describes how to add your API key and the Swift package to your |
| 68 | +app, initialize the model, and then call the API to access the model. It also |
| 69 | +describes some additional use cases and features, like streaming, counting |
| 70 | +tokens, and controlling responses. |
61 | 71 |
|
62 | 72 | ## Logging
|
63 | 73 |
|
64 |
| -To enable additional logging in the Xcode console, including a cURL command and raw stream |
65 |
| -response for each model request, add `-GoogleGenerativeAIDebugLogEnabled` as |
66 |
| -`Arguments Passed On Launch` in the Xcode scheme. |
| 74 | +To enable additional logging in the Xcode console, including a cURL command and |
| 75 | +raw stream response for each model request, add |
| 76 | +`-GoogleGenerativeAIDebugLogEnabled` as `Arguments Passed On Launch` in the |
| 77 | +Xcode scheme. |
67 | 78 |
|
68 | 79 | ## Command Line Tool
|
69 | 80 |
|
70 |
| -A command line tool is available to experiment with Gemini model requests via Xcode or the command |
71 |
| -line: |
| 81 | +A command line tool is available to experiment with Gemini model requests via |
| 82 | +Xcode or the command line: |
72 | 83 |
|
73 |
| -1. `open Examples/GenerativeAICLI/Package.swift` |
74 |
| -1. Run in Xcode and examine the console to see the options. |
75 |
| -1. Edit the scheme's `Arguments Passed On Launch` with the desired options. |
| 84 | +1. `open Examples/GenerativeAICLI/Package.swift` |
| 85 | +1. Run in Xcode and examine the console to see the options. |
| 86 | +1. Edit the scheme's `Arguments Passed On Launch` with the desired options. |
76 | 87 |
|
77 | 88 | ## Documentation
|
78 | 89 |
|
79 |
| -Find complete documentation for the Google AI SDKs and the Gemini model in the Google |
80 |
| -documentation: https://ai.google.dev/docs |
| 90 | +See the |
| 91 | +[Gemini API Cookbook](https://github.com/google-gemini/gemini-api-cookbook/) or |
| 92 | +[ai.google.dev](https://ai.google.dev) for complete documentation. |
81 | 93 |
|
82 | 94 | ## Contributing
|
83 | 95 |
|
84 |
| -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. |
87 |
| - |
| 96 | +See |
| 97 | +[Contributing](https://github.com/google/generative-ai-swift/blob/main/docs/CONTRIBUTING.md) |
| 98 | +for more information on contributing to the Google AI SDK for Swift. |
88 | 99 |
|
89 | 100 | ## Developers who use the PaLM SDK for Swift (Deprecated)
|
90 | 101 |
|
91 |
| -> [!IMPORTANT] |
92 |
| -> The PaLM API is deprecated for use with Google AI services and tools (but _not_ for Vertex AI). |
93 |
| -> Learn more about this deprecation, its timeline, and how to migrate to use Gemini in the |
| 102 | +> [!IMPORTANT] The PaLM API is deprecated for use with Google AI services and |
| 103 | +> tools (but *not* for Vertex AI). Learn more about this deprecation, its |
| 104 | +> timeline, and how to migrate to use Gemini in the |
94 | 105 | > [PaLM API deprecation guide](http://ai.google.dev/palm_docs/deprecation).
|
95 | 106 |
|
96 |
| -If you're using the PaLM SDK for Swift, review the information below to continue using the |
97 |
| -**deprecated** PaLM SDK until you've migrated to the new version that allows you to use Gemini. |
| 107 | +If you're using the PaLM SDK for Swift, review the information below to |
| 108 | +continue using the **deprecated** PaLM SDK until you've migrated to the new |
| 109 | +version that allows you to use Gemini. |
98 | 110 |
|
99 |
| -- To continue using PaLM models, make sure your app depends on version |
100 |
| -[`0.3.0`](https://github.com/google/generative-ai-swift/releases/tag/0.3.0) |
101 |
| -_up to_ the next minor version |
102 |
| -([`0.4.0`](https://github.com/google/generative-ai-swift/releases/tag/0.4.0)) |
103 |
| -of `generative-ai-swift`. |
| 111 | +- To continue using PaLM models, make sure your app depends on version |
| 112 | + [`0.3.0`](https://github.com/google/generative-ai-swift/releases/tag/0.3.0) |
| 113 | + *up to* the next minor version |
| 114 | + ([`0.4.0`](https://github.com/google/generative-ai-swift/releases/tag/0.4.0)) |
| 115 | + of `generative-ai-swift`. |
104 | 116 |
|
105 |
| -- When you're ready to use Gemini models, migrate your code to the Gemini API and update your app's |
106 |
| -`generative-ai-swift` dependency to version `0.4.0` or higher. |
| 117 | +- When you're ready to use Gemini models, migrate your code to the Gemini API |
| 118 | + and update your app's `generative-ai-swift` dependency to version `0.4.0` or |
| 119 | + higher. |
107 | 120 |
|
108 | 121 | To see the PaLM documentation and code, go to the
|
109 | 122 | [`palm` branch](https://github.com/google/generative-ai-swift/tree/palm).
|
| 123 | + |
| 124 | +## License |
| 125 | + |
| 126 | +The contents of this repository are licensed under the |
| 127 | +[Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0). |
0 commit comments