This document shows you how to use the Google Cloud client libraries for Swift to send text and multimodal requests to the Gemini API in Vertex AI and read the responses.
Before you begin
To complete the steps in this document, you need a Google Cloud project with billing enabled and the Vertex AI API enabled. Using Vertex AI models incurs charges; see Vertex AI pricing for details.
- Set up your project and enable the Vertex AI API by following the Vertex AI setup guide.
- Set up the Swift client libraries by following Quickstart: Get started with Swift.
- Verify that your principal has the Vertex AI User Identity and Access Management (IAM)
role (
roles/aiplatform.user) oraiplatform.endpoints.predictpermission.
Add the Vertex AI client library
To add the swift-google-cloud-aiplatform-v1 package and its
GoogleCloudAIPlatformV1 module to your project, follow these steps:
Add the
swift-google-cloud-aiplatform-v1package to yourPackage.swiftdependencies:swift package add-dependency \ https://github.com/googleapis/swift-google-cloud-aiplatform-v1.git --from 0.6.0Add the
GoogleCloudAIPlatformV1module as a dependency of your target:swift package add-target-dependency \ GoogleCloudAIPlatformV1 TARGET_NAME \ --package swift-google-cloud-aiplatform-v1Replace
TARGET_NAMEwith the name of your target.
Send a text prompt
To send a text prompt to the Gemini API in Vertex AI and read the generated text response, follow these steps:
Add the imports needed to use the client library:
Define a function that accepts the project ID as a parameter:
Initialize a
PredictionServiceClientinstance using the default settings:Construct the model resource name:
Build a
GenerateContentRequestobject and callgenerateContent(request:):Check
promptFeedbackfor safety blocks and extract the generated text from the first candidate response:
Send a prompt and an image
Use the same Foundation and GoogleCloudAIPlatformV1 imports and
sample(projectId:) function signature as the previous section. Follow these
steps to send a multimodal request with a Cloud Storage image URI and a text
prompt:
Initialize a
PredictionServiceClientinstance:Construct the model resource name:
Build the image
Partobject with the Cloud Storage URI and MIME type:Build the prompt text
Partobject:Send the multimodal
GenerateContentRequestobject usinggenerateContent(request:):Check
promptFeedbackfor safety blocks and extract the generated text from the first candidate response:
Complete code
The following examples show the complete code for both requests. Call
try await sample(projectId: "PROJECT_ID") from your
app's @main async entry point, replacing PROJECT_ID
with your Google Cloud project ID. Then run swift run to print output
similar to the following:
The image shows scones with blueberries and flowers next to cups of coffee.
Text prompt
Prompt and image
What's next
- Learn how to configure package traits for large
client libraries such as
GoogleCloudAIPlatformV1. - Learn how to override the default endpoint.
- Learn how to override the default authentication credentials.
- Learn how to enable debug logging to troubleshoot requests and responses.
- Browse the Google Cloud client libraries for Swift reference documentation.