使用前置字元快取功能提升推論速度

前置字串快取功能會儲存及重複使用處理共用和週期性提示前置字串部分的 LLM 中繼狀態,藉此縮短推論時間。如要啟用前置字元快取,只要在 API 要求中,將靜態前置字元與動態後置字元分開即可。

前置字元快取目前僅支援純文字輸入,因此如果提示中包含圖片,請勿使用這項功能。

前置字元快取有兩種實作方式:隱式或顯式:

隱含使用前置字串快取

如要啟用前置詞快取,請將提示的共用部分新增至 promptPrefix 欄位,如以下程式碼片段所示:

Kotlin

val promptPrefix = "Reverse the given sentence: "
val dynamicSuffix = "Hello World"

val result = generativeModel.generateContent(
  generateContentRequest(TextPart(dynamicSuffix)) {
    promptPrefix = PromptPrefix(promptPrefix)
  }
)

Java

String promptPrefix = "Reverse the given sentence: ";
String dynamicSuffix = "Hello World";

GenerateContentResponse response = generativeModelFutures.generateContent(
    new GenerateContentRequest.Builder(new TextPart(dynamicSuffix))
    .setPromptPrefix(new PromptPrefix(promptPrefix))
    .build())
    .get();

在上述程式碼片段中,dynamicSuffix 會做為主要內容傳遞,而 promptPrefix 則會單獨提供。

預估成效提升

不使用前置字串快取

前置字串快取命中

(首次使用前置字元時,可能會發生前置字元快取失敗)

Pixel 9 搭配 300 個詞元的固定前置字串和 50 個詞元的動態後置字串提示

0.82 秒

0.45 秒

Pixel 9,固定前置字串為 1,000 個詞元,動態後置字串提示為 100 個詞元

2.11 秒

0.5 秒

儲存空間注意事項

使用隱含前置字元快取時,快取檔案會儲存在用戶端應用程式的私人儲存空間,這會增加應用程式的儲存空間用量。系統會儲存加密快取檔案和相關聯的中繼資料,包括原始前置字串文字。請留意下列儲存空間注意事項:

  • 快取數量由 LRU (最近最少使用) 機制管理。超過快取總量上限時,系統會自動刪除最少使用的快取。
  • 提示快取大小取決於前置字串長度。
  • 如要清除前置字元快取建立的所有快取,請使用 generativeMode.clearImplicitCaches() 方法。

使用明確快取管理

直接存取模型時,可使用明確的快取管理方法,讓開發人員更精確地控管快取的建立、搜尋、使用和移除方式。這些手動作業與系統的自動快取處理作業各自獨立運作。

以下範例說明如何初始化明確快取管理,並執行推論:

Kotlin

val cacheName = "my_cache"
val promptPrefix = "Reverse the given sentence: "
val dynamicSuffix = "Hello World"

// Create a cache
val cacheRequest = createCachedContextRequest(cacheName, PromptPrefix(promptPrefix))
val cache = generativeModel.caches.create(cacheRequest)

// Run inference with the cache
val response = generativeModel.generateContent(
  generateContentRequest(TextPart(dynamicSuffix)) {
    cachedContextName = cache.name
  }
)

Java

String cacheName = "my_cache";
String promptPrefix = "Reverse the given sentence: ";
String dynamicSuffix = "Hello World";

// Create a cache
CachedContext cache = cachesFutures.create(
  new CreateCachedContextRequest.Builder(cacheName, new PromptPrefix(promptPrefix))
  .build())
  .get();

// Run inference with the cache
GenerateContentResponse response = generativeModelFutures.generateContent(
  new GenerateContentRequest.Builder(new TextPart(dynamicSuffix))
  .setCachedContextName(cache.getName())
  .build())
  .get();

以下範例說明如何使用 generativeModel.caches 查詢、擷取及刪除明確管理的快取:

Kotlin

val cacheName = "my_cache"

// Query pre-created caches
for (cache in generativeModel.caches.list()) {
  // Do something with cache
}

// Get specific cache
val cache = generativeModel.caches.get(cacheName)

// Delete a pre-created cache
generativeModel.caches.delete(cacheName)

Java

String cacheName = "my_cache";

// Query pre-created caches
for (PrefixCache cache : cachesFutures.list().get()) {
  // Do something with cache
}

// Get specific cache
PrefixCache cache = cachesFutures.get(cacheName).get();

// Delete a pre-created cache
cachesFutures.delete(cacheName);