在 Google Cloud 上使用 Gemini 构建

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

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

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

如需了解哪些产品、框架和工具最适合在 Google Cloud 上构建生成式 AI 应用,请参阅在 Google Cloud 上构建生成式 AI 应用

从 Gemini on Google AI 迁移到 Vertex AI

如果您的应用使用 Google AI Gemini API,您需要迁移到 Google Cloud 的 Vertex AI Gemini API。

迁移时:

Python:从 Google AI Gemini API 迁移到 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)
        

删除未使用的 API 密钥

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

后续步骤