Migrar da API Gemini 1.5 para a API Gemini 2.0 na Vertex AI

Este guia mostra como migrar dos modelos Gemini 1.0 e Gemini 1.5 (Flash e Pro) para os modelos Gemini 2.0.

Diferenças entre o Gemini 1.5 e o Gemini 2.0

Confira a seguir algumas diferenças entre o Gemini 2.0 e os modelos 1.0 e 1.5:

Configuração

SDK da Vertex AI

Se você reutilizar o SDK da Vertex AI , o processo de configuração será o mesmo para os modelos 1.5 e 2.0. Para mais informações, consulte Introdução ao SDK da Vertex AI para Python.

Confira a seguir um exemplo de código curto que instala o SDK da Vertex AI para Python:

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

import vertexai

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

Substitua PROJECT_ID pelo ID do projeto Google Cloud e LOCATION pelo local do projeto Google Cloud (por exemplo, us-central1).

SDK da Gen AI

Se você optar por usar o SDK da IA generativa, o processo de configuração será diferente entre os modelos 1.0 e 1.5/2.0. Para mais informações, acesse os SDKs do Google Gen AI.

Confira a seguir um exemplo de código curto que instala o SDK da Gen AI para Python:

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

from google import genai

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

Substitua PROJECT_ID pelo ID do projeto Google Cloud e LOCATION pelo local do projeto Google Cloud (por exemplo, us-central1).

Migrar para a versão 2.0

As seções a seguir incluem instruções sobre como migrar para o Gemini 2.0 do SDK da Vertex AI e do novo SDK da IA generativa.

SDK da Vertex AI

Cada um dos pares de exemplos de código a seguir inclui o código do Gemini 1.5 e do Gemini 2.0 que foi migrado do código 1.5.

Geração de texto simples

Os exemplos de código a seguir mostram as diferenças entre a API Gemini 1.5 e a API Gemini 2.0 para criar um modelo de geração de texto:

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)

Geração de texto com parâmetros

Os exemplos de código a seguir mostram as diferenças entre a API Gemini 1.5 e a API Gemini 2.0 para criar um modelo de geração de texto com parâmetros opcionais:

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)

SDK da Gen AI

Cada um dos pares de exemplos de código a seguir inclui o código do Gemini 1.5 e do Gemini 2.0 que foi migrado do código 1.5:

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)