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:

json321 B
{
  "plugins": {
    "database": {
      "path": "app.db"
    },
    "embeddings": {
      "fields": {
        "embedding": {
          "dimensions": 384,
          "metric": {
            "#local": "cosine"
          },
          "normalization": {
            "#local": "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.

json171 B
{
  "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

json575 B
{
  "find": [
    {
      "#local": "?title"
    },
    {
      "#local": "?d"
    }
  ],
  "where": [
    [
      {
        "#local": "embeddings/nearest"
      },
      {
        "#local": "embedding"
      },
      {
        "#const": "query"
      },
      {
        "#local": "?e"
      },
      {
        "#local": "?d"
      },
      {
        "top-k": 5
      }
    ],
    [
      {
        "#local": "?e"
      },
      {
        "#local": "title"
      },
      {
        "#local": "?title"
      }
    ]
  ],
  "order-by": [
    {
      "#local": "?d"
    }
  ]
}
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.