Vector search: embeddings
embeddings finds the entities whose vectors are nearest to a query vector.
It requires database. It keeps an index of the #vf fields that its
settings name, and it adds the embeddings/nearest query clause. The search
is exact: it compares the query with each vector of the field.
| Setting | Accepts | Purpose | Default |
|---|---|---|---|
fields | A map from a field name to a map of the settings below, or true | The #vf fields to index, each with its own settings or the ones at the top. | No fields |
dimensions | An integer of 1 or more | The number of lanes of each vector of the field. | None: each field gets one |
metric | cosine, squared-euclidean or dot-product | How embeddings/nearest measures the distance. | None: each field gets one |
normalization | unit or none | unit divides each vector, and the query vector, by its length before the index keeps or compares it. none keeps the vectors as written. | None: each field gets one |
A setting at the top of the map is the default for each field. Each field
must get all three of dimensions, metric and normalization. The plugin
refuses an unknown setting, or a value that the table does not accept, when
the database opens.
Dependencies
- Requires
database
Configuration
Select this plugin, and database with a path, when you open the database:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 000 | 54 52 4F 4E | magic TRON |
| 004 | 4C 70617468 | txt "path" |
| 009 | 6C 6170702E6462 | txt "app.db" |
| 010 | 0F 0A 04000000 0900 0000 | map leaf, size 10: @4 → @9 |
| 01A | 6C 236C6F63616C | txt "#local" |
| 021 | 6C 636F73696E65 | txt "cosine" |
| 028 | 0F 0A 1A000000 2100 0000 | map leaf, size 10: @1A → @21 |
| 032 | 6C 236C6F63616C | txt "#local" |
| 039 | 4C 756E6974 | txt "unit" |
| 03E | 0F 0A 32000000 3900 0000 | map leaf, size 10: @32 → @39 |
| 048 | DC 6E6F726D616C69 7A6174696F6E | txt "normalization" |
| 056 | 0F 0A 48000000 3E00 0000 | map leaf, size 10: @48 → @3E |
| 060 | AC 64696D656E7369 6F6E73 | txt "dimensions" |
| 06B | 02 80010000000000 00 | i64 384 |
| 074 | 0F 0A 60000000 6B00 0000 | map leaf, size 10: @60 → @6B |
| 07E | 6C 6D6574726963 | txt "metric" |
| 085 | 0F 0A 7E000000 2800 0000 | map leaf, size 10: @7E → @28 |
| 08F | 07 12 E0000000 5600 0000 74000000 8500 0000 | map branch, size 18, depth 0 (root), slots 5, 6, 7: @56 → @74 → @85 |
| 0A1 | 9C 656D6265646469 6E67 | txt "embedding" |
| 0AB | 0F 0A A1000000 8F00 0000 | map leaf, size 10: @A1 → @8F |
| 0B5 | 6C 6669656C6473 | txt "fields" |
| 0BC | 0F 0A B5000000 AB00 0000 | map leaf, size 10: @B5 → @AB |
| 0C6 | 8C 64617461626173 65 | txt "database" |
| 0CF | 0F 0A C6000000 1000 0000 | map leaf, size 10: @C6 → @10 |
| 0D9 | AC 656D6265646469 6E6773 | txt "embeddings" |
| 0E4 | 0F 0A D9000000 BC00 0000 | map leaf, size 10: @D9 → @BC |
| 0EE | 07 0E 02020000 CF00 0000 E4000000 | map branch, size 14, depth 1, slots 1, 9: @CF → @E4 |
| 0FC | 07 0A 00100000 EE00 0000 | map branch, size 10, depth 0 (root), slots 12: @EE |
| 106 | 7C 706C7567696E73 | txt "plugins" |
| 10E | 0F 0A 06010000 FC00 0000 | map leaf, size 10: @106 → @FC |
| 118 | 0E010000 00000000 | root → @10E; previous: none |
Values
The value of an indexed field must be a #vf with dimensions lanes. Each
write that gives the field another value fails with <invalid value>, and
nothing of that write commits. With normalization unit, a vector whose
lanes are all 0 also fails. A write that removes the field removes the entity
from the index in the same transaction.
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 00 | 54 52 4F 4E | magic TRON |
| 04 | 3C 237666 | txt "#vf" |
| 08 | 15 18 B81E85EB51B8 BE3FB81E85EB51B8 9EBF295C8FC2F528 EC3F | bin, length 24 |
| 22 | 0F 0A 04000000 0800 0000 | map leaf, size 10: @4 → @8 |
| 2C | 9C 656D6265646469 6E67 | txt "embedding" |
| 36 | 0F 0A 2C000000 2200 0000 | map leaf, size 10: @2C → @22 |
| 40 | 5C 7469746C65 | txt "title" |
| 46 | 5C 4170706C65 | txt "Apple" |
| 4C | 0F 0A 40000000 4600 0000 | map leaf, size 10: @40 → @46 |
| 56 | 07 0E 00600000 3600 0000 4C000000 | map branch, size 14, depth 0 (root), slots 13, 14: @36 → @4C |
| 64 | 6C 5F6170706C65 | txt "_apple" |
| 6B | 0F 0A 64000000 5600 0000 | map leaf, size 10: @64 → @56 |
| 75 | 5C 7061746368 | txt "patch" |
| 7B | 0F 0A 75000000 6B00 0000 | map leaf, size 10: @75 → @6B |
| 85 | 7B000000 00000000 | root → @7B; previous: none |
The index keeps each lane as a 32-bit float: 4 bytes a lane, so a vector of 1,536 lanes uses 6,144 bytes of index. The facts keep the 64-bit lanes. When the settings change, the next open makes the index again from the current facts.
The clause
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 000 | 54 52 4F 4E | magic TRON |
| 004 | 6C 236C6F63616C | txt "#local" |
| 00B | 6C 3F7469746C65 | txt "?title" |
| 012 | 0F 0A 04000000 0B00 0000 | map leaf, size 10: @4 → @B |
| 01C | 6C 236C6F63616C | txt "#local" |
| 023 | 2C 3F64 | txt "?d" |
| 026 | 0F 0A 1C000000 2300 0000 | map leaf, size 10: @1C → @23 |
| 030 | 0E 11 00 0300 020000 00 12000000 260000 00 | array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @12 → @26 |
| 041 | 6C 236C6F63616C | txt "#local" |
| 048 | 14 12 656D62656464 696E67732F6E6561 72657374 | txt "embeddings/nearest" |
| 05C | 0F 0A 41000000 4800 0000 | map leaf, size 10: @41 → @48 |
| 066 | 6C 236C6F63616C | txt "#local" |
| 06D | 9C 656D6265646469 6E67 | txt "embedding" |
| 077 | 0F 0A 66000000 6D00 0000 | map leaf, size 10: @66 → @6D |
| 081 | 6C 23636F6E7374 | txt "#const" |
| 088 | 5C 7175657279 | txt "query" |
| 08E | 0F 0A 81000000 8800 0000 | map leaf, size 10: @81 → @88 |
| 098 | 6C 236C6F63616C | txt "#local" |
| 09F | 2C 3F65 | txt "?e" |
| 0A2 | 0F 0A 98000000 9F00 0000 | map leaf, size 10: @98 → @9F |
| 0AC | 6C 236C6F63616C | txt "#local" |
| 0B3 | 2C 3F64 | txt "?d" |
| 0B6 | 0F 0A AC000000 B300 0000 | map leaf, size 10: @AC → @B3 |
| 0C0 | 5C 746F702D6B | txt "top-k" |
| 0C6 | 02 05000000000000 00 | i64 5 |
| 0CF | 0F 0A C0000000 C600 0000 | map leaf, size 10: @C0 → @C6 |
| 0D9 | 0E 21 00 3F00 060000 00 5C000000 770000 00 8E000000 A20000 00 B6000000 CF0000 00 | array leaf, size 33, shift 0, bitmap 003F, count 6 (root): @5C → @77 → @8E → @A2 → @B6 → @CF |
| 0FA | 6C 236C6F63616C | txt "#local" |
| 101 | 2C 3F65 | txt "?e" |
| 104 | 0F 0A FA000000 0101 0000 | map leaf, size 10: @FA → @101 |
| 10E | 6C 236C6F63616C | txt "#local" |
| 115 | 5C 7469746C65 | txt "title" |
| 11B | 0F 0A 0E010000 1501 0000 | map leaf, size 10: @10E → @115 |
| 125 | 6C 236C6F63616C | txt "#local" |
| 12C | 6C 3F7469746C65 | txt "?title" |
| 133 | 0F 0A 25010000 2C01 0000 | map leaf, size 10: @125 → @12C |
| 13D | 0E 15 00 0700 030000 00 04010000 1B0100 00 33010000 | array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @104 → @11B → @133 |
| 152 | 0E 11 00 0300 020000 00 D9000000 3D0100 00 | array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @D9 → @13D |
| 163 | 6C 236C6F63616C | txt "#local" |
| 16A | 2C 3F64 | txt "?d" |
| 16D | 0F 0A 63010000 6A01 0000 | map leaf, size 10: @163 → @16A |
| 177 | 0E 0D 00 0100 010000 00 6D010000 | array leaf, size 13, shift 0, bitmap 0001, count 1 (root): @16D |
| 184 | 5C 7768657265 | txt "where" |
| 18A | 0F 0A 84010000 5201 0000 | map leaf, size 10: @184 → @152 |
| 194 | 4C 66696E64 | txt "find" |
| 199 | 0F 0A 94010000 3000 0000 | map leaf, size 10: @194 → @30 |
| 1A3 | 8C 6F726465722D62 79 | txt "order-by" |
| 1AC | 0F 0A A3010000 7701 0000 | map leaf, size 10: @1A3 → @177 |
| 1B6 | 07 12 0A800000 8A01 0000 99010000 AC01 0000 | map branch, size 18, depth 0 (root), slots 1, 3, 15: @18A → @199 → @1AC |
| 1C8 | B6010000 00000000 | root → @1B6; previous: none |
| Position | Member | Accepts |
|---|---|---|
| 1 | embeddings/nearest | The clause name |
| 2 | field | A field that the settings index |
| 3 | query | A #vf, a #vi or a list of numbers with dimensions items: a literal or an :input |
| 4 | entity | A ?variable |
| 5 | distance | A ?variable |
| 6 | options | An optional map |
| Option | Accepts | Default |
|---|---|---|
top-k | The maximum number of hits, an integer of 1 or more | 100 |
metric | The metric of the field | The metric of the field |
filters | A map from a field to a value: each hit must have that value | No filters |
The distance is lower for a nearer entity:
| Metric | Distance |
|---|---|
cosine | 1 minus the cosine of the angle between the vectors, from 0 to 2 |
squared-euclidean | The sum of the squared differences of the lanes |
dot-product | The dot product, multiplied by -1 |
The clause gives the nearest entities first. Two entities at the same
distance come in entity order. top-k counts only the hits that the other
clauses of the query accept. A query with as-of compares the vectors of that
state.