Vertex AI で Gemini 1.5 API から Gemini 2.0 API に移行する

このガイドでは、Gemini 1.0 モデルと Gemini 1.5 モデル(Flash と Pro の両方)から Gemini 2.0 モデルに移行する方法について説明します。

Gemini 1.5 と Gemini 2.0 の違い

Gemini 2.0 と 1.0 モデル、1.5 モデルの違いは次のとおりです。

設定

Vertex AI SDK

Vertex AI SDK を再利用する場合、設定プロセスは 1.5 モデルと 2.0 モデルで同じです。詳細については、Vertex AI SDK for Python の概要をご覧ください。

Vertex AI SDK for Python をインストールする簡単なコードサンプルを次に示します。

# pip install --upgrade --quiet google-cloud-aiplatform

import vertexai

vertexai.init(project="PROJECT_ID", location="LOCATION")

PROJECT_ID はプロジェクト ID に、LOCATION はプロジェクトのロケーションに置き換えます( Google Cloud など)。 Google Cloud us-central1

Gen AI SDK

Gen AI SDK を使用する場合、設定プロセスは 1.0 モデルと 1.5/2.0 モデルで異なります。詳細については、Google Gen AI SDK をご覧ください。

Vertex AI SDK for Python をインストールする簡単なコードサンプルを次に示します。

# pip install --upgrade --quiet google-genai

from google import genai

client = genai.Client(vertexai=True, project="PROJECT_ID", location="LOCATION")

PROJECT_ID はプロジェクト ID に、LOCATION はプロジェクトのロケーションに置き換えます( Google Cloud など)。 Google Cloud us-central1

2.0 への移行

以降のセクションでは、Vertex AI SDK と新しい Gen AI SDK の両方から Gemini 2.0 に移行する方法について説明します。

Vertex AI SDK

次のコードサンプルのペアのそれぞれには、Gemini 1.5 コードと、1.5 コードから移行された Gemini 2.0 コードが含まれています。

シンプルなテキスト生成

次のコードサンプルは、テキスト生成モデルの作成における Gemini 1.5 API と Gemini 2.0 API の違いを示しています。

Gemini 1.5 Gemini 2.0
from vertexai.generative_models import GenerativeModel


model = GenerativeModel("gemini-1.5-flash-002")
response = model.generate_content("The opposite of hot is")
print(response.text)
from vertexai.generative_models import GenerativeModel


model = GenerativeModel("gemini-2.0-flash-001")
response = model.generate_content("The opposite of hot is")
print(response.text)

パラメータを使用したテキスト生成

次のコードサンプルは、テキスト生成モデルの作成における Gemini 1.5 API と Gemini 2.0 API の違いを、オプションのパラメータとともに示しています。

Gemini 1.5 Gemini 2.0
from vertexai.generative_models import GenerativeModel


model = GenerativeModel("gemini-1.5-flash-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.generate_content( prompt,generation_config={ "temperature": 0.1,"top_p": 1.0,"top_k": 40,"max_output_tokens": 800,} )

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."""

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

print(response.text)

Gen AI SDK

次のコードサンプルのペアのそれぞれには、Gemini 1.5 コードと、1.5 コードから移行された Gemini 2.0 コードが含まれています。

Gemini 1.5 Gemini 2.0
import vertexai


from vertexai.generative_models import GenerativeModel

vertexai.init(project="PROJECT_ID",
  location="LOCATION")

model = GenerativeModel("gemini-1.5-flash-002")

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

print(response.text)
from google import genai


client = genai.Client(vertexai=True,
  project='YOUR_CLOUD_PROJECT',
  location='us-central1',
  http_options={'api_version': 'v1'})

response = client.models.generate_content(
  model='gemini-2.0-flash-001',
  contents='The opposite of hot is')

print(response.text)