Generar una secuencia de contenido con un modelo de IA multimodal

El código de ejemplo muestra cómo usar modelos de IA generativa para generar texto en formato de streaming a partir de una combinación de entradas de vídeo, imagen y texto.

Investigar más

Para obtener documentación detallada que incluya este código de muestra, consulta lo siguiente:

Código de ejemplo

Go

Antes de probar este ejemplo, sigue las Go instrucciones de configuración de la guía de inicio rápido de Vertex AI con bibliotecas de cliente. Para obtener más información, consulta la documentación de referencia de la API Go de Vertex AI.

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta el artículo Configurar la autenticación en un entorno de desarrollo local.

import (
	"context"
	"fmt"
	"io"

	"google.golang.org/genai"
)

// generateChatStreamWithText shows how to generate chat stream using a text prompt.
func generateChatStreamWithText(w io.Writer) error {
	ctx := context.Background()

	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
	})
	if err != nil {
		return fmt.Errorf("failed to create genai client: %w", err)
	}

	modelName := "gemini-2.5-flash"

	chatSession, err := client.Chats.Create(ctx, modelName, nil, nil)
	if err != nil {
		return fmt.Errorf("failed to create genai chat session: %w", err)
	}

	var streamErr error
	contents := genai.Part{Text: "Why is the sky blue?"}

	stream := chatSession.SendMessageStream(ctx, contents)
	stream(func(resp *genai.GenerateContentResponse, err error) bool {
		if err != nil {
			streamErr = err
			return false
		}
		for _, cand := range resp.Candidates {
			for _, part := range cand.Content.Parts {
				fmt.Fprintln(w, part.Text)
			}
		}
		return true
	})

	// Example response:
	// The
	// sky appears blue due to a phenomenon called **Rayleigh scattering**.
	// Here's a breakdown:
	// ...

	return streamErr
}

Python

Antes de probar este ejemplo, sigue las Python instrucciones de configuración de la guía de inicio rápido de Vertex AI con bibliotecas de cliente. Para obtener más información, consulta la documentación de referencia de la API Python de Vertex AI.

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta el artículo Configurar la autenticación en un entorno de desarrollo local.

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

client = genai.Client(http_options=HttpOptions(api_version="v1"))
chat_session = client.chats.create(model="gemini-2.5-flash")

for chunk in chat_session.send_message_stream("Why is the sky blue?"):
    print(chunk.text, end="")
# Example response:
# The
#  sky appears blue due to a phenomenon called **Rayleigh scattering**. Here's
#  a breakdown of why:
# ...

Siguientes pasos

Para buscar y filtrar ejemplos de código de otros Google Cloud productos, consulta el Google Cloud navegador de ejemplos.