更新上下文缓存

您可以在上下文缓存到期时进行更新。上下文缓存的默认到期时间为其创建时间 60 分钟后。已过期的上下文缓存会在垃圾回收过程中被删除,并且无法使用或更新。如需更新未过期的上下文缓存的到期时间,请更新其以下任一属性:

  • ttl - 缓存在创建后或在 ttl 更新后到失效之前的有效期(以秒和纳秒为单位)。设置 ttl 后,缓存的 expireTime 会更新。

  • expire_time - 一个 Timestamp,用于指定上下文缓存的到期绝对日期和时间。

使用 ttl 参数更新上下文缓存

以下 curl 命令示例会将其到期时间更新为 3,600 秒。

Gen AI SDK for 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=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="v1beta1"))  # 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

Gen AI SDK for Go

了解如何安装或更新 Gen AI SDK for 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=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: "36000s", 	}) 	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。如需了解详情,请参阅创建上下文缓存列出上下文缓存
  • SECONDS:一个 float,用于指定缓存过期前时长的秒数组件。
  • NANOSECONDS:一个 float,用于指定缓存过期前时长的纳秒部分。

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:您的项目 ID
  • LOCATION:处理该上下文缓存创建请求的区域。
  • CACHE_ID:相应上下文缓存的 ID。创建上下文缓存时,您可以在响应中找到此 ID。
  • EXPIRE_TIME:一个 Timestamp,用于指定上下文缓存的到期时间。

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" }' 

后续步骤