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.

SettingAcceptsPurposeDefault
fieldsA map from a field name to a map of the settings below, or trueThe #vf fields to index, each with its own settings or the ones at the top.No fields
dimensionsAn integer of 1 or moreThe number of lanes of each vector of the field.None: each field gets one
metriccosine, squared-euclidean or dot-productHow embeddings/nearest measures the distance.None: each field gets one
normalizationunit or noneunit 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

Configuration

Select this plugin, and database with a path, when you open the database:

TRON288 B
Offset (hex)Bytes (hex)Meaning
00054 52 4F 4Emagic TRON
0044C 70617468txt "path"
0096C 6170702E6462txt "app.db"
0100F 0A 04000000 0900 0000map leaf, size 10: @4 → @9
01A6C 236C6F63616Ctxt "#local"
0216C 636F73696E65txt "cosine"
0280F 0A 1A000000 2100 0000map leaf, size 10: @1A → @21
0326C 236C6F63616Ctxt "#local"
0394C 756E6974txt "unit"
03E0F 0A 32000000 3900 0000map leaf, size 10: @32 → @39
048DC 6E6F726D616C69 7A6174696F6Etxt "normalization"
0560F 0A 48000000 3E00 0000map leaf, size 10: @48 → @3E
060AC 64696D656E7369 6F6E73txt "dimensions"
06B02 80010000000000 00i64 384
0740F 0A 60000000 6B00 0000map leaf, size 10: @60 → @6B
07E6C 6D6574726963txt "metric"
0850F 0A 7E000000 2800 0000map leaf, size 10: @7E → @28
08F07 12 E0000000 5600 0000 74000000 8500 0000map branch, size 18, depth 0 (root), slots 5, 6, 7: @56 → @74 → @85
0A19C 656D6265646469 6E67txt "embedding"
0AB0F 0A A1000000 8F00 0000map leaf, size 10: @A1 → @8F
0B56C 6669656C6473txt "fields"
0BC0F 0A B5000000 AB00 0000map leaf, size 10: @B5 → @AB
0C68C 64617461626173 65txt "database"
0CF0F 0A C6000000 1000 0000map leaf, size 10: @C6 → @10
0D9AC 656D6265646469 6E6773txt "embeddings"
0E40F 0A D9000000 BC00 0000map leaf, size 10: @D9 → @BC
0EE07 0E 02020000 CF00 0000 E4000000map branch, size 14, depth 1, slots 1, 9: @CF → @E4
0FC07 0A 00100000 EE00 0000map branch, size 10, depth 0 (root), slots 12: @EE
1067C 706C7567696E73txt "plugins"
10E0F 0A 06010000 FC00 0000map leaf, size 10: @106 → @FC

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.

TRON141 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
043C 237666txt "#vf"
0815 18 B81E85EB51B8 BE3FB81E85EB51B8 9EBF295C8FC2F528 EC3Fbin, length 24
220F 0A 04000000 0800 0000map leaf, size 10: @4 → @8
2C9C 656D6265646469 6E67txt "embedding"
360F 0A 2C000000 2200 0000map leaf, size 10: @2C → @22
405C 7469746C65txt "title"
465C 4170706C65txt "Apple"
4C0F 0A 40000000 4600 0000map leaf, size 10: @40 → @46
5607 0E 00600000 3600 0000 4C000000map branch, size 14, depth 0 (root), slots 13, 14: @36 → @4C
646C 5F6170706C65txt "_apple"
6B0F 0A 64000000 5600 0000map leaf, size 10: @64 → @56
755C 7061746368txt "patch"
7B0F 0A 75000000 6B00 0000map leaf, size 10: @75 → @6B

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

TRON464 B
Offset (hex)Bytes (hex)Meaning
00054 52 4F 4Emagic TRON
0046C 236C6F63616Ctxt "#local"
00B6C 3F7469746C65txt "?title"
0120F 0A 04000000 0B00 0000map leaf, size 10: @4 → @B
01C6C 236C6F63616Ctxt "#local"
0232C 3F64txt "?d"
0260F 0A 1C000000 2300 0000map leaf, size 10: @1C → @23
0300E 11 00 0300 020000 00 12000000 260000 00array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @12 → @26
0416C 236C6F63616Ctxt "#local"
04814 12 656D62656464 696E67732F6E6561 72657374txt "embeddings/nearest"
05C0F 0A 41000000 4800 0000map leaf, size 10: @41 → @48
0666C 236C6F63616Ctxt "#local"
06D9C 656D6265646469 6E67txt "embedding"
0770F 0A 66000000 6D00 0000map leaf, size 10: @66 → @6D
0816C 23636F6E7374txt "#const"
0885C 7175657279txt "query"
08E0F 0A 81000000 8800 0000map leaf, size 10: @81 → @88
0986C 236C6F63616Ctxt "#local"
09F2C 3F65txt "?e"
0A20F 0A 98000000 9F00 0000map leaf, size 10: @98 → @9F
0AC6C 236C6F63616Ctxt "#local"
0B32C 3F64txt "?d"
0B60F 0A AC000000 B300 0000map leaf, size 10: @AC → @B3
0C05C 746F702D6Btxt "top-k"
0C602 05000000000000 00i64 5
0CF0F 0A C0000000 C600 0000map leaf, size 10: @C0 → @C6
0D90E 21 00 3F00 060000 00 5C000000 770000 00 8E000000 A20000 00 B6000000 CF0000 00array leaf, size 33, shift 0, bitmap 003F, count 6 (root): @5C → @77 → @8E → @A2 → @B6 → @CF
0FA6C 236C6F63616Ctxt "#local"
1012C 3F65txt "?e"
1040F 0A FA000000 0101 0000map leaf, size 10: @FA → @101
10E6C 236C6F63616Ctxt "#local"
1155C 7469746C65txt "title"
11B0F 0A 0E010000 1501 0000map leaf, size 10: @10E → @115
1256C 236C6F63616Ctxt "#local"
12C6C 3F7469746C65txt "?title"
1330F 0A 25010000 2C01 0000map leaf, size 10: @125 → @12C
13D0E 15 00 0700 030000 00 04010000 1B0100 00 33010000array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @104 → @11B → @133
1520E 11 00 0300 020000 00 D9000000 3D0100 00array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @D9 → @13D
1636C 236C6F63616Ctxt "#local"
16A2C 3F64txt "?d"
16D0F 0A 63010000 6A01 0000map leaf, size 10: @163 → @16A
1770E 0D 00 0100 010000 00 6D010000array leaf, size 13, shift 0, bitmap 0001, count 1 (root): @16D
1845C 7768657265txt "where"
18A0F 0A 84010000 5201 0000map leaf, size 10: @184 → @152
1944C 66696E64txt "find"
1990F 0A 94010000 3000 0000map leaf, size 10: @194 → @30
1A38C 6F726465722D62 79txt "order-by"
1AC0F 0A A3010000 7701 0000map leaf, size 10: @1A3 → @177
1B607 12 0A800000 8A01 0000 99010000 AC01 0000map branch, size 18, depth 0 (root), slots 1, 3, 15: @18A → @199 → @1AC
PositionMemberAccepts
1embeddings/nearestThe clause name
2fieldA field that the settings index
3queryA #vf, a #vi or a list of numbers with dimensions items: a literal or an :input
4entityA ?variable
5distanceA ?variable
6optionsAn optional map
OptionAcceptsDefault
top-kThe maximum number of hits, an integer of 1 or more100
metricThe metric of the fieldThe metric of the field
filtersA map from a field to a value: each hit must have that valueNo filters

The distance is lower for a nearer entity:

MetricDistance
cosine1 minus the cosine of the angle between the vectors, from 0 to 2
squared-euclideanThe sum of the squared differences of the lanes
dot-productThe 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.