在 Vertex AI 上從 PaLM API 遷移至 Gemini API

本指南說明如何將 Vertex AI SDK for Python 程式碼從使用 PaLM API 遷移至使用 Gemini API。你可以使用 Gemini 生成文字、進行多輪對話 (即時通訊) 和生成程式碼。遷移後,請檢查回覆內容,因為 Gemini 的輸出結果可能與 PaLM 不同。

Gemini 與 PaLM 的差異

以下是 Gemini 和 PaLM 模型之間的部分差異:

  • 兩者的回應結構不同。如要瞭解 Gemini 回應結構,請參閱 Gemini API 模型參考資料回應主體

  • 安全類別不同。如要瞭解 Gemini 和 PaLM 安全性設定的差異,請參閱「Gemini 與其他模型系列的重大差異」。

  • Gemini 無法執行程式碼補全功能。如要建立程式碼自動完成應用程式,請使用 code-gecko 模型。詳情請參閱「Codey 程式碼完成模型」。

  • 在程式碼生成方面,Gemini 的背誦區塊率較高。

  • Gemini 不會顯示 Codey 程式碼生成模型中的可信度分數,這項分數代表模型對回覆內容的確定程度。

更新 PaLM 程式碼,改用 Gemini 模型

GenerativeModel 類別的方法大多與 PaLM 類別的方法相同。舉例來說,請使用 GenerativeModel.start_chat 替換 PaLM 對應項目 ChatModel.start_chat。不過,由於 Google Cloud 會持續改善及更新 Gemini,因此可能會出現一些差異。詳情請參閱 Python SDK 參考資料

如要從 PaLM API 遷移至 Gemini API,必須進行下列程式碼修改:

  • 對於所有 PaLM 模型類別,您都會在 Gemini 中使用 GenerativeModel 類別。

  • 如要使用 GenerativeModel 類別,請執行下列匯入陳述式:

    from vertexai.generative_models import GenerativeModel

  • 如要載入 Gemini 模型,請使用 GenerativeModel 建構函式,而非 from_pretrained 方法。舉例來說,如要載入 Gemini 1.0 Pro 模型,請使用 GenerativeModel(gemini-2.0-flash-001)

  • 如要在 Gemini 中生成文字,請使用 GenerativeModel.generate_content 方法,而非 PaLM 模型使用的 predict 方法。例如:

   model = GenerativeModel("gemini-2.0-flash-001")
   response = model.generate_content("Write a short poem about the moon")

Gemini 和 PaLM 類別比較

在 Gemini 中,每個 PaLM 模型類別都會由 GenerativeModel 類別取代。下表列出 PaLM 模型使用的類別,以及 Gemini 中對應的類別。

PaLM 模型 PaLM 模型類別 Gemini 模型類別
text-bison TextGenerationModel GenerativeModel
chat-bison ChatModel GenerativeModel
code-bison CodeGenerationModel GenerativeModel
codechat-bison CodeChatModel GenerativeModel

常見設定操作說明

無論是 Vertex AI 中的 PaLM API 或 Gemini API,設定程序都相同。詳情請參閱 Python 適用的 Vertex AI SDK 簡介。 以下是安裝 Python 適用的 Vertex AI SDK 的簡短程式碼範例。

pip install google-cloud-aiplatform
import vertexai
vertexai.init(project="PROJECT_ID", location="LOCATION")

在這個範例程式碼中,請將 PROJECT_ID 替換為您的 Google Cloud 專案 ID,並將 LOCATION 替換為專案的 Google Cloud 位置 (例如 us-central1)。

Gemini 和 PaLM 程式碼範例

下列每組程式碼範例都包含 PaLM 程式碼,以及從 PaLM 程式碼遷移的 Gemini 程式碼。

文字生成:基本

下列程式碼範例說明如何使用 PaLM API 和 Gemini API 建立文字生成模型。

PaLM Gemini
from vertexai.language_models import TextGenerationModel

model = TextGenerationModel.from_pretrained("text-bison@002")

response = model.predict(prompt="The opposite of hot is")
print(response.text) #  'cold.'
        
from vertexai.generative_models import GenerativeModel

model = GenerativeModel("gemini-2.0-flash-001")

responses = model.generate_content("The opposite of hot is")

for response in responses:
    print(response.text)
        

使用參數生成文字

下列程式碼範例說明如何使用 PaLM API 和 Gemini API 建立文字生成模型,並提供選用參數

PaLM Gemini
from vertexai.language_models import TextGenerationModel

model = TextGenerationModel.from_pretrained("text-bison@002")

prompt = """
You are an expert at solving word problems.

Solve the following problem:

I have three houses, each with three cats.
each cat owns 4 mittens, and a hat. Each mitten was
knit from 7m of yarn, each hat from 4m.
How much yarn was needed to make all the items?

Think about it step by step, and show your work.
"""

response = model.predict(
    prompt=prompt,
    temperature=0.1,
    max_output_tokens=800,
    top_p=1.0,
    top_k=40
)

print(response.text)
        
from vertexai.generative_models import GenerativeModel

model = GenerativeModel("gemini-2.0-flash-001")

prompt = """
You are an expert at solving word problems.

Solve the following problem:

I have three houses, each with three cats.
each cat owns 4 mittens, and a hat. Each mitten was
knit from 7m of yarn, each hat from 4m.
How much yarn was needed to make all the items?

Think about it step by step, and show your work.
"""

responses = model.generate_content(
    prompt,
    generation_config={
        "temperature": 0.1,
        "max_output_tokens": 800,
        "top_p": 1.0,
        "top_k": 40,
    }
  )

for response in responses:
    print(response.text)
        

即時通訊

下列程式碼範例說明如何使用 PaLM API 和 Gemini API 建立即時通訊模型。

PaLM Gemini
from vertexai.language_models import ChatModel

model = ChatModel.from_pretrained("chat-bison@002")

chat = model.start_chat()

print(
    chat.send_message(
        """
Hello! Can you write a 300 word abstract for a research paper I need to write about the impact of AI on society?
"""
    )
)

print(
    chat.send_message(
        """
Could you give me a catchy title for the paper?
"""
    )
)
        
from vertexai.generative_models import GenerativeModel

model = GenerativeModel("gemini-2.0-flash-001")

chat = model.start_chat()


responses = chat.send_message(
        """
Hello! Can you write a 300 word abstract for a research paper I need to write about the impact of AI on society?
"""
    )

for response in responses:
   print(response.text)


responses = chat.send_message(
        """
Could you give me a catchy title for the paper?
"""
    )

for response in responses:
   print(response.text)
        

程式碼生成

下列程式碼範例顯示 PaLM API 和 Gemini API 的差異,兩者都會產生函式,預測某一年是否為閏年。

Codey Gemini
from vertexai.language_models import CodeGenerationModel

model = CodeGenerationModel.from_pretrained("code-bison@002")

response = model.predict(
        prefix="Write a function that checks if a year is a leap year."
    )

print(response.text)
        
from vertexai.generative_models import GenerativeModel

model = GenerativeModel("gemini-2.0-flash-001")

response = model.generate_content("Write a function that checks if a year is a leap year.")

print(response.text)
        

將提示遷移至 Gemini 模型

如果您有先前搭配 PaLM 2 模型使用的提示集,可以透過 Vertex AI 提示最佳化工具 (搶先版) 進行最佳化,以便搭配 Gemini 模型使用。

後續步驟

  • 如要進一步瞭解最新型號和功能,請參閱「Google 型號」頁面。