# Run Gemini on Google Cloud

- # Migrate to the Cloud

- In this guide, learn why you might want to migrate your development projects to Google Cloud and how to migrate your Python code from the Google AI Gemini API to the Gemini API on Vertex AI.

- ## Why Migrate to Cloud

- If you're new to Gemini, refer to the [Quickstart](https://ai.google/dev/tutorials/ai-studio_quickstart?hl=zh-cn) and [samples](https://ai.google.dev/pricing?hl=zh-cn) in [Google AI Studio](https://ai.google/dev/tutorials/ai-studio_quickstart?hl=zh-cn) to get started quickly.

- However, as your generative AI solutions mature, you might need a platform to build and deploy end-to-end generative AI applications and solutions. Google Cloud provides a comprehensive ecosystem of tools that empower developers to harness the power of generative AI, from the initial stages of application deployment to managing complex data at scale. Google Cloud's Vertex AI platform offers a suite of MLOps tools that streamline the consumption, deployment, and monitoring of AI models to improve efficiency and reliability.

- The following table summarizes the key differences between Google AI and Vertex AI to help you determine which option is suitable for your use case:

- | **Feature**                 | **Google AI Gemini API**                                     | **Google Cloud Vertex AI Gemini API**                        |
  | :-------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- |
  | Latest Gemini Models        | Gemini Pro and Gemini Ultra                                  | Gemini Pro and Gemini Ultra                                  |
  | Sign-up                     | Google Account                                               | Google Cloud Account (including Terms Agreement and Billing) |
  | Authentication              | API Key                                                      | Google Cloud Service Account                                 |
  | Playground                  | Google AI Studio                                             | Vertex AI Studio                                             |
  | API and SDK                 | Python, Node.js, Android (Kotlin/Java), Swift, Go            | SDK supports Python, Node.js, Java, Go                       |
  | Free Tier                   | Yes                                                          | $300 Google Cloud credits for new users                      |
  | Quota (Requests Per Minute) | [60 (can request increase)](https://ai.google.dev/docs/increase_quota?hl=zh-cn) | Increase on request (default: 60)                            |
  | Enterprise Support          | No                                                           | Data privacy commitments, customer-managed encryption keys, Virtual Private Cloud (VPC), data residency, access transparency |
  | MLOps                       | No                                                           | Full MLOps on Vertex AI (e.g., model evaluation, model monitoring, model registry) |

- Here are other considerations to keep in mind when migrating:

- - You can use an existing Google Cloud project (i.e., the project you used to generate the API key) or [create a new Google Cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects?hl=zh-cn).
  - The regions supported by Google AI Studio and Vertex AI may vary. Refer to the list of [Google Cloud regions that support generative AI](https://cloud.google.com/vertex-ai/docs/generative-ai/learn/locations-genai?hl=zh-cn).
  - Any models you created in Google AI Studio will need to be retrained in Vertex AI.

- Additionally, integration with databases, DevOps tools, logging, monitoring, and IAM provides a comprehensive approach to managing the entire generative AI lifecycle.

- Here are some examples of common use cases that are well-suited for Google Cloud products.

- - Put your applications and solutions into production. With products such as [Cloud Functions](https://cloud.google.com/functions/docs/concepts/overview?hl=zh-cn) and [Cloud Run](https://cloud.google.com/run/docs/overview/what-is-cloud-run?hl=zh-cn), you can deploy applications with enterprise-grade scale, security, and privacy. For more information about security and privacy, see the [Security, Privacy, and Cloud Compliance on Google Cloud](https://cloud.google.com/security?hl=zh-cn) guide.
  - Achieve end-to-end MLOps capabilities with Vertex AI, from tuning to vector similarity search and machine learning pipelines.
  - Trigger LLM (Large Language Model) calls using event-driven architecture with [Cloud Functions](https://cloud.google.com/functions/docs/concepts/overview?hl=zh-cn) or [Cloud Run](https://cloud.google.com/run/docs/overview/what-is-cloud-run?hl=zh-cn).
  - Monitor application usage with [Cloud Logging](https://cloud.google.com/logging/docs?hl=zh-cn) and [BigQuery](https://cloud.google.com/logging/docs/export/bigquery?hl=zh-cn).
  - Store data at scale with enterprise-grade security using services such as [BigQuery](https://cloud.google.com/bigquery/docs?hl=zh-cn), [Cloud Storage](https://cloud.google.com/storage/docs/introduction?hl=zh-cn), and [Cloud SQL](https://cloud.google.com/sql?hl=zh-cn).
  - Perform Retrieval Augmented Generation (RAG) using data in the cloud via [BigQuery](https://cloud.google.com/bigquery/docs?hl=zh-cn) or [Cloud Storage](https://cloud.google.com/storage/docs/introduction?hl=zh-cn).
  - Create and schedule data pipelines. You can [schedule jobs](https://cloud.google.com/scheduler/docs/schedule-run-cron-job?hl=zh-cn) using [Cloud Scheduler](https://cloud.google.com/scheduler/docs/overview?hl=zh-cn).
  - Bring LLMs to your data in the cloud. If you have data stored in Cloud Storage or BigQuery, you can prompt LLMs to process that data. For example, extract information, summarize information, or ask questions.
  - Leverage Google Cloud's [data governance/residency](https://cloud.google.com/learn/what-is-data-governance?hl=zh-cn) policies to manage the data lifecycle.

- ## Migrate from Gemini on Google AI to Vertex AI

- This section describes how to migrate from using Google AI Gemini to Google Cloud's Vertex AI Gemini.

- ### Get Started with Vertex AI Studio

- The easiest way to get started with Gemini through Vertex AI is to use Vertex AI Studio.

- 1. If you previously created an API key through Google AI Studio, a Google Cloud project has already been created for you, and you can use the same project. To find your Google Cloud project, go to API Keys in Google AI Studio.

- ![image.png](https://api.apidog.com/api/v1/projects/3757320/resources/414778/image-preview)

- 1. Otherwise, you will need to create a Google Cloud project and enable the Vertex AI API. For instructions, see the [Set up your project](https://cloud.google.com/vertex-ai/docs/start/cloud-environment?hl=zh-cn) guide.
  2. Go to the [Google Cloud Console](https://console.cloud.google.com/?hl=zh-cn).
  3. [Enable billing](https://cloud.google.com/billing/docs/how-to/modify-project?hl=zh-cn#how-to-enable-billing) for your Google Cloud project, if you haven't already. New Google Cloud users get [$300] in free credits.
  4. Go to [Vertex AI Studio](https://console.cloud.google.com/vertex-ai/generative?hl=zh-cn) via this link. You can also search for "Vertex AI Studio" in the search bar.

- Now you can start using Vertex AI Studio. For more information, see the [Vertex AI Studio documentation](https://cloud.google.com/vertex-ai/docs/generative-ai/learn/generative-ai-studio?hl=zh-cn).

- ### Python: Migrate from Google AI Gemini API to Vertex AI Gemini API

- The following sections show code snippets to help you migrate your Python code to use the Vertex AI Gemini API.

- #### Vertex AI Python SDK Setup

- On Vertex AI, you do not need an API key. Gemini on Vertex AI uses IAM (Identity and Access Management) access controls that govern what users, groups, or service accounts can do when calling the Gemini API through the Vertex AI SDK.

- There are many ways to authenticate. You can follow this [decision tree to choose the appropriate authentication method for your use case](https://cloud.google.com/docs/authentication?hl=zh-cn#auth-decision-tree).

- To make inference calls to Vertex AI, you must also ensure that you have enabled [Vertex AI User](https://cloud.google.com/vertex-ai/docs/general/access-control?hl=zh-cn#aiplatform.user).

- #### Code Example for Installing the Client

- | Google AI                                                    | Vertex AI                                                    |
  | :----------------------------------------------------------- | :----------------------------------------------------------- |
  | `pythonpip install google-generativeaifrom google.generativeai import GenerativeModelfrom google.colab import userdatagenai.configure(userdata.get('API_KEY'))` | `pythonpip install google-cloud-aiplatformimport vertexaifrom google.cloud.aiplatform.private_preview.generative_models import GenerativeModel, ImagePROJECT_ID = ""REGION = "" # e.g. us-central1vertexai.init(project=PROJECT_ID, location=REGION)` |

- #### Code Example for Generating Text from a Text Prompt

- | Google AI                                                    | Vertex AI                                                    |
  | :----------------------------------------------------------- | :----------------------------------------------------------- |
  | `pythonmodel = GenerativeModel('gemini-pro')response = model.generate_content('The opposite of hot is')print(response.text) # The opposite of hot is cold.` | `pythonmodel = GenerativeModel('gemini-pro')response = model.generate_content('The opposite of hot is')print(response.text) # The opposite of hot is cold.` |

- #### Code Example for Generating Text from Text and Image

- | Google AI                                                    | Vertex AI                                                    |
  | :----------------------------------------------------------- | :----------------------------------------------------------- |
  | `pythonimport PIL.Imagemultimodal_model = GenerativeModel('gemini-pro-vision')image = PIL.Image.open('image.jpg')response = multimodal_model.generate_content(['What is this picture?', image])print(response.text) # A cat is shown in this picture.` | `pythonmultimodal_model = GenerativeModel("gemini-pro-vision")image = Image.load_from_file("image.jpg")response = multimodal_model.generate_content(["What is shown in this image?", image])print(response.text) # A cat is shown in this picture.` |

- #### Code Example for Generating Multi-Turn Chat

- | Google AI                                                    | Vertex AI                                                    |
  | :----------------------------------------------------------- | :----------------------------------------------------------- |
  | `pythonmodel = GenerativeModel('gemini-pro')chat = model.start_chat()print(chat.send_message("How are you?").text)print(chat.send_message("What can you do?").text)` | `pythonmodel = GenerativeModel("gemini-pro")chat = model.start_chat()print(chat.send_message("How are you?").text)print(chat.send_message("What can you do?").text)` |

- ## Delete Unused API Keys

- If you no longer need to use the Google AI Gemini API key, follow security best practices and [delete it](https://developers.google.com/maps/api-security-best-practices?hl=zh-cn#deleting-unused-apikeys).

- ## Next Steps

- - See the [Vertex AI overview](https://cloud.google.com/vertex-ai/docs/generative-ai/learn/overview?hl=zh-cn) to learn more about generative AI solutions on Vertex AI.
  - Review the [Gemini API overview](https://ai.google.dev/docs/gemini_api_overview?hl=zh-cn) to dive deeper into the Google AI Gemini API

