מחיקת מטמון הקשר

כדי למחוק מטמון הקשר, צריך את מזהה המטמון, את מזהה הפרויקט Google Cloud שאליו משויך מטמון ההקשר ואת האזור שבו טופלה הבקשה ליצור את מטמון ההקשר. מזהה המטמון של מטמון ההקשר מוחזר כשיוצרים את מטמון ההקשר. אפשר גם לאתר את מזהה המטמון של כל מטמון הקשר שמשויך לפרויקט באמצעות הפקודה context cache list.

דוגמה למחיקת מטמון ההקשר

בדוגמה הבאה אפשר לראות איך מוחקים מטמון הקשר.

Python

התקנה

pip install --upgrade google-genai

מידע נוסף מופיע ב מאמרי העזרה בנושא SDK.

מגדירים משתני סביבה כדי להשתמש ב-Gen AI SDK עם 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=global
export GOOGLE_GENAI_USE_VERTEXAI=True

from google import genai

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

Go

כך מתקינים או מעדכנים את Go.

מידע נוסף מופיע ב מאמרי העזרה בנושא SDK.

מגדירים משתני סביבה כדי להשתמש ב-Gen AI SDK עם 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=global
export GOOGLE_GENAI_USE_VERTEXAI=True

import (
	"context"
	"fmt"
	"io"

	genai "google.golang.org/genai"
)

// deleteContentCache shows how to delete content cache.
func deleteContentCache(w io.Writer, cacheName string) 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)
	}

	_, err = client.Caches.Delete(ctx, cacheName, &genai.DeleteCachedContentConfig{})
	if err != nil {
		return fmt.Errorf("failed to delete content cache: %w", err)
	}

	fmt.Fprintf(w, "Deleted cache %q\n", cacheName)

	// Example response:
	// Deleted cache "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111"

	return nil
}

Java

כך מתקינים או מעדכנים את Java.

מידע נוסף מופיע ב מאמרי העזרה בנושא SDK.

מגדירים משתני סביבה כדי להשתמש ב-Gen AI SDK עם 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=global
export GOOGLE_GENAI_USE_VERTEXAI=True


import com.google.genai.Client;
import com.google.genai.types.HttpOptions;

public class ContentCacheDelete {

  public static void main(String[] args) {
    // TODO(developer): Replace these variables before running the sample.
    // E.g cacheName = "projects/111111111111/locations/global/cachedContents/1111111111111111111"
    String cacheName = "your-cache-name";
    contentCacheDelete(cacheName);
  }

  // Deletes the cache using the specified cache name
  public static void contentCacheDelete(String cacheName) {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (Client client =
        Client.builder()
            .location("global")
            .vertexAI(true)
            .httpOptions(HttpOptions.builder().apiVersion("v1").build())
            .build()) {

      client.caches.delete(cacheName, null);
      System.out.println("Deleted cache: " + cacheName);
      // Example response
      // Deleted cache: projects/111111111111/locations/global/cachedContents/1111111111111111111

    }
  }
}

Node.js

התקנה

npm install @google/genai

מידע נוסף מופיע ב מאמרי העזרה בנושא SDK.

מגדירים משתני סביבה כדי להשתמש ב-Gen AI SDK עם 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=global
export GOOGLE_GENAI_USE_VERTEXAI=True

const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';

async function deleteContentCache(
  projectId = GOOGLE_CLOUD_PROJECT,
  location = GOOGLE_CLOUD_LOCATION,
  cacheName = 'example-cache'
) {
  const client = new GoogleGenAI({
    vertexai: true,
    project: projectId,
    location: location,
    httpOptions: {
      apiVersion: 'v1',
    },
  });

  console.log('Removing cache');
  const contentCache = await client.caches.delete({
    name: cacheName,
  });

  console.log(contentCache.text);

  return contentCache;
}
// Example response
//    Deleted Cache projects/111111111111/locations/us-central1/cachedContents/1111111111111111111

REST

בדוגמה הבאה מוצג אופן השימוש ב-REST כדי למחוק מטמון הקשר שמשויך לפרויקט Google Cloud על ידי שליחת בקשת DELETE לנקודת הקצה של המודל של בעל התוכן הדיגיטלי.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה ליצירת מטמון ההקשר עובדה ושבו התוכן שנשמר במטמון מאוחסן.
  • CACHE_ID: המזהה של מטמון ההקשר שרוצים למחוק. מזהה מטמון ההקשר מוחזר כשיוצרים את מטמון ההקשר. אפשר גם למצוא מזהים של מטמון הקשר על ידי הצגת רשימת מטמון ההקשר של פרויקט Google Cloud באמצעות. מידע נוסף זמין במאמרים בנושא יצירת מטמון הקשר ורשימת מטמוני הקשר.

ה-method של ה-HTTP וכתובת ה-URL:

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

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

curl

מריצים את הפקודה הבאה:

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

PowerShell

מריצים את הפקודה הבאה:

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

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

אם פעולת המחיקה מצליחה, התגובה ריקה:

דוגמה לפקודת 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}

המאמרים הבאים