从 Gemini on Google AI 迁移到 Vertex AI

如果您刚开始使用 Gemini,则使用快速入门是最快的入门方法。

但是,随着您的生成式 AI 解决方案的成熟,您可能需要一个用于端到端地构建和部署生成 AI 应用和解决方案的平台。Google Cloud 提供了一个全面的工具生态系统,使开发者能够充分利用生成式 AI 的强大力量(从应用开发的初始阶段到应用部署、应用托管以及大规模管理复杂的数据)。

Google Cloud 的 Vertex AI Platform 提供了一套 MLOps 工具,可简化 AI 模型的使用、部署和监控,以提高效率和可靠性。此外,与数据库、DevOps 工具、日志记录、监控和 IAM 的集成提供了一种全面的方法来管理整个生成式 AI 生命周期。

此外,与数据库、DevOps 工具、日志记录、监控和 IAM 的集成提供了一种全面的管理生成式 AI 生命周期的方法。

以下是一些非常适合 Google Cloud 产品的常见用例。

Google AI 与 Vertex AI 的区别

下表总结了 Google AI 与 Vertex AI 之间的主要区别,可帮助您确定适合您的应用场景的选项:

特性 Google AI Gemini API Google Cloud Vertex AI Gemini API
最新的 Gemini 模型 Gemini Pro 和 Gemini Ultra Gemini Pro 和 Gemini Ultra
注册 Google 账号 Google Cloud 账号(具有条款协议和结算)
Authentication API 密钥 Google Cloud 服务账号
界面平台 Google AI Studio Vertex AI Studio
API 和 SDK Python、Node.js、Android(Kotlin/Java)、Swift、Go SDK 支持 Python、Node.js、Java、Go
免费层级 面向新用户的 300 美元 Google Cloud 赠金
配额(每分钟请求数) 60(可以申请增加配额) 请求增加(默认值:60)
企业支持服务 客户加密密钥
虚拟私有云
数据驻留
Access Transparency
用于应用托管的可伸缩基础架构
数据库和数据存储
MLOps Vertex AI 上的完整 MLOps(示例:模型评估、模型监控、Model Registry)

迁移到 Vertex AI

本部分介绍如何从使用 Google AI Gemini 迁移到 Google Cloud 中的 Vertex AI Gemini。

迁移时的注意事项

迁移时,请考虑以下事项:

  • 您可以使用现有的 Google Cloud 项目(即用于生成 API 密钥的项目),也可以创建新的 Google Cloud 项目

  • Google AI Studio 和 Vertex AI 支持的区域可能会有所不同。请参阅 Google Cloud 上的生成式 AI 支持的区域列表。

  • 您在 Google AI Studio 中创建的任何模型都需要在 Vertex AI 中重新训练。

开始使用 Vertex AI Studio

迁移到 Vertex AI 的过程有所不同,具体取决于您已拥有 Google Cloud 账号,还是第一次使用 Google Cloud。

如需了解如何迁移到 Vertex AI,请根据您的 Google Cloud 账号状态点击以下标签页之一:

已经在使用 Google Cloud

  1. 登录 Google AI Studio
  2. 在左侧导航窗格的底部,点击在 Google Cloud 上使用 Vertex AI 构建

    系统会打开免费试用 Vertex AI 和 Google Cloud 页面。

  3. 点击同意并继续

    系统会显示 Vertex AI Studio 使用入门对话框。

  4. 如需启用运行 Vertex AI 所需的 API,请点击同意并继续

    系统会显示 Vertex AI 控制台。如需了解如何从 Google AI Studio 迁移数据,请参阅迁移提示

Google Cloud 新手

  1. 登录 Google AI Studio
  2. 在左侧导航窗格的底部,点击在 Google Cloud 上使用 Vertex AI 构建

    系统会打开创建账号以开始使用 Google Cloud 页面。

  3. 点击同意并继续

    系统会显示让我们确认您的身份页面。

  4. 点击开始免费试用

    系统会显示 Vertex AI Studio 使用入门对话框。

  5. 如需启用运行 Vertex AI 所需的 API,请点击同意并继续

  6. 可选:如需了解如何从 Google AI Studio 迁移数据,请参阅本页面迁移提示中的“迁移提示”。

Python:迁移到 Vertex AI Gemini API

以下部分显示了代码段,可帮助您迁移 Python 代码以使用 Vertex AI Gemini API。

Vertex AI Python SDK 设置

在 Vertex AI 上,您不需要 API 密钥。不过,Gemini on Vertex AI 会使用 IAM 访问权限进行管理,该访问权限控制用户、群组或服务账号通过 Vertex AI SDK 调用 Gemini API 的权限。

虽然有多种身份验证方法,但在开发环境中,最简单的身份验证方法是安装 Google Cloud CLI,然后使用用户凭据登录 CLI

如需对 Vertex AI 进行推理调用,您还必须确保用户或服务账号具有 Vertex AI User 角色

要安装客户端的代码示例

Google AI Vertex AI

# To install the Python SDK, use this CLI command:
# pip install google-generativeai

from google.generativeai import GenerativeModel
from google.colab import userdata

genai.configure(userdata.get('API_KEY'))
        

# To install the Python SDK, use this CLI command:
# pip install google-cloud-aiplatform

import vertexai
from vertexai.generative_models
          import GenerativeModel, Image

PROJECT_ID = ""
REGION = ""  # e.g. us-central1
vertexai.init(project=PROJECT_ID, location=REGION)
        

用于从文本提示生成文本的代码示例

Google AI Vertex AI

model = GenerativeModel('gemini-1.0-pro')

response = model.generate_content('The opposite of hot is')
print(response.text) #  The opposite of hot is cold.
        

model = GenerativeModel('gemini-1.0-pro')

response = model.generate_content('The opposite of hot is')
print(response.text) #  The opposite of hot is cold.
        

用于从文本和图片生成文本的代码示例

Google AI Vertex AI

import PIL.Image

multimodal_model = GenerativeModel('gemini-1.0-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.
        

multimodal_model = GenerativeModel("gemini-1.0-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.
        

生成多轮聊天的代码示例

Google AI Vertex AI

model = GenerativeModel('gemini-1.0-pro')

chat = model.start_chat()

print(chat.send_message("How are you?").text)
print(chat.send_message("What can you do?").text)
        

model = GenerativeModel("gemini-1.0-pro")

chat = model.start_chat()

print(chat.send_message("How are you?").text)
print(chat.send_message("What can you do?").text)
        

将提示迁移到 Vertex AI Studio

您的 Google AI Studio 提示数据保存在 Google 云端硬盘文件夹中。本部分介绍了如何将提示迁移到 Vertex AI Studio。

  1. 打开 Google 云端硬盘
  2. 转到存储提示的 AI_Studio 文件夹。Google 云端硬盘中提示的位置
  3. 将提示从 Google 云端硬盘下载到本地目录。

  4. 在 Google Cloud 控制台中打开 Vertex AI Generative AI Studio

  5. Vertex AI 菜单中,点击语言

  6. 点击我的提示标签页。

  7. 点击导入提示

  8. 提示文件字段中,点击浏览,然后从本地目录中选择提示。

    如需批量上传提示,您必须手动将提示合并到单个 JSON 文件中。

  9. 点击上传

    提示会上传到我的提示标签页。

将训练数据上传到 Vertex AI Studio

如需将训练数据迁移到 Vertex AI,您需要将数据上传到 Google Cloud Storage 存储桶。如需了解详情,请参阅调整语言基础模型

删除未使用的 API 密钥

如果您不再需要使用 Google AI Gemini API 密钥,请遵循安全性最佳做法并将其删除

后续步骤