刪除脈絡快取

如要刪除脈絡快取,您需要其快取 ID、 Google Cloud 與脈絡快取相關聯的專案 ID,以及建立脈絡快取要求的處理區域。建立脈絡快取時,系統會傳回脈絡快取的快取 ID。您也可以使用內容快取清單指令,取得與專案相關聯的每個內容快取的快取 ID。

刪除脈絡快取範例

以下範例說明如何刪除內容快取。

Gen AI SDK for Python

安裝

pip install --upgrade google-genai

詳情請參閱 SDK 參考說明文件

設定環境變數,以便透過 Vertex AI 使用 Gen AI SDK:

# 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

client = genai.Client()
# 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

Gen AI SDK for Go

瞭解如何安裝或更新 Gen AI SDK for Go

詳情請參閱 SDK 參考說明文件

設定環境變數,以便透過 Vertex AI 使用 Gen AI SDK:

# 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

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
}

REST

以下說明如何使用 REST 傳送 DELETE 要求至發布商模型端點,藉此刪除與 Google Cloud 專案相關聯的內容快取。

使用任何要求資料之前,請先替換以下項目:

  • PROJECT_ID:您的專案 ID
  • LOCATION:處理建立脈絡快取要求的區域,以及儲存快取內容的區域。
  • CACHE_ID:要刪除的脈絡快取 ID。建立脈絡快取時,系統會傳回脈絡快取 ID。您也可以列出 Google Cloud 專案的脈絡快取,藉此找出脈絡快取 ID。詳情請參閱「建立脈絡快取」和「列出脈絡快取」。

HTTP 方法和網址:

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}

後續步驟