建立向量索引
本文內容
在 Azure AI 搜尋服務中,「向量存放區」 具有定義向量和非向量欄位的索引結構描述、建立內嵌空間的演算法向量設定,以及向量欄位定義上用於查詢要求的設定。 建立 或更新索引 API 會建立向量存放區。
請遵循下列步驟來編製向量資料的索引:
本文說明工作流程,並使用 REST 來說明每個步驟。 每個最新版本的 REST API 都會新增新功能。 瞭解基本工作流程和每個 API 版本提供的內容後,請繼續進行 azure-search-vector-samples 存放庫中的 Azure SDK 程式代碼範例,以取得在測試和生產程式代碼中使用這些功能的指引。
提示
使用 Azure 入口網站 來建立向量索引 ,並嘗試整合向量化。
必要條件
Azure AI 搜尋,在任何區域和任何層級。 大部分現有的服務都支援向量搜尋。 對於在 2019 年 1 月之前建立的服務,有一個小子集無法建立向量索引。 在此情況下,必須建立新的服務。
如果您使用 Azure SDK 和 REST API 的正式推出版本,則源檔中的預先存在的向量內嵌。 如需詳細資訊,請參閱產生內嵌 。 替代方式是整合向量化(預覽)。
您應該知道用來建立內嵌的模型有何維度限制,以及計算相似度的方式。 在 Azure OpenAI 中,針對 text-embedding-ada-002 ,數值向量長度為 1536。 相似度是使用 cosine
來計算。 有效值是 2 到 3072 個維度。
您應知悉如何建立索引 。 結構描述必須包含文件索引鍵的欄位、您想要搜尋或篩選的其他欄位,以及編製索引和查詢期間所需行為的其他設定。
準備文件以編製索引
在編制索引之前,請先組合文件承載,其中包含向量和非向量資料的欄位。 文件結構必須符合索引結構描述。
請確定您的文件:
提供可唯一識別每份文件的欄位或中繼資料屬性。 所有搜尋索引都需要文件索引鍵。 若要滿足文件索引鍵需求,來源文件必須具有一個可在索引中唯一識別其的欄位或屬性。 此來源欄位必須對應至搜尋索引中類型為 Edm.String
和 key=true
的索引欄位。
提供來源欄位中的向量資料 (單精度浮點數陣列)。
向量欄位包含內嵌模型所產生的數值資料,每個欄位有一個內嵌。 建議 Azure OpenAI 中的內嵌模型,例如 text-embedding-ada-002 用於文字文件,或影像擷取 REST API 用於影像。 僅支援索引最上層向量字段:目前不支援向量子欄位。
為查詢回應,以及在相同要求中包含全文檢索搜尋或語意排名的混合式查詢案例,提供其他欄位與人類可讀取的英數內容。
對於您想要支援的所有查詢案例,您的搜尋索引應該包含其欄位和內容。 假設您想要搜尋或篩選產品名稱、版本、中繼資料或位址。 在此情況下,相似度搜尋並不特別有用。 關鍵字搜尋、異地搜尋或篩選條件將是更好的選擇。 搜尋索引若包含向量和非向量資料的完整欄位集合,可為查詢建構和回應組合提供最大彈性。
本文的載入向量資料 一節中有包含向量和非向量欄位的文件承載簡短範例。
新增向量搜尋設定
向量設定會指定向量搜尋演算法 ,以及在編製索引期間用來在向量節點之間建立「最接近像素」資訊的參數:
如果在欄位上選擇 HNSW,您可以在查詢時選擇詳盡的 KNN。 但另一個方向無法運作:如果您選擇詳盡,則無法稍後要求 HNSW 搜尋,因為啟用近似搜尋的額外數據結構不存在。
正在尋找預覽版到穩定版本的移轉指引嗎? 如需步驟,請參閱升級 REST API 。
2023-11-01 已正式推出。 它支援具有下列的向量組態:
vectorSearch.algorithms
具有索引和評分的參數。
vectorSearch.profiles
,適用於多個演算法設定組合。
請務必具有將內容向量化 的策略。 穩定版本不提供 內建編碼的技能 或 向量化工具 。
使用建立或更新索引 API 來建立索引。
在索引中新增 vectorSearch
區段,指定用來建立內嵌空間的搜尋演算法。
"vectorSearch": {
"algorithms": [
{
"name": "my-hnsw-config-1",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
},
{
"name": "my-hnsw-config-2",
"kind": "hnsw",
"hnswParameters": {
"m": 8,
"efConstruction": 800,
"efSearch": 800,
"metric": "cosine"
}
},
{
"name": "my-eknn-config",
"kind": "exhaustiveKnn",
"exhaustiveKnnParameters": {
"metric": "cosine"
}
}
],
"profiles": [
{
"name": "my-default-vector-profile",
"algorithm": "my-hnsw-config-2"
}
]
}
重點︰
設定的名稱。 名稱在索引內必須是唯一的。
profiles
會新增抽象層以容納更豐富的定義。 設定檔定義於 vectorSearch
中,然後依每個向量欄位上的名稱加以參考。
"hnsw"
和 "exhaustiveKnn"
是用來在編製索引期間組織向量內容的近似最接近像素 (ANN) 演算法。
"m"
(雙向連結計數) 預設值為 4。 範圍是 4 到 10。 較低的值應該會在結果中傳回較少的雜訊。
"efConstruction"
預設值為 400。 範圍是 100 到 1,000。 這是編製索引期間使用的最接近像素數目。
"efSearch"
預設值為 500。 範圍是 100 到 1,000。 這是搜尋期間使用的最接近像素數目。
如果您使用 Azure OpenAI,則 "metric"
應該是 "cosine",否則請使用與您所使用內嵌模型相關聯的相似度計量。 支援的值為 cosine
dotProduct
euclidean
。
2024-03-01-Preview 新增 純量量化 和儲存儲存選項。
新增 vectorSearch.compressions
純量量化和過度取樣的屬性。
新增窄數據類型: Float16
、 Int16
和 Int8
,以提升向量儲存效率。
包含 2023-10-01-preview 整合向量化 。
包含 2023-11-01 vectorSearch.algorithms
和 vectorSearch.profiles
。
使用建立或更新索引預覽版 REST API 來建立索引。
vectorSearch
在索引中新增區段,指定壓縮設定,以及用來建立內嵌空間的搜尋演算法。 如需詳細資訊,請參閱 設定向量量化和縮減記憶體 。
"vectorSearch": {
"compressions": [
{
"name": "my-scalar-quantization",
"kind": "scalarQuantization",
"rerankWithOriginalVectors": true,
"defaultOversampling": 10.0,
"scalarQuantizationParameters": {
"quantizedDataType": "int8"
}
}
],
"algorithms": [
{
"name": "my-hnsw-config-1",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
},
{
"name": "my-hnsw-config-2",
"kind": "hnsw",
"hnswParameters": {
"m": 8,
"efConstruction": 800,
"efSearch": 800,
"metric": "cosine"
}
},
{
"name": "my-eknn-config",
"kind": "exhaustiveKnn",
"exhaustiveKnnParameters": {
"metric": "cosine"
}
}
],
"profiles": [
{
"name": "my-default-vector-profile",
"algorithm": "my-hnsw-config-2"
}
]
}
重點︰
vectorSearch.compressions.kind
必須是 scalarQuantization
。
rerankWithOriginalVectors
會使用原始未壓縮的向量來重新計算相似度,並重新調整初始搜尋查詢所傳回的最上層結果。 即使 為 false,未壓縮的向量仍存在於搜尋索引 stored
中。 這個屬性為選擇性。 預設為 true。
defaultOversampling
會考慮一組更廣泛的潛在結果,以抵消量化信息減少。 潛在結果的公式包含 k
查詢中的 ,具有過度取樣乘數。 例如,如果查詢指定 5 的 , k
且過度取樣為 20,則查詢會有效要求 100 份檔用於重新執行,並針對該目的使用原始未壓縮的向量。 只會傳回最上層 k
重新建立的結果。 這個屬性為選擇性。 預設值為 4。
quantizedDataType
必須設定為 int8
。 這是目前唯一支援的基本數據類型。 這個屬性為選擇性。 預設值為 int8
。
2023-10-01-Preview 新增 內部向量化,但向量 搜尋組態(vectorSearch
結構)大多與 2023-11-01 版完全相同。
使用建立或更新索引預覽版 REST API 來建立索引。
在索引中新增 vectorSearch
區段,指定用來建立內嵌空間的搜尋演算法。
"vectorSearch": {
"algorithms": [
{
"name": "my-hnsw-config-1",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
},
{
"name": "my-hnsw-config-2",
"kind": "hnsw",
"hnswParameters": {
"m": 8,
"efConstruction": 800,
"efSearch": 800,
"metric": "cosine"
}
},
{
"name": "my-eknn-config",
"kind": "exhaustiveKnn",
"exhaustiveKnnParameters": {
"metric": "cosine"
}
}
],
"profiles": [
{
"name": "my-default-vector-profile",
"algorithm": "my-hnsw-config-2"
}
]
}
重點︰
設定的名稱。 名稱在索引內必須是唯一的。
profiles
是本預覽版中的新功能。 其會新增抽象層以容納更豐富的定義。 設定檔定義於 vectorSearch
中,然後做為每個向量欄位上的屬性。
hnsw
和 "exhaustiveKnn"
是用來在編製索引期間組織向量內容的近似最接近像素 (ANN) 演算法。
m
(雙向連結計數) 預設值為 4。 範圍是 4 到 10。 較低的值應該會在結果中傳回較少的雜訊。
efConstruction
預設值為 400。 範圍是 100 到 1,000。 這是編製索引期間使用的最接近像素數目。
efSearch
預設值為 500。 範圍是 100 到 1,000。 這是搜尋期間使用的最接近像素數目。
如果您使用 Azure OpenAI,則 metric
應該是 "cosine",否則請使用與您所使用內嵌模型相關聯的相似度計量。 支援的值為 cosine
dotProduct
euclidean
。
重要
2023-07-01-Preview 是第一個支援向量的 REST API 版本。 它會使用更新預覽版中已取代的過時結構。 建議您 移轉至較新的 REST API 。
此預覽已新增:
vectorSearch.algorithmConfigurations
,用於指定 HNSW 演算法。
hnsw
最接近的鄰近演算法,用於編製向量內容的索引。
使用建立或更新索引 REST API 來建立索引。
在索引中新增 vectorSearch
區段,指定用來建立內嵌空間的搜尋演算法。
"vectorSearch": {
"algorithmConfigurations": [
{
"name": "vectorConfig",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
}
]
}
重點︰
設定的名稱。 名稱在索引內必須是唯一的。
hnsw
是近似最接近像素 (ANN) 演算法,用來在編製索引期間建立鄰近圖。 此 API 版本僅支援階層式導覽小型世界 (HNSW)。
m
(雙向連結計數) 預設值為 4。 範圍是 4 到 10。 較低的值應該會在結果中傳回較少的雜訊。
efConstruction
預設值為 400。 範圍是 100 到 1,000。 這是編製索引期間使用的最接近像素數目。
efSearch
預設值為 500。 範圍是 100 到 1,000。 這是搜尋期間使用的最接近像素數目。
如果您使用 Azure OpenAI,則 metric
應該是 "cosine",否則請使用與您所使用內嵌模型相關聯的相似度計量。 支援的值為 cosine
dotProduct
euclidean
。
將向量欄位新增至欄位集合
欄位集合必須包含文件索引鍵的欄位、向量欄位,以及混合式搜尋案例所需的任何其他欄位。
向量欄位的特點是 其資料類型 、 dimensions
根據用來輸出向量和向量配置檔的內嵌模型的屬性。
如果您只想要正式推出的功能,請使用此版本。
使用建立或更新索引 來建立索引。
使用下列屬性定義向量欄位。 您可以為每個欄位儲存一個產生的內嵌。 針對每個向量欄位:
type
必須 Collection(Edm.Single)
在此 API 版本中。
dimensions
是內嵌模型所產生的維度數目。 針對 text-embedding-ada-002,其為 1536。
vectorSearchProfile
是在索引中其他地方所定義的設定檔名稱。
searchable
必須是 true。
retrievable
可以是 true 或 false。 True 會以純文字傳回原始向量 (其中 1536 個),並取用儲存空間。 如果您要將向量結果傳遞至下游應用程式,請設定為 true。
filterable
、facetable
、sortable
必須是 false。
如果您想要在向量查詢 上叫用預先篩選或後置篩選 ,請將可篩選的非向量欄位新增至集合,例如 filterable
設為 true 的 "title"。
新增其他欄位,為您要編製索引的文字內容定義其要旨和結構。 您至少需要一個文件索引鍵。
您也應該在查詢或其回應中新增有用的欄位。 下列範例顯示標題和內容的向量欄位 ("titleVector"、"contentVector"),相當於向量。 其也會提供對等文字內容 ("title"、"content") 的欄位,有助於搜尋結果中的排序、篩選和讀取。
下列範例顯示欄位集合:
PUT https://my-search-service.search.windows.net/indexes/my-index?api-version=2023-11-01&allowIndexDowntime=true
Content-Type: application/json
api-key: {{admin-api-key}}
{
"name": "{{index-name}}",
"fields": [
{
"name": "id",
"type": "Edm.String",
"key": true,
"filterable": true
},
{
"name": "title",
"type": "Edm.String",
"searchable": true,
"filterable": true,
"sortable": true,
"retrievable": true
},
{
"name": "titleVector",
"type": "Collection(Edm.Single)",
"searchable": true,
"retrievable": true,
"dimensions": 1536,
"vectorSearchProfile": "my-default-vector-profile"
},
{
"name": "content",
"type": "Edm.String",
"searchable": true,
"retrievable": true
},
{
"name": "contentVector",
"type": "Collection(Edm.Single)",
"searchable": true,
"retrievable": true,
"dimensions": 1536,
"vectorSearchProfile": "my-default-vector-profile"
}
],
"vectorSearch": {
"algorithms": [
{
"name": "my-hnsw-config-1",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
}
],
"profiles": [
{
"name": "my-default-vector-profile",
"algorithm": "my-hnsw-config-1"
}
]
}
}
此 API 版本是以 2023-10-01-preview 為基礎,新增對 窄數據類型 和 純量量化 的支援。
使用建立 或更新索引預覽 REST API 來定義索引的欄位集合。
將向量欄位新增至欄位集合。 您可以為每個文件欄位儲存一個產生的內嵌。 針對每個向量欄位:
type
可以是 Collection(Edm.Single)
、、Collection(Edm.Half)
、 Collection(Edm.Int16)
Collection(Edm.SByte)
dimensions
是內嵌模型所產生的維度數目。 針對 text-embedding-ada-002,其為 1536。
vectorSearchProfile
是在索引中其他地方所定義的設定檔名稱。
searchable
必須是 true。
retrievable
可以是 true 或 false。 True 會以純文字傳回原始向量 (其中 1536 個),並取用儲存空間。 如果您要將向量結果傳遞至下游應用程式,請設定為 true。 如果 stored
為 false,則為 False。
stored
是僅適用於向量欄位的新布爾值屬性。 True 會儲存在搜尋結果中傳回的向量複本。 False 會在編製索引期間捨棄該複本。 您可以搜尋向量,但無法傳回結果中的向量。
filterable
、facetable
、sortable
必須是 false。
下列範例顯示欄位集合:
PUT https://my-search-service.search.windows.net/indexes/my-index?api-version=2024-03-01-Preview&allowIndexDowntime=true
Content-Type: application/json
api-key: {{admin-api-key}}
{
"name": "{{index-name}}",
"fields": [
{
"name": "id",
"type": "Edm.String",
"key": true,
"filterable": true
},
{
"name": "firstVectorfield-float32-embeddings",
"type": "Collection(Edm.Single)",
"searchable": true,
"retrievable": false,
"stored": false,
"dimensions": 1536,
"vectorSearchProfile": "my-default-vector-profile"
},
{
"name": "secondVectorfield-float16-embeddings",
"type": "Collection(Edm.Half)",
"searchable": true,
"retrievable": false,
"stored": false,
"vectorSearchProfile": "my-default-vector-profile"
},
{
"name": "thirdVectorfield-int8-embeddings-for-my-custom-quantization-output",
"type": "Collection(Edm.SByte)",
"searchable": true,
"retrievable": false,
"stored": false,
"vectorSearchProfile": "my-default-vector-profile"
},
],
"vectorSearch": {
"algorithms": [
{
"name": "my-hnsw-config-1",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
}
],
"profiles": [
{
"name": "my-default-vector-profile",
"algorithm": "my-hnsw-config-1"
}
]
}
}
在下列 REST API 範例中,"title" 和 "content" 包含全文檢索搜尋和語意排名中使用的文字內容,而 “titleVector” 和 “contentVector” 則包含向量資料。 在此 API 版本中,您可以使用索引器和技能集,使用 整合向量化來填入向量 欄位。 索引定義不會變更,但您可以將索引器和技能新增至您的解決方案,以填入欄位。
使用建立 或更新索引預覽 REST API 來定義索引的欄位集合。
將向量欄位新增至欄位集合。 您可以為每個文件欄位儲存一個產生的內嵌。 針對每個向量欄位:
type
必須是 Collection(Edm.Single)
。
dimensions
是內嵌模型所產生的維度數目。 針對 text-embedding-ada-002,其為 1536。
vectorSearchProfile
是在索引中其他地方所定義的設定檔名稱。
searchable
必須是 true。
retrievable
可以是 true 或 false。 True 會以純文字傳回原始向量 (其中 1536 個),並取用儲存空間。 如果您要將向量結果傳遞至下游應用程式,請設定為 true。
filterable
、facetable
、sortable
必須是 false。
如果您想要在 [向量查詢](vector-search-how-to-query.md 上叫用預先篩選或後置篩選 ,請將可篩選的非向量欄位新增至集合,例如 filterable
設為 true 的 "title"
新增其他欄位,為您要編製索引的文字內容定義其要旨和結構。 您至少需要一個文件索引鍵。
您也應該在查詢或其回應中新增有用的欄位。 下列範例顯示標題和內容的向量欄位 ("titleVector"、"contentVector"),相當於向量。 其也會提供對等文字內容 ("title"、"content") 的欄位,有助於搜尋結果中的排序、篩選和讀取。
下列範例顯示欄位集合:
PUT https://my-search-service.search.windows.net/indexes/my-index?api-version=2023-10-01-Preview&allowIndexDowntime=true
Content-Type: application/json
api-key: {{admin-api-key}}
{
"name": "{{index-name}}",
"fields": [
{
"name": "id",
"type": "Edm.String",
"key": true,
"filterable": true
},
{
"name": "title",
"type": "Edm.String",
"searchable": true,
"filterable": true,
"sortable": true,
"retrievable": true
},
{
"name": "titleVector",
"type": "Collection(Edm.Single)",
"searchable": true,
"retrievable": true,
"dimensions": 1536,
"vectorSearchProfile": "my-default-vector-profile"
},
{
"name": "content",
"type": "Edm.String",
"searchable": true,
"retrievable": true
},
{
"name": "contentVector",
"type": "Collection(Edm.Single)",
"searchable": true,
"retrievable": true,
"dimensions": 1536,
"vectorSearchProfile": "my-default-vector-profile"
}
],
"vectorSearch": {
"algorithms": [
{
"name": "my-hnsw-config-1",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
}
],
"profiles": [
{
"name": "my-default-vector-profile",
"algorithm": "my-hnsw-config-1"
}
]
}
}
2023-07-01-Preview 是第一個支援向量案例的 REST API 版本。
在下列 REST API 範例中,“title” 和 “content” 包含全文搜索和語意排名中使用的文字內容,而 “titleVector” 和 “contentVector” 則包含外部產生的向量數據。
使用建立 或更新索引預覽 REST API 來定義索引的欄位集合。
將向量欄位新增至欄位集合。 您可以為每個文件欄位儲存一個產生的內嵌。 針對每個向量欄位:
指派 Collection(Edm.Single)
數據類型。
提供向量搜尋演算法設定的名稱。
提供內嵌模型所產生的維度數目。
設定屬性:
"searchable" 必須是 "true"。
"retrievable" 設定為 "true" 可讓您顯示原始向量 (例如,做為驗證步驟),但這樣做會增加儲存體。 如果您不需要傳回原始向量,請設定為 "false"。 您不需要傳回查詢的向量,但如果您要將向量結果傳遞至下游應用程式,請將 "retrievable" 設定為 "true"。
"filterable"、"facetable"、"sortable" 欄位必須是 "false"。 請不要將其設定為 "true",因為這些行為不適用於向量欄位的內容,而且要求將會失敗。
新增其他欄位,為您要編製索引的文字內容定義其要旨和結構。 您至少需要一個文件索引鍵。
您也應該在查詢或其回應中新增有用的欄位。 下列範例顯示標題和內容的向量欄位 ("titleVector"、"contentVector"),相當於向量。 其也會提供對等文字內容 ("title"、"content") 的欄位,有助於搜尋結果中的排序、篩選和讀取。
具有所描述元素的索引定義看起來像這樣:
PUT https://my-search-service.search.windows.net/indexes/my-index?api-version=2023-07-01-Preview&allowIndexDowntime=true
Content-Type: application/json
api-key: {{admin-api-key}}
{
"name": "{{index-name}}",
"fields": [
{
"name": "id",
"type": "Edm.String",
"key": true,
"filterable": true
},
{
"name": "title",
"type": "Edm.String",
"searchable": true,
"filterable": true,
"sortable": true,
"retrievable": true
},
{
"name": "titleVector",
"type": "Collection(Edm.Single)",
"searchable": true,
"retrievable": true,
"dimensions": 1536,
"vectorSearchConfiguration": "vectorConfig"
},
{
"name": "content",
"type": "Edm.String",
"searchable": true,
"retrievable": true
},
{
"name": "contentVector",
"type": "Collection(Edm.Single)",
"searchable": true,
"retrievable": true,
"dimensions": 1536,
"vectorSearchConfiguration": "vectorConfig"
}
],
"vectorSearch": {
"algorithmConfigurations": [
{
"name": "vectorConfig",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
}
]
}
}
載入用於編製索引的向量資料
您為了編制索引而提供的內容必須符合索引結構描述,並包含文件索引鍵的唯一字串值。 預先向量化的資料會載入至一或多個向量欄位,這些欄位可以與其他包含英數內容的欄位共存。
您可以使用推送或提取方法 進行資料擷取。
使用 Documents - Index 將向量和非函式數據載入索引中。 用於編製索引的推送 API 在所有穩定和預覽版本中都相同。 使用下列任何 API 載入檔案:
POST https://{{search-service-name}}.search.windows.net/indexes/{{index-name}}/docs/index?api-version=2023-11-01
Content-Type: application/json
api-key: {{admin-api-key}}
{
"value": [
{
"id": "1",
"title": "Azure App Service",
"content": "Azure App Service is a fully managed platform for building, deploying, and scaling web apps. You can host web apps, mobile app backends, and RESTful APIs. It supports a variety of programming languages and frameworks, such as .NET, Java, Node.js, Python, and PHP. The service offers built-in auto-scaling and load balancing capabilities. It also provides integration with other Azure services, such as Azure DevOps, GitHub, and Bitbucket.",
"category": "Web",
"titleVector": [
-0.02250031754374504,
. . .
],
"contentVector": [
-0.024740582332015038,
. . .
],
"@search.action": "upload"
},
{
"id": "2",
"title": "Azure Functions",
"content": "Azure Functions is a serverless compute service that enables you to run code on-demand without having to manage infrastructure. It allows you to build and deploy event-driven applications that automatically scale with your workload. Functions support various languages, including C#, F#, Node.js, Python, and Java. It offers a variety of triggers and bindings to integrate with other Azure services and external services. You only pay for the compute time you consume.",
"category": "Compute",
"titleVector": [
-0.020159931853413582,
. . .
],
"contentVector": [
-0.02780858241021633,
. . .
],
"@search.action": "upload"
}
. . .
]
}
所有較新的預覽版本都會使用提取 API(索引器和技能集)在編製索引和查詢期間進行整合向量化。
索引子可以擷取來源文件中的向量欄位並編製其索引,前提是索引結構描述符合向量欄位需求和預覽版 REST API。 資料來源會以資料來源支援的任何格式提供向量 (例如 JSON 中的字串)。 索引子假設輸入為 Collection(Edm.Single)
的欄位包含向量,並將該內容編製為向量索引。
技能與向量化工具可用來產生內嵌。 若要在編製索引期間進行向量化,請選擇下列技能:
檢查您的索引以取得向量內容
基於驗證目的,您可以使用 Azure 入口網站中的搜尋總管或 REST API 呼叫來查詢索引。 由於 Azure AI 搜尋服務無法將向量轉換成人類可讀取的文字,因此請嘗試從相同文件中傳回提供相符項目辨識項的欄位。 例如,如果向量查詢以 "titleVector" 欄位為目標,您可以為搜尋結果選取 "title"。
欄位必須歸屬為 "retrievable",才能包含在結果中。
您可以使用搜尋總管 來查詢索引。 搜尋總管有兩個檢視:查詢檢視 (預設值) 和 JSON 檢視。
下列 REST API 範例是向量查詢,但只會傳回非向量欄位 (標題、內容、類別)。 只有標示為 "retrievable" 的欄位才能在搜尋結果中傳回。
POST https://my-search-service.search.windows.net/indexes/my-index/docs/search?api-version=2023-11-01
Content-Type: application/json
api-key: {{admin-api-key}}
{
"vector": {
"value": [
-0.009154141,
0.018708462,
. . .
-0.02178128,
-0.00086512347
],
"fields": "contentVector",
"k": 5
},
"select": "title, content, category"
}
更新向量存放區
若要更新向量存放區,請修改結構描述,如有必要,重新載入文件以填入新的欄位。 結構描述更新的 API 包括建立或更新索引 (REST) 、Azure SDK for .NET 中的 CreateOrUpdateIndex 、Azure SDK for Python 中的 create_or_update_index ,以及其他 Azure SDK 中的類似方法。
更新索引的標準指引涵蓋在卸除和重建索引 中。
重點包括:
下一步
做為下一個步驟,我們建議在搜尋索引中查詢向量資料 。
azure-search-vector 存放庫中的程式碼範例示範端對端工作流程,其中包含結構描述定義、向量化、編製索引和查詢。
有 Python 、C# 和 JavaScript 的程式碼示範。