SemanticCacheLookup 政策

本頁適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

總覽

SemanticCacheLookup 政策是進階快取政策,旨在最佳化 AI 工作負載的效能,尤其是涉及大型語言模型 (LLM) 的工作負載。

這項政策會使用 Vertex AI 文字嵌入 API 為文字產生嵌入,並使用 Vector Search 根據語意相似性 (而非完全相符) 找出相似提示。

SemanticCacheLookup 政策可減少對 LLM 的呼叫量,進而縮短重複查詢的回應時間,並降低成本。

這項政策會與 SemanticCachePopulate 政策搭配使用。

這項政策是可擴充的政策,視您的 Apigee 授權而定,使用這項政策可能會產生費用或使用量影響。如要瞭解政策類型和使用相關性,請參閱「政策類型」。

事前準備

使用 SemanticCacheLookup 政策前,請先完成下列工作:

  • 建立 Vertex AI 專案。
  • 建立 Vector Search 索引。
  • 為索引建立 Vertex AI 端點。
  • 建立 SemanticCachePopulate 政策。

如要進一步瞭解如何完成這些工作,請參閱「開始使用語意快取政策」。

必要的角色

如要取得適用 SemanticCacheLookup 政策並使用該政策所需的權限,請管理員在您用於部署 Apigee 代理的服務帳戶上,授予您「AI 平台使用者」 (roles/aiplatform.user) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

您或許還可透過自訂角色或其他預先定義的角色取得必要權限。

啟用 API

Enable the Compute Engine, Vertex AI, and Cloud Storage APIs.

Enable the APIs

<SemanticCacheLookup> 元素

定義 SemanticCacheLookup 政策。

預設值 請參閱下方的「Default Policy」分頁
是否必要? 必填
類型 複雜物件
上層元素 N/A
子元素 <DisplayName>
<IgnoreUnresolvedVariables>
<UserPromptSource>
<Embeddings>
<SimilaritySearch>

<SemanticCacheLookup> 元素使用以下語法:

語法

<SemanticCacheLookup> 元素使用以下語法:

<SemanticCacheLookup async="false" continueOnError="false" enabled="true" name="SCL-lookup">
  <DisplayName>SCL-lookup</DisplayName>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <UserPromptSource>{jsonPath($.contents[-1].parts[-1].text,request.content,true)}</UserPromptSource>
  <Embeddings>
    <VertexAI>
      <URL>https://{LOCATION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/google/models/{MODEL_ID}:predict</URL>
    </VertexAI>
  </Embeddings>
  <SimilaritySearch>
    <VertexAI>
      <URL>https://{PUBLIC_DOMAIN_NAME}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/indexEndpoints/{INDEX_ENDPOINT_ID}:findNeighbors</URL>
      <DeployedIndexID>{DEPLOYED_INDEX_ID}</DeployedIndexID>
      <Threshold>0.95</Threshold>
    </VertexAI>
  </SimilaritySearch>
</SemanticCacheLookup>

預設政策

以下範例顯示在 Apigee UI 中將 SemanticCacheLookup 政策新增至流程時的預設設定:

<SemanticCacheLookup async="false" continueOnError="false"enabled="true" name="SCL-lookup">
  <DisplayName>SCL-lookup</DisplayName>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <UserPromptSource>{jsonPath($.contents[-1].parts[-1].text,request.content,true)}</UserPromptSource>
  <Embeddings>
    <VertexAI>
      <URL>https://{LOCATION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/google/models/{MODEL_ID}:predict
      </URL>
    </VertexAI>
  </Embeddings>
  <SimilaritySearch>
    <VertexAI>
      <URL>https://{PUBLIC_DOMAIN_NAME}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/indexEndpoints/{INDEX_ENDPOINT_ID}:findNeighbors</URL>
      <Threshold>0.9</Threshold>
      <DeployedIndexID>{DEPLOYED_INDEX_ID}</DeployedIndexID>
    </VertexAI>
  </SimilaritySearch>
</SemanticCacheLookup>

在 Apigee UI 中插入新的 SemanticCacheLookup 政策時,範本會包含所有可能作業的存根。請參閱下方資訊,瞭解必要元素。

這個元素包含下列所有政策都適用的屬性:

屬性 預設 是否必要? 說明
name 不適用 必要

政策的內部名稱。name 屬性的值可以包含英文字母、數字、空格、連字號、底線和句號。這個值不得超過 255 個半形字元。

您可以選擇使用 <DisplayName> 元素,在管理 UI 代理程式編輯器中為政策加上不同、自然語言的名稱。

continueOnError false 選用 將其設為 false,即可在政策失敗時傳回錯誤。這是大多數政策的預期行為。將其設為 true,即使政策失敗,流程執行作業仍會繼續進行。另請參閱:
enabled 選用 設為 true 即可強制執行政策。設為 false 即可關閉政策。即使政策仍附加至流程,系統也不會強制執行這項政策。
async   false 已淘汰 此屬性已淘汰。

下表概略說明 <SemanticCacheLookup> 的子元素:

子元素 是否必要 說明
<DisplayName> 選用 政策名稱。

<IgnoreUnresolvedVariables> 選用 判斷在變數未解析時是否停止處理。將其設為 true 可忽略未解析的變數,並繼續處理。
<UserPromptSource> 選用 要擷取使用者提示文字的酬載位置。系統僅支援字串文字值。

這個欄位支援 Apigee 訊息範本語法,包括使用 變數JSON 路徑函式。

例如:

{jsonPath($.contents[-1].parts[-1].text,request.content,true)}

<Embeddings> 必填 包含產生嵌入資料所需資訊的元素。
<SimilaritySearch> 必填 包含執行相似搜尋所需資訊的元素。

詳情請參閱「查詢公開索引以取得最近鄰點」。

子元素參照

本節將說明 <SemanticCacheLookup> 的子元素。

<DisplayName>

除了 name 屬性之外,您也可以在管理 UI 代理程式編輯器中使用其他更自然的名稱標記政策。

<DisplayName> 元素適用於所有政策。

預設值 不適用
是否必要? (非必要) 如果省略 <DisplayName>,系統會使用政策的 name 屬性值。
類型 字串
上層元素 <PolicyElement>
子元素

<DisplayName> 元素使用以下語法:

語法

<PolicyElement>
  <DisplayName>POLICY_DISPLAY_NAME</DisplayName>
  ...
</PolicyElement>

範例

<PolicyElement>
  <DisplayName>My Validation Policy</DisplayName>
</PolicyElement>

<DisplayName> 元素沒有屬性或子項元素。

<IgnoreUnresolvedVariables>

判斷在變數未解析時是否停止處理。將其設為 true 可忽略未解析的變數,並繼續處理。

提供 <DefaultValue> 時,IgnoreUnresolvedVariables 不適用。

預設值
是否必要? 選用
類型 布林值
上層元素 <SemanticCacheLookup>
子元素

<UserPromptSource>

要擷取使用者提示文字的酬載位置。系統僅支援字串文字值。

這個欄位支援 Apigee 訊息範本語法,包括使用變數JSON 路徑函式

例如:

{jsonPath($.contents[-1].parts[-1].text,request.content,true)}
預設值 {jsonPath($.contents[-1].parts[-1].text,request.content,true)}
是否必要? 選用
類型 字串
上層元素 <SemanticCacheLookup>
子元素

<Embeddings>

這個元素包含產生文字嵌入資料所需的資訊。

預設值 不適用
是否必要? 選用
類型 字串
上層元素 <SemanticCacheLookup>
子元素 <VertexAI>

<Embeddings> 元素使用以下語法:

<Embeddings>
  <VertexAI>
    <URL>https://{LOCATION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/google/models/{MODEL_ID}:predict</URL>
  </VertexAI>
</Embeddings>

<VertexAI> (<Embeddings> 的子項)

包含 Vertex AI 專屬屬性的 <URL> 元素。

預設值 不適用
是否必要? 必填
類型 字串
上層元素 <Embeddings>
子元素 <URL>

VertexAI 元素使用以下語法:

<VertexAI>
  <URL>https://{LOCATION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/google/models/{MODEL_ID}:predict</URL>
</VertexAI>

<網址> (<VertexAI> 的子項)

用於產生文字嵌入的網址。如需可為 SemanticCacheLookup 政策提供文字嵌入資料的模型清單,請參閱「 支援的模型」。

預設值 不適用
是否必要? 必填
類型 字串
上層元素 <VertexAI>
子元素

URL 元素使用以下語法:

<URL>https://{LOCATION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/google/models/{MODEL_ID}:predict</URL>

<SimilaritySearch>

這個元素包含執行相似搜尋所需的資訊。

詳情請參閱「查詢公開索引以取得最近鄰點」。

預設值 不適用
是否必要? 必填
類型 字串
上層元素 <SemanticCacheLookup>
子元素 <VertexAI>

<SimilaritySearch> 元素使用以下語法:

<SimilaritySearch>
  <VertexAI>
    <URL>https://{PUBLIC_DOMAIN_NAME}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/indexEndpoints/{INDEX_ENDPOINT_ID}:findNeighbors
    </URL>
    <Threshold>0.9</Threshold>
    <DeployedIndexID>{DEPLOYED_INDEX_ID}</DeployedIndexID>
  </VertexAI>
</SimilaritySearch>

<VertexAI> (<SimilaritySearch> 的子項)

包含 Vertex AI 專屬屬性的 <URL> 元素。

預設值 不適用
是否必要? 必填
類型 字串
上層元素 <SimilaritySearch>
子元素 <URL>

VertexAI 元素使用以下語法:

<VertexAI>
  <URL>https://{PUBLIC_DOMAIN_NAME}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/indexEndpoints/{INDEX_ENDPOINT_ID}:findNeighbors</URL>
  <Threshold>0.9</Threshold>
  <DeployedIndexID>{DEPLOYED_INDEX_ID}</DeployedIndexID>
</VertexAI>

下表概略說明 <VertexAI> 的子元素。

子元素 是否必要 說明
<URL> 必填 字串

用於執行相似度搜尋的網址。系統會根據相似度門檻,使用相符度最高的資料點。

<Threshold> 選用 字串

相似度分數,用於判斷兩個提示是否相符。 介於 0 到 1 的值。

預設值為 0.9。

<DeployedIndexID> 必填 字串

在索引端點上部署的索引 ID,用於語意快取。

流程變數

您可以使用流程變數,根據 HTTP 標頭或訊息內容,或是流程中可用的內容,設定政策和流程的動態執行階段行為。如要進一步瞭解流程變數,請參閱「流程變數參考資料」。

政策可以在執行期間設定這些唯讀變數。

變數名稱 說明
request.content 包含傳入的 API 要求完整內容。
request.url 包含傳入的 API 要求網址。
semanticcache.lookup.policy_name.user_prompt 包含從要求提示中擷取的特定元件,用於產生嵌入或執行相似度搜尋。
semanticcache.lookup.policy_name.embeddings_request 包含傳送至 Vertex AI Embeddings API 的要求酬載,用於為輸入文字產生文字嵌入。
semanticcache.lookup.policy_name.embeddings_response 包含 Vertex AI Embeddings API 的回應,其中包含產生的文字嵌入內容。
semanticcache.lookup.policy_name.dense_embeddings 包含 Vertex AI Embeddings API 產生的實際數值嵌入值。
semanticcache.lookup.policy_name.is_nearest_neighbor_hit 指定向量資料庫是否在指定要求中找到最近鄰,且資料點符合相似度門檻。
semanticcache.lookup.policy_name.cache_hit 指定是否在語意快取中找到回應。
semanticcache.lookup.policy_name.cached_llm_response 包含從語意快取擷取的回應 (如果發生快取命中)。

錯誤參考資料

本節將說明傳回的錯誤代碼和錯誤訊息,以及 Apigee 針對 <SemanticCacheLookup> 政策設定的錯誤變數。如果您要開發錯誤處理規則,就必須瞭解這項資訊。如需更多資訊,請參閱「政策錯誤的相關資訊」和「處理錯誤」。

執行階段錯誤

政策執行時可能會發生這些錯誤。

錯誤代碼 HTTP 狀態 原因
steps.semanticcache.lookup.MessageTemplateExtractionFailed 400 無法使用 JSON Path 運算式從要求中擷取資料。
steps.semanticcache.lookup.FailedToExtractUserPrompt 500 無法從 API 要求中擷取使用者提示。
steps.semanticcache.lookup.EmbeddingsServiceUnavailable 400 Vertex AI 嵌入服務目前無法使用。
steps.semanticcache.lookup.EmbeddingsAPIFailed 400 Vertex AI Embeddings 服務失敗。
steps.semanticcache.lookup.VectorSearchServiceUnavailable 400 Vertex AI Vector Search 服務目前無法使用。
steps.semanticcache.lookup.VectorSearchAPIFailed 400 Vertex AI Vector Search 服務失敗。
steps.semanticcache.lookup.AuthenticationFailure 500 服務帳戶沒有必要權限。
steps.semanticcache.lookup.InternalError 500 SemanticCacheLookup 政策中發生未預期的錯誤。
steps.semanticcache.lookup.CalloutError 500 Vertex AI 服務呼叫失敗。

部署錯誤

部署含有這項政策的 Proxy 時,可能會發生這些錯誤。

錯誤名稱 原因
The Embeddings/VertexAI element is required. 如果 <Embeddings> 中的 <VertexAI> 元素為空,就會發生這個錯誤。
The SimilaritySearch/VertexAI element is required. 如果 <SimilaritySearch> 中的 <VertexAI> 元素為空白,就會發生這個錯誤。
The Embeddings/URL element is required. 如果 <Embeddings> 中的 <URL> 元素為空,就會發生這個錯誤。
The SimilaritySearch/URL element is required. 如果 <SimilaritySearch> 中的 <URL> 元素為空白,就會發生這個錯誤。
Embeddings URL {url} is invalid. 如果 <Embeddings> 中的 <URL> 元素為空白或無效,就會發生此錯誤。
The SimilaritySearch URL {url} is invalid. 如果 <SimilaritySearch> 中的 <URL> 元素為空白或無效,就會發生此錯誤。
The scheme {http-scheme} of Embeddings URL {url} must be one of http, https. 如果嵌入 <URL> 元素的 http 配置無效,就會發生此錯誤。
The scheme {http-scheme} of SimilaritySearch URL {url} must be one of http, https. 如果 SimilaritySearch <URL> 元素的 http 配置無效,就會發生此錯誤。
SimilaritySearch/Threshold element must be >= 0 and <= 1. 如果屬性不在 0 和 1 之間,API 代理程式就會部署失敗。
SimilaritySearch/DeployedIndexID element is required. 如果 <SimilaritySearch> 中的 <DeployedIndexID> 元素為空白,就會發生此錯誤。
SimilaritySearch/DeployedIndexID element must not contain spaces. 如果 <SimilaritySearch> 中的 <DeployedIndexID> 元素含有空格,就會發生這個錯誤。

錯誤變數

當這項政策在執行階段觸發錯誤時,系統就會設定這些變數。詳情請參閱「關於政策錯誤的相關資訊」。

變數 地點 範例
fault.name="FAULT_NAME" FAULT_NAME 是錯誤名稱,如上方「執行階段錯誤」表格所列。錯誤名稱是錯誤代碼的最後一個部分。 fault.name Matches "UnresolvedVariable"
semanticcachelookup.POLICY_NAME.failed POLICY_NAME 是使用者指定的政策名稱,該政策會擲回錯誤。 semanticcachelookup.SC-lookup.failed = true

錯誤回應範例

{
  "fault": {
    "faultstring": "SemanticCacheLookup[SC-lookup]: unable to resolve variable [variable_name]",
    "detail": {
      "errorcode": "steps.semanticcachelookup.UnresolvedVariable"
    }
  }
}

錯誤規則範例

<FaultRule name="SemanticCacheLookup Faults">
    <Step>
        <Name>SCL-CustomSetVariableErrorResponse</Name>
        <Condition>(fault.name = "SetVariableFailed")</Condition>
    </Step>
    <Condition>(semanticcachelookup.failed = true)</Condition>
</FaultRule>

結構定義

每個政策類型都由 XML 架構 (.xsd) 定義。如需參考,請前往 GitHub 查看政策架構