Migra de Gemini 1.5 a Gemini 2.0 con la API de Gemini 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 y migración

SDK de IA generativa

Te recomendamos que migres al SDK de Gen AI cuando actualices a Gemini 2.0.

Si decides usar el SDK de Gen AI, el proceso de configuración es diferente al del SDK de Vertex AI.

Para obtener más información, visita el SDK de IA generativa de Google.

Instalar

pip install --upgrade google-genai
Para obtener más información, consulta la documentación de referencia del SDK.

Establece variables de entorno para usar el SDK de Gen AI con Vertex AI:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=us-central1
export GOOGLE_GENAI_USE_VERTEXAI=True

from google import genai
from google.genai.types import HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
    model="gemini-2.0-flash-001",
    contents="How does AI work?",
)
print(response.text)
# Example response:
# Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
#
# Here's a simplified overview:
# ...

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

SDK de Vertex AI

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

Instala el SDK:

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

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

import vertexai
from vertexai.generative_models import GenerativeModel

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

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

response = model.generate_content(
    "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?"
)

print(response.text)
# Example response:
# **Emphasizing the Dried Aspect:**
# * Everlasting Blooms
# * Dried & Delightful
# * The Petal Preserve
# ...

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). Luego, cambia el ID del modelo de gemini-1.5-flash-002 a gemini-2.0-flash.