取得背景資訊快取的相關資訊

您可以瞭解脈絡快取的建立時間、最近更新時間和到期時間。如要取得與 Google Cloud 專案相關聯的所有內容快取資訊 (包括快取 ID),請使用指令列出內容快取。如果您知道內容快取的快取 ID,可以只取得該內容快取的相關資訊。

取得脈絡快取清單

如要取得與 Google Cloud 專案相關聯的內容快取清單,您需要建立專案的區域和專案 ID。 Google Cloud 以下說明如何取得 Google Cloud 專案的內容快取清單。

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
from google.genai.types import HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))

content_cache_list = client.caches.list()

# Access individual properties of a ContentCache object(s)
for content_cache in content_cache_list:
    print(f"Cache `{content_cache.name}` for model `{content_cache.model}`")
    print(f"Last updated at: {content_cache.update_time}")
    print(f"Expires at: {content_cache.expire_time}")

# Example response:
# * Cache `projects/111111111111/locations/us-central1/cachedContents/1111111111111111111` for
#       model `projects/111111111111/locations/us-central1/publishers/google/models/gemini-XXX-pro-XXX`
# * Last updated at: 2025-02-13 14:46:42.620490+00:00
# * CachedContentUsageMetadata(audio_duration_seconds=None, image_count=167, text_count=153, total_token_count=43130, video_duration_seconds=None)
# ...

REST

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

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

HTTP 方法和網址:

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

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

curl

執行下列指令:

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

PowerShell

執行下列指令:

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

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

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

cURL 指令範例

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

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

取得內容快取的相關資訊

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

以下說明如何取得單一內容快取的相關資訊。

Go

在試用這個範例之前,請先按照 Vertex AI 快速入門導覽課程的操作說明設定 Go 環境。詳情請參閱 Vertex AI Go 版 Gemini SDK 參考說明文件

如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「 為本機開發環境設定 ADC」。

串流和非串流回應

您可以選擇模型生成串流非串流回覆。如果是串流回應,系統會在生成輸出權杖後立即傳送每個回應。如果不是逐句顯示回覆,系統會在生成所有輸出權杖後,傳送所有回覆。

如要取得串流回應,請使用 GenerateContentStream 方法。

  iter := model.GenerateContentStream(ctx, genai.Text("Tell me a story about a lumberjack and his giant ox. Keep it very short."))
  

如為非串流回應,請使用 GenerateContent 方法。

  resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
  

程式碼範例

import (
	"context"
	"fmt"
	"io"

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

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

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

REST

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

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

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

HTTP 方法和網址:

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

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

curl

執行下列指令:

curl -X GET \
-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 GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID" | Select-Object -Expand Content

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

cURL 指令範例

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

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