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

前置字串快取功能會儲存並重複使用處理共用和重複提示前置字串部分的中間 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() 方法。

使用明確的快取管理機制

Prompt API 包含明確的快取管理方法,可讓開發人員更精確地控管快取的建立、搜尋、使用和移除方式。這些手動作業的執行方式與系統的自動快取處理程序無關。

這個範例說明如何初始化明確的快取管理機制,以及執行推論:

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);