Mendapatkan informasi tentang cache konteks

Anda dapat mempelajari waktu pembuatan context cache, waktu pembaruan terakhir, dan waktu habis masa berlakunya. Untuk mendapatkan informasi tentang setiap cache konteks yang terkait dengan project Google Cloud , termasuk ID cache-nya, gunakan perintah untuk mencantumkan cache konteks. Jika mengetahui ID cache dari cache konteks, Anda dapat memperoleh informasi hanya tentang cache konteks tersebut.

Mendapatkan daftar context cache

Untuk mendapatkan daftar cache konteks yang terkait dengan project Google Cloud , Anda memerlukan region tempat Anda membuat dan ID project Google Cloud . Berikut ditunjukkan cara mendapatkan daftar cache konteks untuk project Google Cloud .

Python

Instal

pip install --upgrade google-genai

Untuk mempelajari lebih lanjut, lihat dokumentasi referensi SDK.

Tetapkan variabel lingkungan untuk menggunakan Gen AI SDK dengan 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"))

content_cache_list = client.caches.list()

# Access individual properties of a ContentCache object(s)
for content_cache in content_cache_list:
    print(f"Cache `{content_cache.name}` for model `{content_cache.model}`")
    print(f"Last updated at: {content_cache.update_time}")
    print(f"Expires at: {content_cache.expire_time}")

# Example response:
# * Cache `projects/111111111111/locations/us-central1/cachedContents/1111111111111111111` for
#       model `projects/111111111111/locations/us-central1/publishers/google/models/gemini-XXX-pro-XXX`
# * Last updated at: 2025-02-13 14:46:42.620490+00:00
# * CachedContentUsageMetadata(audio_duration_seconds=None, image_count=167, text_count=153, total_token_count=43130, video_duration_seconds=None)
# ...

REST

Berikut ini cara menggunakan REST untuk mencantumkan cache konteks yang terkait dengan project Google Cloud dengan mengirim permintaan GET ke endpoint model publisher.

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

Metode HTTP dan URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents"

PowerShell

Jalankan perintah berikut:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents" | Select-Object -Expand Content

Anda akan menerima respons JSON yang mirip dengan yang berikut ini:

Contoh perintah curl

LOCATION="us-central1"
PROJECT_ID="PROJECT_ID"

curl \
-X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/cachedContents

Mendapatkan informasi tentang context cache

Untuk mendapatkan informasi tentang satu cache konteks, Anda memerlukan ID cache-nya,Google Cloud ID project yang terkait dengan cache konteks, dan region tempat permintaan untuk membuat cache konteks diproses. ID cache context cache ditampilkan saat Anda membuat context cache. Anda juga bisa mendapatkan ID cache setiap cache konteks yang terkait dengan project menggunakan perintah daftar cache konteks.

Berikut ini menunjukkan cara mendapatkan informasi tentang satu cache konteks.

Go

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Go di panduan memulai Vertex AI. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Vertex AI Go SDK untuk Gemini.

Untuk melakukan autentikasi ke Vertex AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal.

Respons streaming dan non-streaming

Anda dapat memilih apakah model menghasilkan respons streaming atau respons non-streaming. Untuk respons streaming, Anda akan menerima setiap respons segera setelah token outputnya dibuat. Untuk respons non-streaming, Anda akan menerima semua respons setelah semua token output dibuat.

Untuk respons streaming, gunakan metode GenerateContentStream.

  iter := model.GenerateContentStream(ctx, genai.Text("Tell me a story about a lumberjack and his giant ox. Keep it very short."))
  

Untuk respons non-streaming, gunakan metode GenerateContent.

  resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
  

Kode contoh

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/vertexai/genai"
)

// getContextCache shows how to retrieve the metadata of a cached content
// contentName is the ID of the cached content to retrieve
func getContextCache(w io.Writer, contentName string, 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()

	cachedContent, err := client.GetCachedContent(ctx, contentName)
	if err != nil {
		return fmt.Errorf("GetCachedContent: %w", err)
	}
	fmt.Fprintf(w, "Retrieved cached content %q", cachedContent.Name)
	return nil
}

REST

Berikut ini cara menggunakan REST untuk mencantumkan cache konteks yang terkait dengan project Google Cloud dengan mengirim permintaan GET ke endpoint model publisher.

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: .
  • LOCATION: Region tempat permintaan untuk membuat cache konteks diproses.
  • CACHE_ID: ID cache konteks. ID context cache ditampilkan saat Anda membuat context cache. Anda juga dapat menemukan ID context cache dengan mencantumkan context cache untuk project Google Cloud menggunakan. Untuk mengetahui informasi selengkapnya, lihat membuat context cache dan mencantumkan context cache.

Metode HTTP dan URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID"

PowerShell

Jalankan perintah berikut:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID" | Select-Object -Expand Content

Anda akan menerima respons JSON yang mirip dengan yang berikut ini:

Contoh perintah curl

LOCATION="us-central1"
PROJECT_ID="PROJECT_ID"
CACHE_ID="CACHE_ID"

curl \
-X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/${CACHE_ID}