如果搜尋查詢量很大,請務必更新索引,確保資訊準確無誤。您可以透過幾種方式更新 Vector Search 索引:
取代整個索引
如要取代現有批次更新或串流 Index 的內容,請使用 IndexService.UpdateIndex 方法。
- 將
Index.metadata.contentsDeltaUri設為包含要更新向量的 Cloud Storage URI。 - 將
Index.metadata.isCompleteOverwrite設為 true。如果設為 true,整個索引會完全覆寫為您提供的新中繼資料檔案。
gcloud
- 更新索引中繼資料檔案,設定
contentsDeltaUri和isCompleteOverwrite=true。 - 使用
gcloud ai indexes update指令。
使用下列任何指令資料前,請先替換以下項目:
- LOCAL_PATH_TO_METADATA_FILE:中繼資料檔案的本機路徑。
- INDEX_ID:索引 ID。
- LOCATION:您使用 Agent Platform 的區域。
- PROJECT_ID:您的 Cloud de Confiance by S3NS 專案 ID。
執行下列指令:
Linux、macOS 或 Cloud Shell
gcloud ai indexes update INDEX_ID \ --metadata-file=LOCAL_PATH_TO_METADATA_FILE \ --region=LOCATION \ --project=PROJECT_ID
Windows (PowerShell)
gcloud ai indexes update INDEX_ID ` --metadata-file=LOCAL_PATH_TO_METADATA_FILE ` --region=LOCATION ` --project=PROJECT_ID
Windows (cmd.exe)
gcloud ai indexes update INDEX_ID ^ --metadata-file=LOCAL_PATH_TO_METADATA_FILE ^ --region=LOCATION ^ --project=PROJECT_ID
REST
使用任何要求資料之前,請先修改下列項目的值:
- INPUT_DIR:索引內容的 Cloud Storage 目錄路徑。
- INDEX_ID:索引 ID。
- LOCATION:您使用 Agent Platform 的區域。
- PROJECT_ID:您的 Cloud de Confiance by S3NS 專案 ID。
- PROJECT_NUMBER:系統自動產生的專案編號。
HTTP 方法和網址:
PATCH https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/indexes/INDEX_ID
JSON 要求主體:
{
"metadata": {
"contentsDeltaUri": "INPUT_DIR",
"isCompleteOverwrite": true
}
}
請展開以下其中一個選項,以傳送要求:
您應該會收到如下的 JSON 回覆:
{
"name": "projects/PROJECT_NUMBER/locations/LOCATION/indexes/INDEX_ID/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.aiplatform.v1.UpdateIndexOperationMetadata",
"genericMetadata": {
"createTime": "2022-01-12T23:56:14.480948Z",
"updateTime": "2022-01-12T23:56:14.480948Z"
}
}
}
Python
如要瞭解如何安裝或更新 Vertex AI SDK for Python,請參閱「安裝 Vertex AI SDK for Python」。詳情請參閱 Python API 參考文件。
叫用下列方法時,請將 is_complete_overwrite=True 設為完全取代索引內容。
控制台
請按照下列操作說明更新批次索引內容。
- 前往 Cloud de Confiance 控制台的「Agent Platform」部分,然後前往「Deploy and Use」(部署及使用) 專區。選取「向量搜尋」
- 選取要更新的索引。「Index info」(索引資訊) 頁面隨即開啟。
- 選取「編輯索引」。「編輯索引」窗格隨即開啟。
- 在「Cloud Storage」欄位中,搜尋並選取儲存向量資料的 Cloud Storage 資料夾。
- 勾選「完全覆寫」方塊,即可覆寫所有現有資料。
- 按一下 [Update] (更新)。
- 按一下「完成」關閉面板。
部分更新批次索引
如要更新現有批次 Index 的嵌入,請使用 IndexService.UpdateIndex 方法。
- 將
Index.metadata.contentsDeltaUri設為包含要更新向量的 Cloud Storage URI。 - 將
Index.metadata.isCompleteOverwrite設為 false。
系統只會更新、插入或刪除 Index.metadata.contentsDeltaUri 中指定的向量。索引中其他現有的嵌入則會保留。
gcloud
- 更新索引中繼資料檔案,設定
contentsDeltaUri。 - 使用
gcloud ai indexes update指令。
使用下列任何指令資料前,請先替換以下項目:
- LOCAL_PATH_TO_METADATA_FILE:中繼資料檔案的本機路徑。
- INDEX_ID:索引 ID。
- LOCATION:您使用 Agent Platform 的區域。
- PROJECT_ID:您的 Cloud de Confiance by S3NS 專案 ID。
執行下列指令:
Linux、macOS 或 Cloud Shell
gcloud ai indexes update INDEX_ID \ --metadata-file=LOCAL_PATH_TO_METADATA_FILE \ --region=LOCATION \ --project=PROJECT_ID
Windows (PowerShell)
gcloud ai indexes update INDEX_ID ` --metadata-file=LOCAL_PATH_TO_METADATA_FILE ` --region=LOCATION ` --project=PROJECT_ID
Windows (cmd.exe)
gcloud ai indexes update INDEX_ID ^ --metadata-file=LOCAL_PATH_TO_METADATA_FILE ^ --region=LOCATION ^ --project=PROJECT_ID
REST
使用任何要求資料之前,請先修改下列項目的值:
- INPUT_DIR:索引內容的 Cloud Storage 目錄路徑。
- INDEX_ID:索引 ID。
- LOCATION:您使用 Agent Platform 的區域。
- PROJECT_ID:您的 Cloud de Confiance by S3NS 專案 ID。
- PROJECT_NUMBER:系統自動產生的專案編號。
HTTP 方法和網址:
PATCH https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/indexes/INDEX_ID
JSON 要求主體:
{
"metadata": {
"contentsDeltaUri": "INPUT_DIR",
"isCompleteOverwrite": false
}
}
請展開以下其中一個選項,以傳送要求:
您應該會收到如下的 JSON 回覆:
{
"name": "projects/PROJECT_NUMBER/locations/LOCATION/indexes/INDEX_ID/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.aiplatform.v1.UpdateIndexOperationMetadata",
"genericMetadata": {
"createTime": "2022-01-12T23:56:14.480948Z",
"updateTime": "2022-01-12T23:56:14.480948Z"
}
}
}
Python
如要瞭解如何安裝或更新 Vertex AI SDK for Python,請參閱「安裝 Vertex AI SDK for Python」。詳情請參閱 Python API 參考文件。
叫用下列方法時,請設定 is_complete_overwrite=False。
控制台
請按照下列操作說明更新批次索引內容。
- 前往 Cloud de Confiance 控制台的「Agent Platform」部分,然後前往「Deploy and Use」(部署及使用) 專區。選取「向量搜尋」
- 選取要更新的索引。「Index info」(索引資訊) 頁面隨即開啟。
- 選取「編輯索引」。「編輯索引」窗格隨即開啟。
- 在「Cloud Storage」欄位中,搜尋並選取儲存向量資料的 Cloud Storage 資料夾。
- 確認「Complete overwrite」(完整覆寫) 方塊已清除。
- 按一下 [Update] (更新)。
- 按一下「完成」關閉面板。
如果 Index 有任何相關聯的部署作業 (請參閱 Index.deployed_indexes 欄位),則在對原始 Index 進行特定變更時,DeployedIndex 會在背景自動以非同步方式更新,以反映這些變更。
如要確認變更是否已套用,請比較更新索引作業完成時間和 DeployedIndex.index_sync_time。
部分更新串流索引
透過串流更新,您可以在幾秒內更新及查詢索引。目前,您無法在現有的批次更新索引上使用串流更新,必須建立新的索引。詳情請參閱「建立用於串流更新的索引」。
串流更新每 GB 費用為 $0.45 美元。如要進一步瞭解定價,請參閱 Agent Platform 定價頁面。串流更新會直接套用至記憶體中已部署的索引,並在短暫延遲後反映在查詢結果中。
新增或更新資料點
請參考這些範例,瞭解如何新增或更新資料點。請注意,upsert-datapoints 只接受陣列格式的 JSON。
Python
Python
Curl
處理量配額限制與 upsert 作業包含的資料量有關。如果索引中已有資料點 ID,系統會更新嵌入,否則會新增嵌入。
DATAPOINT_ID_1=
DATAPOINT_ID_2=
curl -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/indexes/${INDEX_ID}:upsertDatapoints \
-d '{datapoints: [{datapoint_id: "'${DATAPOINT_ID_1}'", feature_vector: [...]},
{datapoint_id: "'${DATAPOINT_ID_2}'", feature_vector: [...]}]}'
混合式搜尋支援資料點的稀疏和稠密嵌入表示法。在 upsert 作業中,省略密集嵌入會刪除密集表示法,省略稀疏嵌入則會刪除稀疏表示法。
這個範例會同時更新稠密和稀疏嵌入。
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/indexes/${INDEX_ID}:upsertDatapoints -d '{datapoints: [{datapoint_id: "111", feature_vector: [0.111, 0.111], "sparse_embedding": {"values": [111.0,111.1,111.2], "dimensions": [10,20,30]}}]}'
這個範例會更新稠密嵌入,並移除稀疏嵌入。
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/indexes/${INDEX_ID}:upsertDatapoints -d '{datapoints: [{datapoint_id: "111", feature_vector: [0.111, 0.111]}]}'
這個範例會更新稀疏嵌入,並移除稠密嵌入。
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/indexes/${INDEX_ID}:upsertDatapoints -d '{datapoints: [{datapoint_id: "111", "sparse_embedding": {"values": [111.0,111.1,111.2], "dimensions": [10,20,30]}}]}'
控制台
控制台
請按照下列操作說明,將內容更新至串流索引。
- 在 Cloud de Confiance 控制台,前往「向量搜尋」頁面。
- 選取要更新的索引。「Index info」(索引資訊) 頁面隨即開啟。
- 按一下「Edit Index」(編輯索引)。「編輯索引」窗格隨即開啟。
- 在窗格中,選取「Upsert data point」(更新插入資料點) 分頁標籤,即可新增內容。
- 輸入資料點 ID。
- 請至少輸入一種嵌入類型:
- 密集嵌入:輸入以半形逗號分隔的浮點值陣列。值的數量必須與索引維度相符。
- 稀疏嵌入:
- 請以半形逗號分隔的整數陣列,輸入稀疏型嵌入維度。值的數量不一定要與索引維度相符。
- 請輸入浮點值陣列,並以半形逗號分隔。值的數量必須與稀疏型嵌入維度數量相符。
- 選用:如要依據這個資料點的權杖限制啟用篩選功能,請按一下「Add token restrict」(新增權杖限制),然後輸入命名空間和以半形逗號分隔的字串做為權杖。
- 選用:如要啟用這個資料點的數值限制篩選功能,請按一下「新增數值限制」,輸入命名空間,選取數字類型,然後輸入值。
- 選用:如要避免出現許多類似結果,請輸入聚集標記字串。
- 按一下「Upsert」。
- 按一下「完成」關閉面板。
處理量配額限制與 upsert 作業包含的資料量有關。如果索引中已有該資料點 ID,系統會更新嵌入,否則會新增嵌入。
更新嵌入中繼資料
您可能基於許多原因,需要更新串流限制或數字限制。舉例來說,處理大量且快速變動的資料時,您可能需要優先處理特定資料串流。直接更新限制或數值限制,即可即時調整焦點,確保最重要的資料立即處理或醒目顯示。
您可以直接更新串流索引中的資料點限制和數值限制,不必支付完整更新的壓縮費用。
如要執行這些僅限中繼資料的更新,您必須在要求中新增 update_mask 欄位。update_mask 的值必須設為 all_restricts。
在資料點中設定的限制和數值限制值,應為您要在更新中套用的新值。
以下範例說明如何為兩個現有資料點新增限制。
DATAPOINT_ID_1=
DATAPOINT_ID_2=
curl -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/indexes/${INDEX_ID}:upsertDatapoints \
-d '{datapoints:
[{datapoint_id: "'${DATAPOINT_ID_1}'", feature_vector: [...], restricts:[{namespace: "color", allow_list: ["red"]}]},
{datapoint_id: "'${DATAPOINT_ID_2}'", feature_vector: [...], restricts:[{namespace: "color", allow_list: ["red"]}]}
], update_mask: "all_restricts"}'
移除資料點
您可能需要從串流索引中移除資料點。您可以使用 curl 或透過 Cloud de Confiance 控制台執行這項操作。
從索引中刪除資料點的主要用途,是維持索引與實際來源之間的一致性。假設某位書商使用向量嵌入來表示書籍庫存,以便進行搜尋和推薦。當書籍售罄或下架時,從索引中刪除對應的資料點,可確保搜尋結果和推薦內容保持準確和最新狀態。
Curl
curl -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" https://{LOCATION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/{REGION}/indexes/{INDEX_ID}:removeDatapoints -d '{datapoint_ids: ["'{DATAPOINT_ID_1}'", "'{DATAPOINT_ID_2}'"]}'
控制台
控制台
請按照下列操作說明,從串流索引中刪除資料點。
- 前往 Cloud de Confiance 控制台的「Agent Platform」部分,然後前往「Deploy and Use」(部署及使用) 專區。選取「向量搜尋」
- 選取要更新的串流索引。「Index info」(索引資訊) 頁面隨即開啟。
- 選取「編輯索引」。「編輯索引」窗格隨即開啟。
- 在窗格中選取「移除資料點」分頁標籤。
- 提供以半形逗號分隔的資料點 ID 清單,最多可新增 20 個資料點
- 按一下 [移除]。
- 按一下「完成」關閉面板。
Python
Python
更新索引中繼資料
IndexService.UpdateIndex 也可以用來更新批次和串流索引的中繼資料欄位 display_name、description 和 labels。請注意,單次呼叫 UpdateIndex 可以更新索引嵌入「或」這些中繼資料欄位,但無法同時更新兩者。
gcloud
- 更新索引中繼資料檔案。
- 使用
gcloud ai indexes update指令。
使用下列任何指令資料前,請先替換以下項目:
- LOCAL_PATH_TO_METADATA_FILE:中繼資料檔案的本機路徑。
- INDEX_ID:索引 ID。
- LOCATION:您使用 Agent Platform 的區域。
- PROJECT_ID:您的 Cloud de Confiance by S3NS 專案 ID。
執行下列指令:
Linux、macOS 或 Cloud Shell
gcloud ai indexes update INDEX_ID \ --metadata-file=LOCAL_PATH_TO_METADATA_FILE \ --region=LOCATION \ --project=PROJECT_ID
Windows (PowerShell)
gcloud ai indexes update INDEX_ID ` --metadata-file=LOCAL_PATH_TO_METADATA_FILE ` --region=LOCATION ` --project=PROJECT_ID
Windows (cmd.exe)
gcloud ai indexes update INDEX_ID ^ --metadata-file=LOCAL_PATH_TO_METADATA_FILE ^ --region=LOCATION ^ --project=PROJECT_ID
REST
使用任何要求資料之前,請先修改下列項目的值:
- INPUT_DIR:索引內容的 Cloud Storage 目錄路徑。
- INDEX_ID:索引 ID。
- LOCATION:您使用 Agent Platform 的區域。
- PROJECT_ID:您的 Cloud de Confiance by S3NS 專案 ID。
- PROJECT_NUMBER:系統自動產生的專案編號。
HTTP 方法和網址:
PATCH https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/indexes/INDEX_ID
JSON 要求主體:
{
"metadata": {
"description": "Updated description",
"display_name": "Updated display name"
}
}
請展開以下其中一個選項,以傳送要求:
您應該會收到如下的 JSON 回覆:
{
"name": "projects/PROJECT_NUMBER/locations/LOCATION/indexes/INDEX_ID/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.aiplatform.v1.UpdateIndexOperationMetadata",
"genericMetadata": {
"createTime": "2022-01-12T23:56:14.480948Z",
"updateTime": "2022-01-12T23:56:14.480948Z"
}
}
}
Python
控制台
請按照下列操作說明更新索引中繼資料 (控制台只能更新 display_name 和 description)。
- 前往 Cloud de Confiance 控制台的「Agent Platform」部分,然後前往「Deploy and Use」(部署及使用) 專區。選取「向量搜尋」
- 選取要更新的索引。「Index info」(索引資訊) 頁面隨即開啟。
- 選取「編輯索引」。「編輯索引」窗格隨即開啟。
- 更新所需的中繼資料欄位。
- 按一下 [Update] (更新)。
- 按一下「完成」關閉面板。
壓縮
系統會定期重建索引,納入上次重建後的所有新更新。這項重建作業 (或稱「壓縮」) 可提升查詢效能和穩定性。串流更新和批次更新都會進行壓縮。
串流更新:向量搜尋會使用以經驗法則為基礎的指標,判斷何時觸發壓縮。如果最舊的未壓縮資料已存在五天,系統一律會觸發壓縮作業。除了串流更新費用外,系統也會按照批次更新的費率,向您收取重建索引的費用。
批次更新:當增量資料集大小 > 基本資料集大小的 20% 時,就會發生這種情況。
重建及查詢索引
您可以照常使用 grpc CLI、用戶端程式庫或 Python 適用的 Agent Platform SDK,傳送比對成功的請求或批次比對要求。重新建立查詢後,您應該會在幾秒內看到更新。如要瞭解如何查詢索引,請參閱「查詢索引以取得最鄰近項目」。
選填欄位
建立索引時,您可以使用一些選用欄位來微調查詢。
Upsert with restricts
更新插入索引並新增限制,即可為資料點加上標記,讓資料點在查詢時已識別完成,可供篩選。建議您在傳送查詢前,新增限制標記來限制資料顯示的結果。舉例來說,客戶想對索引執行查詢,但想確保搜尋鞋類時,結果只顯示與「紅色」相符的項目。在以下範例中,系統會更新插入索引,並篩選出所有紅色鞋子,但拒絕藍色鞋子。這可確保搜尋作業在執行前,先從龐大且多樣的索引中篩選出最合適的特定選項。
除了符記限制外,這個範例也使用了數值限制。在本例中,資料點與價格 20、長度 0.3 和寬度 0.5 相關聯。查詢時,您可以使用這些數字限制篩選結果,將查詢結果限制在價格、長度和寬度值。舉例來說,如果查詢篩選條件為價格 > 25、長度 < 1 且寬度 < 1,這個資料點就會顯示在查詢結果中。
如要進一步瞭解篩選功能,請參閱「向量搜尋索引」。
Python
# Upsert datapoints
_TEST_DATAPOINT_1 = aiplatform_v1.types.index.IndexDatapoint(
datapoint_id="3",
feature_vector=[0.00526886899, -0.0198396724],
restricts=[
aiplatform_v1.types.index.IndexDatapoint.Restriction(namespace="Color", allow_list=["red"])
],
numeric_restricts=[
aiplatform_v1.types.index.IndexDatapoint.NumericRestriction(
namespace="cost",
value_int=1,
)
],
)
_TEST_DATAPOINT_2 = aiplatform_v1.types.index.IndexDatapoint(
datapoint_id="4",
feature_vector=[0.00526886899, -0.0198396724],
numeric_restricts=[
aiplatform_v1.types.index.IndexDatapoint.NumericRestriction(
namespace="cost",
value_double=0.1,
)
],
crowding_tag=aiplatform_v1.types.index.IndexDatapoint.CrowdingTag(crowding_attribute="crowding"),
)
_TEST_DATAPOINT_3 = aiplatform_v1.types.index.IndexDatapoint(
datapoint_id="5",
feature_vector=[0.00526886899, -0.0198396724],
numeric_restricts=[
aiplatform_v1.types.index.IndexDatapoint.NumericRestriction(
namespace="cost",
value_float=1.1,
)
],
)
_TEST_DATAPOINTS = [_TEST_DATAPOINT_1, _TEST_DATAPOINT_2, _TEST_DATAPOINT_3]
my_streaming_index = my_streaming_index.upsert_datapoints(datapoints=_TEST_DATAPOINTS)
# Dynamic metadata update
_TEST_DATAPOINT_4 = aiplatform_v1.types.index.IndexDatapoint(
datapoint_id="-2",
numeric_restricts=[
aiplatform_v1.types.index.IndexDatapoint.NumericRestriction(
namespace="cost",
value_float=1.1,
)
],
)
my_streaming_index = my_streaming_index.upsert_datapoints(datapoints=[_TEST_DATAPOINT4], update_mask=["all_restricts"])
curl
curl -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" https://${ENDPOINT}/v1/projects/${PROJECT_ID}/locations/us-central1/indexes/${INDEX_ID}:upsertDatapoints \
-d '{
datapoints: [
{
datapoint_id: "'${DATAPOINT_ID_1}'",
feature_vector: [...],
restricts: { namespace: "color", allow_list: ["red"], deny_list: ["blue"]},
numeric_restricts: [{namespace: "price", value_int: 20}, {namespace: "length", value_float: 0.3}, {namespace: "width", value_double: 0.5}]
}
]}'
使用群聚功能執行 upsert
聚集標記會提升結果多樣性,限制相似結果。Crowding 是對最近鄰搜尋產生的鄰點清單施加的限制,要求一組結果中,傳回相同 crowding_attribute 值的結果數量不得超過某個值。舉例來說,假設您又回到線上購物網站買鞋。您希望結果中呈現多種顏色,但可能希望採用單一風格,例如足球鞋。假設您在插入資料點時,將 crowding_attribute 設為鞋子的顏色,則可以在查詢中設定 per_crowding_attribute_num_neighbors = 3,要求傳回不超過 3 雙相同顏色的鞋子。
這個欄位代表具有相同聚集標記的比對結果數量上限。
curl -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" https://${ENDPOINT}/v1/projects/${PROJECT_ID}/locations/us-central1/indexes/${INDEX_ID}:upsertDatapoints \
-d '{
datapoints: [
{
datapoint_id: "'${DATAPOINT_ID_1}'",
feature_vector: [...],
restricts: { namespace: "type", allow_list: ["cleats"]}
crowding_tag: { crowding_attribute: "red-shoe"},
}
]}'