向量函式
建立、比較並操作向量嵌入
向量函式
Turso 擴充功能:向量函式是 Turso 特有的功能,可直接在 SQL 中處理向量嵌入。這些函式並非 SQLite 標準的一部分。
Turso 提供內建函式來儲存與查詢向量嵌入。向量以精簡的二進位格式儲存為 BLOB,並可使用距離函式進行比較,以執行相似度搜尋。
建立向量
vector32
從 JSON 數字陣列建立 32 位元浮點數向量。
vector32(json_array)
| 參數 | 型別 | 說明 |
|---|---|---|
json_array |
TEXT | 數字組成的 JSON 陣列,例如 '[1.0, 2.0, 3.0]' |
傳回值: BLOB — 以 32 位元浮點二進位格式表示的向量。
這是最常見的向量格式,在精確度與儲存效率之間取得良好平衡。每個維度使用 4 個位元組。
SELECT vector32('[1.0, 2.0, 3.0]');
-- (BLOB: 12 bytes)
-- Store embeddings in a table
CREATE TABLE documents (
id INTEGER PRIMARY KEY,
content TEXT,
embedding BLOB
);
INSERT INTO documents VALUES (1, 'Introduction to databases', vector32('[0.1, 0.3, 0.5, 0.7]'));
vector64
從 JSON 數字陣列建立 64 位元浮點數向量。
vector64(json_array)
| 參數 | 型別 | 說明 |
|---|---|---|
json_array |
TEXT | 數字組成的 JSON 陣列,例如 '[1.0, 2.0, 3.0]' |
傳回值: BLOB — 以 64 位元浮點二進位格式表示的向量。
當您需要更高精確度時可使用此格式。每個維度使用 8 個位元組。
SELECT vector64('[1.0, 2.0, 3.0]');
-- (BLOB: 24 bytes)
vector
使用預設格式(32 位元浮點數)建立向量。
vector(json_array)
| 參數 | 型別 | 說明 |
|---|---|---|
json_array |
TEXT | 數字組成的 JSON 陣列 |
傳回值: BLOB — 以預設(32 位元浮點數)二進位格式表示的向量。
SELECT vector('[1.0, 2.0, 3.0]');
vector8
從 JSON 數字陣列建立 8 位元量化向量。
vector8(json_array)
| 參數 | 型別 | 說明 |
|---|---|---|
json_array |
TEXT | 整數值(0-255)組成的 JSON 陣列 |
傳回值: BLOB — 以 8 位元整數二進位格式表示的向量。
此格式適用於節省儲存空間的近似表示。每個維度使用 1 個位元組。
SELECT vector8('[128, 64, 255, 0]');
vector1bit
從 JSON 陣列建立 1 位元二進位向量。
vector1bit(json_array)
| 參數 | 型別 | 說明 |
|---|---|---|
json_array |
TEXT | 由 0 與 1 組成的 JSON 陣列 |
傳回值: BLOB — 以 1 位元二進位格式表示的向量。
此格式適用於極度精簡的表示。八個維度會打包進單一位元組中。
SELECT vector1bit('[1, 0, 1, 1, 0, 0, 1, 0]');
vector32_sparse
從 JSON 陣列建立 32 位元浮點數稀疏向量。僅儲存非零分量,對於大多為零的高維向量相當有效率。
vector32_sparse(json_array)
| 參數 | 型別 | 說明 |
|---|---|---|
json_array |
TEXT | 數字組成的 JSON 陣列,通常包含大量零值 |
傳回值: BLOB — 以 32 位元浮點稀疏二進位格式表示的向量。
SELECT vector32_sparse('[0.0, 0.0, 1.5, 0.0, 0.0, 2.5]');
稀疏向量可使用實驗性的稀疏向量索引方法建立索引(啟用 --experimental-index-method)。請參閱 CREATE INDEX。
距離函式
距離函式會計算兩個向量之間的距離。數值越小代表向量越相似。
vector_distance_cos
計算兩個向量之間的餘弦距離,定義為 1 - cosine_similarity。結果為 0 表示兩個向量方向相同;結果為 1 表示兩者正交。
vector_distance_cos(v1, v2)
| 參數 | 型別 | 說明 |
|---|---|---|
v1 |
BLOB | 第一個向量 |
v2 |
BLOB | 第二個向量(維度數量必須與 v1 相同) |
傳回值: REAL — 兩個向量之間的餘弦距離。
SELECT vector_distance_cos(
vector32('[1.0, 0.0, 0.0]'),
vector32('[0.0, 1.0, 0.0]')
);
-- 1.0 (orthogonal vectors)
SELECT vector_distance_cos(
vector32('[1.0, 0.0]'),
vector32('[1.0, 0.0]')
);
-- 0.0 (identical vectors)
vector_distance_l2
計算兩個向量之間的歐幾里得(L2)距離。結果為 0 表示兩個向量完全相同。
vector_distance_l2(v1, v2)
| 參數 | 型別 | 說明 |
|---|---|---|
v1 |
BLOB | 第一個向量 |
v2 |
BLOB | 第二個向量(維度數量必須與 v1 相同) |
傳回值: REAL — 兩個向量之間的歐幾里得距離。
SELECT vector_distance_l2(
vector32('[1.0, 0.0, 0.0]'),
vector32('[0.0, 1.0, 0.0]')
);
-- 1.4142135623730951
SELECT vector_distance_l2(
vector32('[3.0, 4.0]'),
vector32('[0.0, 0.0]')
);
-- 5.0
vector_distance_dot
計算兩個向量之間的負內積距離。結果為內積的負值,因此數值越小(越負)代表相似度越高。
vector_distance_dot(v1, v2)
| 參數 | 型別 | 說明 |
|---|---|---|
v1 |
BLOB | 第一個向量 |
v2 |
BLOB | 第二個向量(維度數量必須與 v1 相同) |
傳回值: REAL — 兩個向量的負內積。
SELECT vector_distance_dot(
vector32('[1.0, 2.0, 3.0]'),
vector32('[4.0, 5.0, 6.0]')
);
-- -32.0 (dot product is 32)
vector_distance_jaccard
計算兩個二進位向量之間的 Jaccard 距離。最適合搭配 vector1bit 向量使用。
vector_distance_jaccard(v1, v2)
| 參數 | 型別 | 說明 |
|---|---|---|
v1 |
BLOB | 第一個向量 |
v2 |
BLOB | 第二個向量(維度數量必須與 v1 相同) |
傳回值: REAL — 兩個向量之間的 Jaccard 距離。
工具函式
vector_extract
將向量 BLOB 轉換回 JSON 文字表示。
vector_extract(vector_blob)
| 參數 | 型別 | 說明 |
|---|---|---|
vector_blob |
BLOB | 由 vector32、vector64 或其他向量函式建立的向量 |
傳回值: TEXT — 向量各分量的 JSON 陣列字串。
SELECT vector_extract(vector32('[1.5, 2.5, 3.5]'));
-- [1.500000,2.500000,3.500000]
vector_concat
將兩個或多個向量串接成單一向量。
vector_concat(v1, v2, ...)
| 參數 | 型別 | 說明 |
|---|---|---|
v1, v2, ... |
BLOB | 要串接的兩個或多個向量。所有向量必須為相同型別。 |
傳回值: BLOB — 依序包含所有輸入向量維度的新向量。
SELECT vector_extract(
vector_concat(
vector32('[1.0, 2.0]'),
vector32('[3.0, 4.0]')
)
);
-- [1.000000,2.000000,3.000000,4.000000]
vector_slice
擷取向量中一段連續的部分。
vector_slice(vector_blob, start, end)
| 參數 | 型別 | 說明 |
|---|---|---|
vector_blob |
BLOB | 來源向量 |
start |
INTEGER | 起始索引(從零開始,包含) |
end |
INTEGER | 結束索引(不包含) |
傳回值: BLOB — 包含從 start 到 end - 1 維度的新向量。
SELECT vector_extract(
vector_slice(vector32('[10.0, 20.0, 30.0, 40.0, 50.0]'), 1, 4)
);
-- [20.000000,30.000000,40.000000]
範例
建立含有向量嵌入的資料表
CREATE TABLE articles (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
content TEXT,
embedding BLOB
);
INSERT INTO articles VALUES (
1,
'Introduction to Machine Learning',
'Machine learning is a subset of artificial intelligence...',
vector32('[0.12, -0.34, 0.56, 0.78, -0.11, 0.45, -0.23, 0.67]')
);
INSERT INTO articles VALUES (
2,
'Database Design Patterns',
'Relational databases organize data into tables...',
vector32('[0.82, 0.15, -0.44, 0.23, 0.61, -0.33, 0.09, 0.77]')
);
INSERT INTO articles VALUES (
3,
'Neural Networks Explained',
'Neural networks are computing systems inspired by biological neural networks...',
vector32('[0.14, -0.28, 0.62, 0.71, -0.09, 0.51, -0.18, 0.59]')
);
使用 ORDER BY 距離進行相似度搜尋
-- Find articles most similar to a query embedding using cosine distance
SELECT
id,
title,
vector_distance_cos(embedding, vector32('[0.1, -0.3, 0.5, 0.8, -0.1, 0.4, -0.2, 0.6]')) AS distance
FROM articles
ORDER BY distance ASC
LIMIT 5;
餘弦距離與 L2 距離的比較
餘弦距離衡量兩個向量之間的角度,忽略向量長度。L2 距離衡量空間中的絕對距離。
-- Cosine distance: direction matters, not magnitude
SELECT vector_distance_cos(
vector32('[1.0, 0.0]'),
vector32('[100.0, 0.0]')
);
-- 0.0 (same direction, cosine distance is 0)
-- L2 distance: magnitude matters
SELECT vector_distance_l2(
vector32('[1.0, 0.0]'),
vector32('[100.0, 0.0]')
);
-- 99.0 (very far apart in absolute terms)
當您在意向量的方向時(例如文字嵌入的語意相似度),請使用餘弦距離。當絕對位置很重要時(例如地理空間座標或影像特徵向量),請使用 L2 距離。
操作向量資料
-- Inspect the contents of a stored vector
SELECT vector_extract(embedding) FROM articles WHERE id = 1;
-- Concatenate vectors from two different models
SELECT vector_extract(
vector_concat(
(SELECT embedding FROM articles WHERE id = 1),
(SELECT embedding FROM articles WHERE id = 2)
)
);
-- Extract the first 4 dimensions of an embedding
SELECT vector_extract(
vector_slice(embedding, 0, 4)
) FROM articles WHERE id = 1;
-- [0.120000,-0.340000,0.560000,0.780000]
根據預設,相似度搜尋會對資料表進行線性掃描。實驗性的稀疏向量索引方法可透過 --experimental-index-method 旗標啟用;若未啟用,針對大型資料集,建議以 WHERE 子句將搜尋範圍限制在部分資料列。
另請參閱
- CREATE INDEX:包含 FTS 在內的索引型別
- 資料型別:Turso 如何處理 BLOB