Migra de la API de Gemini 1.5 a la API de Gemini 2.0 en Vertex AI

En esta guía, se muestra cómo migrar de los modelos Gemini 1.0 y Gemini 1.5 (tanto Flash como Pro) a los modelos Gemini 2.0.

Diferencias entre Gemini 1.5 y Gemini 2.0

Las siguientes son algunas diferencias entre Gemini 2.0 y nuestros modelos 1.0 y 1.5:

Configuración

SDK de Vertex AI

Si reutilizas el SDK de Vertex AI , el proceso de configuración es el mismo para nuestros modelos 1.5 y 2.0. Para obtener más información, consulta Introducción al SDK de Vertex AI para Python.

La siguiente es una muestra de código corto que instala el SDK de Vertex AI para Python:

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

import vertexai

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

Reemplaza PROJECT_ID por el ID de tu Google Cloud proyecto y LOCATION por la ubicación de tu Google Cloud proyecto (por ejemplo, us-central1).

SDK de IA generativa

Si decides usar el SDK de IA generativa, el proceso de configuración es diferente entre los modelos 1.0 y 1.5/2.0. Para obtener más información, visita los SDK de Google Gen AI.

La siguiente es una muestra de código corto que instala el SDK de Gen AI para Python:

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

from google import genai

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

Reemplaza PROJECT_ID por el ID de tu Google Cloud proyecto y LOCATION por la ubicación de tu Google Cloud proyecto (por ejemplo, us-central1).

Cómo migrar a la versión 2.0

En las siguientes secciones, se incluyen instrucciones para migrar a Gemini 2.0 desde el SDK de Vertex AI y nuestro nuevo SDK de Gen AI.

SDK de Vertex AI

Cada uno de los siguientes pares de muestras de código incluye código Gemini 1.5 y código Gemini 2.0 que se migró desde el código 1.5.

Generación de texto simple

En las siguientes muestras de código, se muestran las diferencias entre la API de Gemini 1.5 y la de Gemini 2.0 para crear un modelo de generación 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)

Generación de texto con parámetros

En las siguientes muestras de código, se muestran las diferencias entre la API de Gemini 1.5 y la de Gemini 2.0 para crear un modelo de generación de texto con parámetros opcionales:

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 de IA generativa

Cada uno de los siguientes pares de muestras de código incluye código Gemini 1.5 y código Gemini 2.0 que se migró desde el 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)