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:
{plugins {database {path "app.db"} embeddings {fields {embedding {dimensions 384 metric cosine normalization unit}}}}}
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.
{patch {_apple {title "Apple" embedding #vf [0.12 -0.03 0.88]}}}
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
{find [?title ?d]
where [[embeddings/nearest embedding :query ?e ?d {top-k 5}]
[?e title ?title]]
order-by [?d]}
| 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.