在 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 金鑰。Vertex AI 的 Gemini 是透過 IAM 存取權管理,可控管使用者、群組或服務帳戶透過 Vertex AI SDK 呼叫 Gemini API 的權限。

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

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

程式碼範例:安裝用戶端

Google AI Vertex AI Generative Studio 是全代管環境
# To install the Python SDK, use this CLI command:
# pip install google-generativeai

import google.generativeai as genai
from google.generativeai import GenerativeModel

API_KEY=""
genai.configure(api_key=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 Generative Studio 是全代管環境
model = GenerativeModel('gemini-1.5-flash')

response = model.generate_content('The opposite of hot is')
print(response.text) #  The opposite of hot is cold.
        
model = GenerativeModel('gemini-1.5-flash')

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

從文字和圖片生成文字的程式碼範例

Google AI Vertex AI Generative Studio 是全代管環境
import PIL.Image

multimodal_model = GenerativeModel('gemini-1.5-flash')

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.5-flash")

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 Generative Studio 是全代管環境
model = GenerativeModel('gemini-1.5-flash')

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.5-flash")

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 金鑰,請按照安全性最佳做法操作並刪除金鑰。

刪除 API 金鑰的做法如下:

  1. 開啟 Google Cloud API 憑證頁面。

  2. 找出要刪除的 API 金鑰,然後按一下「動作」圖示

  3. 選取「刪除 API 金鑰」

  4. 在「刪除憑證」互動視窗中,選取「刪除」

    刪除 API 金鑰需要幾分鐘的時間才會生效。作業完畢後,凡是使用已刪除 API 金鑰的流量都會遭拒。

後續步驟