向量搜尋快速入門
若要啟用您的機器學習模型,向量搜尋會使用資料在資料庫中依相似性進行比較,即使並未明確由連線定義。向量是浮點型陣列,代表特定的物件或實體。
向量搜尋的基礎在於嵌入,這是文字的精簡表示,以浮點數字向量表示。這些嵌入是透過將文字傳輸到 API 而產生,該 API 使用神經網路將輸入轉換為固定長度的向量。嵌入擷取文字的語意意義,提供比傳統基於術語的方法更細緻的理解。向量表示允許實質上類似的輸入產生幾何上接近的輸出向量;不類似的輸入在幾何上相距較遠。
若要啟用向量搜尋,新的 vector
資料類型會在您的 Cassandra 資料庫中使用向量搜尋提供。
先決條件
沒有先決條件任務。
一般來說,若要將向量搜尋與 Apache Cassandra 搭配使用,您會遵循這些說明
嵌入在此快速入門中隨機產生。一般來說,您會將您的原始文件/內容透過嵌入產生器執行,以及您要求比對的查詢。此範例只是為了顯示如何使用 CQL 建立向量搜尋資料物件的機制。
建立向量鍵空間
建立您想用於向量搜尋資料表的鍵空間。這個範例使用 cycling
作為 鍵空間名稱
CREATE KEYSPACE IF NOT EXISTS cycling
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };
建立向量資料表
在您的鍵空間建立新的資料表,包含用於向量的 comments_vector
欄位。下列程式碼建立一個具有五個值的向量
CREATE TABLE IF NOT EXISTS cycling.comments_vs (
record_id timeuuid,
id uuid,
commenter text,
comment text,
comment_vector VECTOR <FLOAT, 5>,
created_at timestamp,
PRIMARY KEY (id, created_at)
)
WITH CLUSTERING ORDER BY (created_at DESC);
您也可以選擇變更現有的資料表來新增向量欄位
ALTER TABLE cycling.comments_vs
ADD comment_vector VECTOR <FLOAT, 5>(1)
建立向量索引
使用儲存附加索引 (SAI) 建立自訂索引
CREATE INDEX IF NOT EXISTS ann_index
ON cycling.comments_vs(comment_vector) USING 'sai';
有關 SAI 的更多資訊,請參閱 儲存附加索引 文件。
可以建立索引,其中包含定義相似度函數的選項
|
將向量資料載入您的資料庫
使用新的類型將資料插入資料表
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
e7ae5cf3-d358-4d99-b900-85902fda9bb0,
'2017-02-14 12:43:20-0800',
'Raining too hard should have postponed',
'Alex',
[0.45, 0.09, 0.01, 0.2, 0.11]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
e7ae5cf3-d358-4d99-b900-85902fda9bb0,
'2017-03-21 13:11:09.999-0800',
'Second rest stop was out of water',
'Alex',
[0.99, 0.5, 0.99, 0.1, 0.34]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
e7ae5cf3-d358-4d99-b900-85902fda9bb0,
'2017-04-01 06:33:02.16-0800',
'LATE RIDERS SHOULD NOT DELAY THE START',
'Alex',
[0.9, 0.54, 0.12, 0.1, 0.95]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
c7fceba0-c141-4207-9494-a29f9809de6f,
totimestamp(now()),
'The gift certificate for winning was the best',
'Amy',
[0.13, 0.8, 0.35, 0.17, 0.03]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
c7fceba0-c141-4207-9494-a29f9809de6f,
'2017-02-17 12:43:20.234+0400',
'Glad you ran the race in the rain',
'Amy',
[0.3, 0.34, 0.2, 0.78, 0.25]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
c7fceba0-c141-4207-9494-a29f9809de6f,
'2017-03-22 5:16:59.001+0400',
'Great snacks at all reststops',
'Amy',
[0.1, 0.4, 0.1, 0.52, 0.09]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
c7fceba0-c141-4207-9494-a29f9809de6f,
'2017-04-01 17:43:08.030+0400',
'Last climb was a killer',
'Amy',
[0.3, 0.75, 0.2, 0.2, 0.5]
);
使用 CQL 查詢向量資料
若要使用向量搜尋查詢資料,請使用 SELECT
查詢
SELECT * FROM cycling.comments_vs
ORDER BY comment_vector ANN OF [0.15, 0.1, 0.1, 0.35, 0.55]
LIMIT 3;
若要取得最接近查詢資料的最佳計分節點的相似度計算作為結果的一部分,請使用 SELECT
查詢
SELECT comment, similarity_cosine(comment_vector, [0.2, 0.15, 0.3, 0.2, 0.05])
FROM cycling.comments_vs
ORDER BY comment_vector ANN OF [0.1, 0.15, 0.3, 0.12, 0.05]
LIMIT 1;
此類查詢支援的函數為
-
similarity_dot_product
-
similarity_cosine
-
similarity_euclidean
其中參數為 (<vector_column>, <embedding_value>)。兩個參數都表示向量。
|
透過程式碼範例,您會看到我們向量搜尋的實際範例。載入您自己的資料並使用搜尋功能。