更新脈絡快取

您可以更新脈絡快取的到期時間。根據預設,背景資訊快取會在建立時間的 60 分鐘後失效。過期的脈絡快取會在垃圾收集程序中刪除,無法使用或更新。如要更新未過期脈絡快取的到期時間,請更新下列其中一個屬性:

  • ttl - 快取在建立後或 ttl 更新後,到期前的存留時間 (以秒和奈秒為單位)。設定 ttl 時,快取的 expireTime 會更新。

  • expire_time - A Timestamp,指定脈絡快取到期的絕對日期和時間。

使用脈絡快取的 ttl 參數更新快取

以下是 curl 指令範例,可將到期時間延長 3,600 秒。

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 datetime import datetime as dt from datetime import timezone as tz from datetime import timedelta  from google import genai from google.genai.types import HttpOptions, UpdateCachedContentConfig  client = genai.Client(http_options=HttpOptions(api_version="v1"))  # Get content cache by name # cache_name = "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111" content_cache = client.caches.get(name=cache_name) print("Expire time", content_cache.expire_time) # Example response #   Expire time 2025-02-20 15:50:18.434482+00:00  # Update expire time using TTL content_cache = client.caches.update(     name=cache_name, config=UpdateCachedContentConfig(ttl="36000s") ) time_diff = content_cache.expire_time - dt.now(tz.utc) print("Expire time(after update):", content_cache.expire_time) print("Expire time(in seconds):", time_diff.seconds) # Example response #   Expire time(after update): 2025-02-14 01:51:42.571696+00:00 #   Expire time(in seconds): 35999  # Update expire time using specific time stamp next_week_utc = dt.now(tz.utc) + timedelta(days=7) content_cache = client.caches.update(     name=cache_name, config=UpdateCachedContentConfig(expireTime=next_week_utc) ) print("Expire time(after update):", content_cache.expire_time) # Example response #   Expire time(after update): 2025-02-20 15:51:42.614968+00:00

Go

瞭解如何安裝或更新 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" 	"time"  	genai "google.golang.org/genai" )  // updateContentCache shows how to update content cache expiration time. func updateContentCache(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) 	}  	// Update expire time using TTL 	resp, err := client.Caches.Update(ctx, cacheName, &genai.UpdateCachedContentConfig{ 		TTL: time.Duration(time.Duration.Seconds(36000)), 	}) 	if err != nil { 		return fmt.Errorf("failed to update content cache exp. time with TTL: %w", err) 	}  	fmt.Fprintf(w, "Cache expires in: %s\n", time.Until(resp.ExpireTime)) 	// Example response: 	// Cache expires in: 10h0m0.005875s  	// Update expire time using specific time stamp 	inSevenDays := time.Now().Add(7 * 24 * time.Hour) 	resp, err = client.Caches.Update(ctx, cacheName, &genai.UpdateCachedContentConfig{ 		ExpireTime: inSevenDays, 	}) 	if err != nil { 		return fmt.Errorf("failed to update content cache expire time: %w", err) 	}  	fmt.Fprintf(w, "Cache expires in: %s\n", time.Until(resp.ExpireTime)) 	// Example response: 	// Cache expires in: 167h59m59.80327s  	return nil } 

REST

您可以使用 REST,透過 Vertex AI API 向發布者模型端點傳送 PATCH 要求,藉此建立及更新內容快取。以下範例說明如何使用 ttl 參數更新到期日。

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

  • PROJECT_ID:您的專案 ID
  • LOCATION:處理建立脈絡快取要求的區域。
  • CACHE_ID:內容快取的 ID。建立脈絡快取時,系統會傳回脈絡快取 ID。您也可以列出 Google Cloud 專案的脈絡快取,找出脈絡快取 ID。詳情請參閱「建立脈絡快取」和「列出脈絡快取」。
  • SECONDSfloat,指定快取過期前持續時間的秒數。
  • NANOSECONDSfloat,指定快取到期前持續時間的奈秒元件。

HTTP 方法和網址:

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

JSON 要求主體:

 {   "seconds":"SECONDS",   "nanos":"NANOSECONDS" } 

如要傳送要求,請選擇以下其中一個選項:

curl

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID"

PowerShell

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

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

Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID" | Select-Object -Expand Content

您應該會收到類似以下的 JSON 回應:

cURL 指令範例

PROJECT_ID="PROJECT_ID" LOCATION="us-central1" CACHE_ID="CACHE_ID"  curl \ -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json"\ "https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/cachedContents/${CACHE_ID}" -d \ '{    "ttl": {"seconds":"3600","nanos":"0"} }' 

使用脈絡快取的 expire_time 參數更新快取

以下是 curl 指令的範例,該指令使用 expire_time 參數將到期時間更新為 2024 年 6 月 30 日上午 9 點。

REST

您可以使用 REST,透過 Vertex AI API 向發布者模型端點傳送 PATCH 要求,藉此建立及更新內容快取。以下範例說明如何使用 expire_time 參數更新到期日。

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

  • PROJECT_ID:。
  • LOCATION:處理建立脈絡快取要求的區域。
  • CACHE_ID:內容快取的 ID。您可以在建立內容快取時,從回應中找到 ID。
  • EXPIRE_TIMETimestamp,指定脈絡快取的到期時間。

HTTP 方法和網址:

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

JSON 要求主體:

 {    "expire_time":"EXPIRE_TIME" } 

如要傳送要求,請選擇以下其中一個選項:

curl

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID"

PowerShell

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

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

Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID" | Select-Object -Expand Content

您應該會收到類似以下的 JSON 回應:

cURL 指令範例

PROJECT_ID="PROJECT_ID" LOCATION="us-central1" CACHE_ID="CACHE_ID"  curl \ -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json"\ "https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/cachedContents/${CACHE_ID}" -d \ '{    "expire_time":"2024-06-30T09:00:00.000000Z" }' 

後續步驟