Menghapus cache konteks

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

Contoh penghapusan cache konteks

Contoh berikut menunjukkan cara menghapus cache konteks.

Gen AI SDK for Python

Pelajari cara menginstal atau mengupdate Gen AI SDK for Python.

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="v1beta1"))

# Delete content cache using name
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
client.caches.delete(name=cache_name)
print("Deleted Cache", cache_name)
# Example response
#   Deleted Cache projects/111111111111/locations/us-central1/cachedContents/1111111111111111111

Vertex AI SDK untuk Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Vertex AI SDK untuk Python.

import vertexai

from vertexai.preview import caching

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# cache_id = "your-cache-id"

vertexai.init(project=PROJECT_ID, location="us-central1")

cached_content = caching.CachedContent(cached_content_name=cache_id)
cached_content.delete()

Go

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Go di Panduan memulai Vertex AI menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Go Vertex AI.

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

import (
	"context"
	"fmt"
	"io"

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

// deleteContextCache shows how to delete a cached content
// contentName is the ID of the cached content
func deleteContextCache(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()

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

C#

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan C# di Panduan memulai Vertex AI menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API C# Vertex AI.

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


using Google.Cloud.AIPlatform.V1Beta1;
using System;
using System.Threading.Tasks;

public class DeleteContextCache
{
    public async Task Delete(CachedContentName name)
    {
        var client = await new GenAiCacheServiceClientBuilder
        {
            Endpoint = "us-central1-aiplatform.googleapis.com"
        }.BuildAsync();

        await client.DeleteCachedContentAsync(name);
        Console.WriteLine($"Deleted cache: {name}");
    }
}

REST

Berikut ini menunjukkan cara menggunakan REST untuk menghapus cache konteks yang terkait dengan project Google Cloud dengan mengirim permintaan DELETE ke endpoint model penayang.

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region tempat permintaan untuk membuat cache konteks diproses dan tempat konten yang di-cache disimpan.
  • CACHE_ID: ID cache konteks yang akan dihapus. ID cache konteks ditampilkan saat Anda membuat cache konteks. Anda juga dapat menemukan ID cache konteks dengan mencantumkan cache konteks untuk project Google Cloud menggunakan. Untuk informasi selengkapnya, lihat membuat cache konteks dan mencantumkan cache konteks.

Metode HTTP dan URL:

DELETE https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/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 DELETE `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID" | Select-Object -Expand Content

Jika operasi penghapusan berhasil, responsnya akan kosong:

Contoh perintah curl

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

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

Langkah berikutnya