You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ydb/docs/en/core/yql/reference/yql-core/udf/list/knn.md
+77-2Lines changed: 77 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ Approximate methods do not perform a complete search of the source data. Due to
31
31
This document provides an [example of approximate search](#approximate-search-examples) using scalar quantization. This example does not require the creation of a secondary vector index.
32
32
33
33
**Scalar quantization** is a method to compress vectors by mapping coordinates to a smaller space.
34
-
{{ ydb-short-name }} support exact search for `Float`, `Int8`, `Uint8`, `Bit` vectors.
34
+
This module supports exact search for `Float`, `Int8`, `Uint8`, `Bit` vectors.
35
35
So, it's possible to apply scalar quantization from `Float` to one of these other types.
36
36
37
37
Scalar quantization decreases read/write times by reducing vector size in bytes. For example, after quantization from `Float` to `Bit,` each vector becomes 32 times smaller.
@@ -45,7 +45,7 @@ It is recommended to measure if such quantization provides sufficient accuracy/r
45
45
## Data types
46
46
47
47
In mathematics, a vector of real or integer numbers is used to store points.
48
-
In {{ ydb-short-name }}, vectors are stored in the `String` data type, which is a binary serialized representation of a vector.
48
+
In this module, vectors are stored in the `String` data type, which is a binary serialized representation of a vector.
49
49
50
50
## Functions
51
51
@@ -57,7 +57,9 @@ Conversion functions are needed to serialize vectors into an internal binary rep
57
57
58
58
All serialization functions wrap returned `String` data into [Tagged](../../types/special.md) types.
59
59
60
+
{% if backend_name == "YDB" %}
60
61
The binary representation of the vector can be stored in the {{ ydb-short-name }} table column. Currently {{ ydb-short-name }} does not support storing `Tagged`, so before storing binary representation vectors you must call [Untag](../../builtins/basic#as-tagged).
62
+
{% endif %}
61
63
62
64
#### Function signatures
63
65
@@ -123,6 +125,7 @@ Error: Failed to find UDF function: Knn.CosineDistance, reason: Error: Module: K
WHERE Knn::CosineDistance(embedding, $TargetEmbedding) < $R;
205
+
```
206
+
{% endif %}
167
207
168
208
## Approximate search examples
169
209
170
210
This example differs from the [exact search example](#еxact-search-examples) by using bit quantization.
171
211
172
212
This allows to first do a approximate preliminary search by the `embedding_bit` column, and then refine the results by the original vector column `embegging`.
UPSERT INTO Facts (id, user, fact, embedding, embedding_bit)
192
233
VALUES (123, "Williams", "Full name is John Williams", Untag(Knn::ToBinaryStringFloat($vector), "FloatVector"), Untag(Knn::ToBinaryStringBit($vector), "BitVector"));
193
234
```
235
+
{% else %}
236
+
### Data declaration
237
+
238
+
```sql
239
+
$vector = [1.f, 2.f, 3.f, 4.f];
240
+
$facts = AsList(
241
+
AsStruct(
242
+
123AS id, -- Id of fact
243
+
"Williams"AS user, -- User name
244
+
"Full name is John Williams"AS fact, -- Human-readable description of a user fact
245
+
Knn::ToBinaryStringFloat($vector) AS embedding, -- Binary representation of embedding vector
246
+
Knn::ToBinaryStringBit($vector) AS embedding_bit, -- Binary representation of embedding vector
247
+
),
248
+
);
249
+
```
250
+
{% endif %}
194
251
195
252
### Scalar quantization
196
253
@@ -219,6 +276,7 @@ Approximate search algorithm:
219
276
* an approximate list of vectors is obtained;
220
277
* we search this list without using quantization.
221
278
279
+
{% if backend_name == "YDB" %}
222
280
```sql
223
281
$K =10;
224
282
$Target = [1.2f, 2.3f, 3.4f, 4.5f];
@@ -234,3 +292,20 @@ WHERE id IN $Ids
234
292
ORDER BY Knn::CosineDistance(embedding, $TargetEmbeddingFloat)
Copy file name to clipboardExpand all lines: ydb/docs/ru/core/yql/reference/yql-core/udf/list/knn.md
+77-2Lines changed: 77 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ LIMIT 10;
31
31
В данном документе приведен [пример приближенного поиска](#примеры-приближенного-поиска) с помощью скалярного квантования, не требущий построения вторичного векторного индекса.
32
32
33
33
**Скалярное квантование** это метод сжатия векторов, когда множество координат отображаются в множество меньшей размерности.
34
-
{{ ydb-short-name }} поддерживает точный поиск по `Float`, `Int8`, `Uint8`, `Bit` векторам.
34
+
Этот модуль поддерживает точный поиск по `Float`, `Int8`, `Uint8`, `Bit` векторам.
35
35
Соответственно, возможно скалярное квантование из `Float` в один из этих типов.
36
36
37
37
Скалярное квантование уменьшает время необходимое для чтения/записи, поскольку число байт сокращается в разы.
@@ -46,7 +46,7 @@ LIMIT 10;
46
46
## Типы данных
47
47
48
48
В математике для хранения точек используется вектор вещественных или целых чисел.
49
-
В {{ ydb-short-name }} вектора хранятся в строковом типе данных `String`, который является бинарным сериализованным представлением вектора.
49
+
В этом модуле вектора представлены типом данных `String`, который является бинарным сериализованным представлением вектора.
50
50
51
51
## Функции
52
52
@@ -58,8 +58,10 @@ LIMIT 10;
58
58
59
59
Все функции сериализации упаковывают возвращаемые данные типа `String` в [Tagged](../../types/special.md) тип.
60
60
61
+
{% if backend_name == "YDB" %}
61
62
Бинарное представление вектора можно сохранить в {{ ydb-short-name }} колонку.
62
63
В настоящий момент {{ ydb-short-name }} не поддерживает хранение `Tagged` типов и поэтому перед сохранением бинарного представления векторов нужно извлечь `String` с помощью функции [Untag](../../builtins/basic#as-tagged).
64
+
{% endif %}
63
65
64
66
#### Сигнатуры функций
65
67
@@ -125,6 +127,7 @@ Error: Failed to find UDF function: Knn.CosineDistance, reason: Error: Module: K
WHERE Knn::CosineDistance(embedding, $TargetEmbedding) < $R;
207
+
```
208
+
{% endif %}
169
209
170
210
## Примеры приближенного поиска
171
211
172
212
Данный пример отличается от [примера с точным поиском](#примеры-точного-поиска) использованием битового квантования.
173
213
Это позволяет сначала делать грубый предварительный поиск по колонке `embedding_bit`, а затем уточнять результаты по основной колонке с векторами `embedding`.
UPSERT INTO Facts (id, user, fact, embedding, embedding_bit)
193
234
VALUES (123, "Williams", "Full name is John Williams", Untag(Knn::ToBinaryStringFloat($vector), "FloatVector"), Untag(Knn::ToBinaryStringBit($vector), "BitVector"));
194
235
```
236
+
{% else %}
237
+
### Декларация данных
238
+
239
+
```sql
240
+
$vector = [1.f, 2.f, 3.f, 4.f];
241
+
$facts = AsList(
242
+
AsStruct(
243
+
123AS id, -- Id of fact
244
+
"Williams"AS user, -- User name
245
+
"Full name is John Williams"AS fact, -- Human-readable description of a user fact
246
+
Knn::ToBinaryStringFloat($vector) AS embedding, -- Binary representation of embedding vector
247
+
Knn::ToBinaryStringBit($vector) AS embedding_bit, -- Binary representation of embedding vector
0 commit comments