skeg_client/vector

Vector index commands.

let assert Ok(Nil) = vector.create(conn, vector.index("docs", 768))
let assert Ok(Nil) =
  vector.set_with_payload(conn, "docs", 1, embedding, <<"lang=it">>)
let assert Ok(hits) =
  vector.search(
    conn,
    "docs",
    vector.query(question, 10) |> vector.filter("lang = it"),
  )

Vectors are lists of floats sent as little-endian float32, so every component must fit in a float32. Ids are unsigned 64-bit integers. Arguments outside those ranges are rejected with InvalidArgument before anything is sent.

Types

pub type Backend {
  Flat
  Disk
  UnknownBackend(String)
}

Constructors

  • Flat

    Exhaustive in-memory search.

  • Disk

    Graph index stored on disk.

  • UnknownBackend(String)

A search result. payload is None unless the query asked for payloads and the row has one.

pub type Hit {
  Hit(id: Int, score: Float, payload: option.Option(BitArray))
}

Constructors

pub type IndexInfo {
  IndexInfo(
    dim: Int,
    kind: Kind,
    backend: Backend,
    persistence: Persistence,
    search_list: Int,
    degree: Int,
    count: Int,
    bytes_disk: option.Option(Int),
    bytes_ram: Int,
    created_at: option.Option(Int),
    incarnation: option.Option(String),
    dirty: Bool,
    raw: resp_codec.Value,
  )
}

Constructors

  • IndexInfo(
      dim: Int,
      kind: Kind,
      backend: Backend,
      persistence: Persistence,
      search_list: Int,
      degree: Int,
      count: Int,
      bytes_disk: option.Option(Int),
      bytes_ram: Int,
      created_at: option.Option(Int),
      incarnation: option.Option(String),
      dirty: Bool,
      raw: resp_codec.Value,
    )

    Arguments

    search_list

    Default search list length of a disk index.

    degree

    Graph degree of a disk index.

    count

    Live vectors.

    bytes_disk

    Bytes on disk, when the server can measure them.

    created_at

    Creation time in Unix milliseconds, when recorded.

    incarnation

    Identifies this creation of the index: it changes when the index is dropped and created again under the same name.

    dirty

    Whether recent writes are not yet folded into the base index.

    raw

    The reply as received, including fields this client does not read.

The settings of an index to create. Build it with index.

pub opaque type IndexSpec
pub type IndexSummary {
  IndexSummary(
    name: String,
    dim: Int,
    kind: Kind,
    backend: Backend,
    vectors: Int,
    shards_resident: Int,
    shards_total: Int,
  )
}

Constructors

  • IndexSummary(
      name: String,
      dim: Int,
      kind: Kind,
      backend: Backend,
      vectors: Int,
      shards_resident: Int,
      shards_total: Int,
    )

    Arguments

    shards_resident

    Counts cover only the resident shards. When fewer than shards_total are resident, they are a partial reading.

One row for set_many. payload: None stores no payload.

pub type Item {
  Item(
    id: Int,
    vector: List(Float),
    payload: option.Option(BitArray),
  )
}

Constructors

  • Item(
      id: Int,
      vector: List(Float),
      payload: option.Option(BitArray),
    )

How an index stores its vectors. UnknownKind holds a name this client does not know, as reported by a newer server.

pub type Kind {
  F32
  Int8
  Binary
  Tq1
  Tq2
  Tq4
  UnknownKind(String)
}

Constructors

  • F32
  • Int8
  • Binary
  • Tq1
  • Tq2
  • Tq4
  • UnknownKind(String)
pub type Persistence {
  Ephemeral
  Durable
  UnknownPersistence(String)
}

Constructors

  • Ephemeral

    Removed when the server restarts.

  • Durable

    Survives restarts.

  • UnknownPersistence(String)

A search request. Build it with query.

pub opaque type Query

Values

pub fn backend_from_string(name: String) -> Backend
pub fn create(
  connection: connection.Connection,
  spec: IndexSpec,
) -> Result(Nil, error.Error)

Creates an index. Every setting is sent explicitly, so the result does not depend on server defaults.

pub fn delete(
  connection: connection.Connection,
  name: String,
  id: Int,
) -> Result(Bool, error.Error)

Deletes the row and reports whether a live row was removed.

pub fn drop(
  connection: connection.Connection,
  name: String,
) -> Result(Nil, error.Error)

Removes the index and every vector in it.

pub fn filter(query: Query, expression: String) -> Query

Keeps only rows whose payload matches expression, such as lang = it AND year >= 2020.

pub fn get(
  connection: connection.Connection,
  name: String,
  id: Int,
) -> Result(option.Option(List(Float)), error.Error)

The stored vector, or None when the row does not exist.

pub fn if_not_exists(spec: IndexSpec) -> IndexSpec

Succeeds without changes when an index with the same settings exists. An existing index with other settings is still an error.

pub fn in_memory(spec: IndexSpec) -> IndexSpec

Makes the index a flat in-memory index, removed when the server restarts.

pub fn index(name: String, dim: Int) -> IndexSpec

A durable disk index of dim dimensions, kind Tq2.

pub fn info(
  connection: connection.Connection,
  name: String,
) -> Result(IndexInfo, error.Error)
pub fn kind(spec: IndexSpec, kind: Kind) -> IndexSpec
pub fn kind_from_string(name: String) -> Kind
pub fn kind_to_string(kind: Kind) -> String
pub fn list(
  connection: connection.Connection,
) -> Result(List(IndexSummary), error.Error)

The indexes visible to this connection’s tenant.

pub const max_batch_items: Int

Most items set_many sends in one request. The server refuses a larger batch as a whole.

pub const max_payload_bytes: Int

Largest payload stored with one vector.

pub fn persistence_from_string(name: String) -> Persistence
pub fn query(vector: List(Float), k: Int) -> Query

The k nearest rows to vector, with the index’s default search list.

pub fn search(
  connection: connection.Connection,
  name: String,
  query: Query,
) -> Result(List(Hit), error.Error)

Runs query against the index. Hits come in the server’s order, best first. The server reduces k above its max_k limit.

pub fn search_list(query: Query, length: Int) -> Query

Search list length for this query: larger is slower and more accurate. 0 uses the index default; the server reduces values above its max_l limit.

pub fn set(
  connection: connection.Connection,
  name: String,
  id: Int,
  vector: List(Float),
) -> Result(Nil, error.Error)

Stores vector under id, replacing the row and dropping any payload it had.

pub fn set_many(
  connection: connection.Connection,
  name: String,
  items: List(Item),
) -> Result(List(Result(Nil, error.Error)), error.Error)

Stores many rows in one request. The result has one entry per item, in order: Ok(Nil) for a stored row, Error(ServerError(..)) for a row the server refused. A refusal of the whole request is the outer Error; it does not prove that no row was stored.

Items must respect max_batch_items and max_payload_bytes; the server also refuses a batch whose vectors exceed 64 MiB in total. An empty payload cannot be sent in a batch, because the server stores it as no payload: use Item(.., payload: None), or set_with_payload for an empty payload.

pub fn set_with_payload(
  connection: connection.Connection,
  name: String,
  id: Int,
  vector: List(Float),
  payload: BitArray,
) -> Result(Nil, error.Error)

Stores vector and payload under id, replacing the row. A payload of key=value fields can be used in search filters.

pub fn with_payload(query: Query) -> Query

Returns each hit’s payload.

pub fn with_search_list(
  spec: IndexSpec,
  length: Int,
) -> IndexSpec

Sets the default search list length of a disk index.

✨ Search Document