在 Google Cloud 中使用 Gemini 建構內容

如果您是第一次使用 Gemini,可以透過快速入門導覽課程快速上手。

然而,隨著生成式 AI 解決方案日漸成熟,您可能需要一個端對端建構及部署生成式 AI 應用程式和解決方案的平台。Google Cloud 提供完善的工俱生態系統,讓開發人員能充分發揮生成式 AI 的效用,無論是應用程式開發的初期階段,還是應用程式部署、應用程式託管及管理複雜的資料,都能輕鬆完成。

Google Cloud 的 Vertex AI 平台提供一套機器學習運作工具,簡化 AI 模型的使用、部署及監控作業,藉此提高效率和可靠性。另外,這項服務與資料庫、開發運作工具、記錄、監控和 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 帳戶 (含條款協議和帳單)
驗證 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)
企業支援 客戶加密金鑰
虛擬私有雲
資料落地
資料存取透明化控管機制
適用於應用程式託管的可擴充基礎架構
資料庫和資料儲存空間
機器學習運作 Vertex AI 中完整的機器學習運作 (範例:模型評估、模型監控、Model Registry)

如要瞭解哪些產品、架構和工具最適合在 Google Cloud 中建構生成式 AI 應用程式,請參閱「在 Google Cloud 上建構生成式 AI 應用程式」一文。

從 Google AI 上的 Gemini 遷移至 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 金鑰。而是使用 IAM 存取權管理 Vertex AI 中的 Gemini 模型,進而控管使用者、群組或服務帳戶透過 Vertex AI SDK 呼叫 Gemini API 的權限。

雖然有多種驗證方式,但最簡單的在開發環境中進行驗證的方法,就是安裝 Google Cloud CLI,然後使用您的使用者憑證登入 CLI

如要呼叫 Vertex AI,您也必須確認您的使用者或服務帳戶具備 Vertex AI 使用者角色

安裝用戶端的程式碼範例

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 金鑰,請遵循安全性最佳做法並刪除

後續步驟