Lista de elementos de contenido almacenado en caché

En esta muestra de código, se muestra cómo enumerar todos los elementos de contenido almacenados en caché de un proyecto y una ubicación determinados.

Explora más

Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:

Muestra de código

Go

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

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/vertexai/genai"
	"google.golang.org/api/iterator"
)

// listContextCaches retrieves all context caches associated with the specified
// Google Cloud project and region
func listContextCaches(w io.Writer, projectID, location string) error {
	// location := "us-central1"
	ctx := context.Background()

	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		return fmt.Errorf("unable to create client: %w", err)
	}
	defer client.Close()

	cacheList := client.ListCachedContents(ctx)
	// `cacheList` is a standard Google API iterator.
	// See https://pkg.go.dev/google.golang.org/api/iterator#example-package for more details
	for {
		item, err := cacheList.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("listContextCaches failed: %w", err)
		}

		fmt.Fprintf(w, "Cache %q will expire at %v\n", item.Name, item.Expiration.ExpireTime.String())
		// Example response:
		// Cache "projects/.../locations/.../cachedContents/12345678900000000" will expire at 2024-10-25 09:13:58.67004 +0000 UTC
	}

	return nil
}

Python

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

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

import vertexai

from vertexai.preview import caching

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

cache_list = caching.CachedContent.list()
# Access individual properties of a CachedContent object
for cached_content in cache_list:
    print(f"Cache '{cached_content.name}' for model '{cached_content.model_name}'")
    print(f"Last updated at: {cached_content.update_time}")
    print(f"Expires at: {cached_content.expire_time}")
    # Example response:
    # Cached content 'example-cache' for model '.../gemini-1.5-pro-001'
    # Last updated at: 2024-09-16T12:41:09.998635Z
    # Expires at: 2024-09-16T13:41:09.989729Z

¿Qué sigue?

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