R/embed.R

Defines functions embed_matrix json_field is_json_object is_one_number embed_request lms_embed

Documented in lms_embed

#' Turn Text into Embedding Vectors
#'
#' Sends one or more texts to an embedding model and returns the vector that
#' the model produced for each one. The texts go out in batches of at most
#' `batch_size`, one request per batch, in the order given. A reply that
#' names another model can add one model-list request, as the "Reply from
#' another model" section describes.
#'
#' @param model Character. The loaded embedding model name. Must be one name,
#'   given as a single string. The string must be valid in its declared
#'   encoding and not marked `"bytes"`. A class, names, and the S4 bit are
#'   removed before the name is sent.
#' @param input Character. The texts to embed. A vector of length `n` returns
#'   `n` embeddings, in the order given. Must hold at least one value and no
#'   missing values.
#' @param host Character. Server URL.
#' @param simplify `TRUE` or `FALSE`. If `TRUE`, the default, returns a
#'   numeric matrix with one row per input. `FALSE` returns a list with one
#'   element per request: the parsed response body, unchanged, or the
#'   condition of a request that failed. Any other value, `NULL` and `NA`
#'   included, aborts before the check for a running server.
#' @param ... Additional fields for the request body. LM Studio ignores a
#'   field it does not recognize, and two OpenAI fields are worth naming for
#'   that reason: LM Studio ignores `dimensions`, so asking for a narrower
#'   vector has no effect. `encoding_format = "base64"` is untested against LM
#'   Studio: a server that honors it returns embeddings this function cannot
#'   read, and the default `simplify = TRUE` path then aborts.
#' @param ttl A whole number of seconds from 1 to `.Machine$integer.max`, or
#'   `NULL` to leave it out. It is how long the model stays loaded with no
#'   request. It has an effect only on a model that this request loads. The
#'   server loads a model that is not loaded yet when its just-in-time loading
#'   setting is on. A model that is already loaded keeps its idle time.
#' @param token Character or `NULL`. An API token for a server that requires
#'   authentication. `NULL` reads the `rlmstudio.token` option and then the
#'   `RLMSTUDIO_API_TOKEN` environment variable. See [rlmstudio_token].
#' @param batch_size A whole number from 1 to `.Machine$integer.max`. The
#'   most texts that one request carries. The default is 100. A value at
#'   least as large as `length(input)` sends every text in one request.
#' @param quiet `TRUE`, `FALSE`, or `NULL`, the default. `NULL` follows the
#'   `rlmstudio.quiet` option. `TRUE` starts no progress bar, and `FALSE` starts
#'   one, also when the option is `TRUE`. cli draws a started bar only after a
#'   delay, two seconds by default. A bar starts only when the
#'   call sends more than one request. Any other value, `NA` included, aborts before the
#'   check for a running server. `quiet` does not suppress the warning about
#'   failed inputs.
#' @return If `simplify = FALSE`, a list with one element per request, in
#'   request order. Each element is the parsed JSON body of that request, or
#'   the condition of a request that failed. A call with one request returns
#'   a list of one. Otherwise, a double matrix with one row per input text and
#'   one column per embedding dimension. The row at position `i` holds the
#'   embedding that the response reported for the input at position `i`, or
#'   `NA` for an input whose request failed. The matrix carries no row or
#'   column names.
#' @details
#' Before each request after the first, the function checks again that the
#' server is running.
#'
#' A request that fails with an `rlmstudio_bad_response` other than an
#' `rlmstudio_model_mismatch`, or with an `rlmstudio_api_error` that does not
#' end the call as described below, fails the inputs it carried alone. Their
#' rows hold `NA`, or their element of the
#' `simplify = FALSE` list holds the condition without its backtrace. The call
#' goes on to the next request and then gives one warning that names the
#' count and the positions of the failed inputs. That warning shows even with
#' `quiet = TRUE`. If every request fails, the call aborts with the condition
#' of the first failed request, and no warning is given.
#'
#' Four faults end the call at once, and no request goes out after them: a
#' server that the check before a request, or the model-list lookup of the
#' reply check, finds gone; an `rlmstudio_api_error` with `status` 401, 403,
#' or 404; an `rlmstudio_api_error` with `status` 400 whose `server_message`
#' starts with "No models loaded", which LM Studio sent for an unknown name
#' with no model loaded; and an `rlmstudio_model_mismatch`, a reply from a
#' model other than the one asked for. The "API failure" and "Reply from
#' another model" sections below describe the last two. A fault that ends the
#' call gives no warning about the inputs that failed before it. With
#' `simplify = TRUE`, a fifth fault does the same: a request whose embeddings
#' have another number of dimensions than those of an earlier request. It
#' aborts with `rlmstudio_bad_response`. With `simplify = FALSE`, the bodies
#' do not go through the matrix checks, so bodies of two widths are returned
#' with no abort. Each abort
#' after a request that succeeded carries a `results` field. With
#' `simplify = TRUE`, it is the matrix so far, with `NA` in each row whose
#' embedding did not arrive. With `simplify = FALSE`, it is the list so far,
#' with `NULL` in the element of the request that ended the call and in every
#' element after it. An abort before any
#' request succeeded carries no `results` field. An error of any other class
#' aborts the call unchanged.
#'
#' On LM Studio 0.4.25+1, the server embeds only the first tokens of each
#' text, up to the context length of the loaded model instance. For a text
#' longer than that, it still returns a vector, with no error or warning, and
#' the vector covers only the start of the text. The `usage` field of the
#' reply reported 0 tokens on that version, so the count does not show the
#' cut. The `loaded_instances` column of `list_models(detailed = TRUE)` gives
#' the `context_length` of each loaded instance. To embed all of a long text,
#' split it into pieces before the call. Or unload the model with
#' [lms_unload()] and load it again with a larger `context_length` through
#' [lms_load()], up to the `max_context_length` column of
#' `list_models(detailed = TRUE)`. For a model that is already loaded,
#' [lms_load()] does not load it again.
#' @inheritSection rlmstudio-conditions Server not running
#' @inheritSection rlmstudio-conditions API failure
#' @inheritSection rlmstudio-conditions Malformed response
#' @inheritSection rlmstudio-conditions Malformed embeddings
#' @inheritSection rlmstudio-conditions Reply from another model
#' @export
#' @examples
#' \dontrun{
#' vectors <- lms_embed(
#'   model = "text-embedding-nomic-embed-text-v1.5",
#'   input = c("the first document", "the second document")
#' )
#' dim(vectors)
#' }
lms_embed <- function(
  model,
  input,
  host = "http://localhost:1234",
  simplify = TRUE,
  ...,
  ttl = NULL,
  token = NULL,
  batch_size = 100,
  quiet = NULL
) {
  model <- rlm_check_id(model, "model")
  rlm_check_text(input, "input")
  rlm_check_ttl(ttl)
  rlm_check_batch_size(batch_size)
  rlm_check_flag(simplify, "simplify")
  rlm_check_flag(quiet, "quiet", null_ok = TRUE)

  stop_if_no_server(host)

  # An R integer is written as a JSON integer whatever the serializer does
  # with a double. The check has already made the value whole and in range.
  base <- list(model = model)
  if (!is.null(ttl)) {
    base$ttl <- as.integer(ttl)
  }
  dots <- list(...)
  client <- lms_client(host, token = token)
  has_token <- request_sends_token(client)

  # Consecutive runs of at most `batch_size` inputs, in input order. The size
  # can be as large as `.Machine$integer.max`, where integer sums overflow, so
  # the ends are computed as doubles and cut back to the input length.
  n <- length(input)
  starts <- seq(1, n, by = batch_size)
  batches <- lapply(starts, function(s) seq.int(s, min(s + batch_size - 1, n)))

  show_bar <- length(batches) > 1L && !is_quiet(quiet)
  if (show_bar) {
    pb <- cli::cli_progress_bar(
      name = "Embedding",
      total = n,
      format = "{cli::pb_name} {cli::pb_bar} {cli::pb_percent} | ETA: {cli::pb_eta}"
    )
    on.exit(cli::cli_progress_done(id = pb), add = TRUE)
  }

  # With `simplify = TRUE` the rows go into one matrix, made once the first
  # request succeeds, because only a reply says how wide a vector is. A failed
  # request leaves its rows NA. With `simplify = FALSE` each request's parsed
  # body takes one slot of a list.
  out <- NULL
  bodies <- vector("list", length(batches))
  any_ok <- FALSE
  failed <- integer()
  first_failure <- NULL

  # A lost server or a fault that holds for every request ends the call, as
  # in `lms_chat_batch()` (D-011, D-019). Once a request has succeeded, the
  # condition carries what the call has so far, so a long run does not lose
  # it. Before that no vectors have arrived, and the condition is unchanged.
  abort_with_results <- function(cnd) {
    if (any_ok) {
      cnd$results <- if (isTRUE(simplify)) out else bodies
    }
    stop(cnd)
  }
  # A failed request loses its own rows, not the whole call. The backtrace is
  # dropped from the kept copy, because it makes each slot large and says
  # nothing about the inputs.
  keep_failure <- function(cnd) {
    if (is.null(first_failure)) {
      first_failure <<- cnd
    }
    cnd$trace <- NULL
    cnd
  }
  # A refused token or a model the server cannot find fails every request the
  # same way, whatever the texts, so these statuses end the call (D-019). So
  # does a server with no model loaded (D-046).
  keep_or_abort_api <- function(cnd) {
    if (
      isTRUE(cnd$status %in% c(401L, 403L, 404L)) || is_no_models_loaded(cnd)
    ) {
      abort_with_results(cnd)
    }
    keep_failure(cnd)
  }
  # A reply from another model means that the wrong model answers every
  # request, so it ends the call (D-025, D-046). The test sits in this handler
  # and not in a handler of its own, because `tryCatch()` runs a handler
  # inside the handlers named after it (LESSONS, M049).
  keep_or_abort_bad <- function(cnd) {
    if (inherits(cnd, "rlmstudio_model_mismatch")) {
      abort_with_results(cnd)
    }
    keep_failure(cnd)
  }
  # A lookup that keeps a reply id keeps it for the rest of the call, so a
  # later reply with that id sends no lookup (D-046).
  old_cache <- reply_model_cache$kept
  on.exit(end_reply_model_cache(old_cache), add = TRUE)
  start_reply_model_cache()

  for (b in seq_along(batches)) {
    rows <- batches[[b]]
    # The probe before the first request ran above. Without this one, a
    # server that stops between two requests makes the next request abort
    # with an `httr2_failure` that carries no `results`.
    if (b > 1L) {
      tryCatch(
        stop_if_no_server(host),
        rlmstudio_no_server = abort_with_results
      )
    }

    # as.list() is what keeps a single input an array of one rather than a
    # bare string: jsonlite serializes an unnamed list as a JSON array
    # whatever its length, while auto-unboxing would turn a length-one
    # character vector into a scalar. unname() is what keeps the list
    # unnamed: as.list() carries the vector's names over, and jsonlite writes
    # a named list as a JSON object. `setNames(df$text, df$id)` is an
    # ordinary way to reach this function.
    body <- c(base, list(input = as.list(unname(input[rows]))))
    body <- utils::modifyList(body, dots)

    # The lookup of the reply-model check probes the server, so a server
    # that stops after the reply ends the call with its results.
    res <- tryCatch(
      embed_request(client, body, has_token, simplify, host, token),
      rlmstudio_no_server = abort_with_results,
      rlmstudio_api_error = keep_or_abort_api,
      rlmstudio_bad_response = keep_or_abort_bad
    )

    if (inherits(res, c("rlmstudio_api_error", "rlmstudio_bad_response"))) {
      failed <- c(failed, rows)
      if (!isTRUE(simplify)) {
        bodies[b] <- list(res)
      }
    } else if (isTRUE(simplify)) {
      if (is.null(out)) {
        out <- matrix(NA_real_, nrow = n, ncol = ncol(res$value))
      } else if (ncol(res$value) != ncol(out)) {
        # Vectors of two widths cannot share a matrix, and no rule says which
        # width is right, so the call ends rather than guess.
        width_fault <- tryCatch(
          rlm_abort_bad_response(
            res$resp,
            "Embeddings Failed",
            cli::format_inline(
              if (length(rows) == 1L) {
                "the embedding of input {rows} has {ncol(res$value)} dimension{?s}, and earlier ones have {ncol(out)}."
              } else {
                "the embeddings of inputs {min(rows)} to {max(rows)} have {ncol(res$value)} dimension{?s}, and earlier ones have {ncol(out)}."
              }
            )
          ),
          rlmstudio_bad_response = identity
        )
        abort_with_results(width_fault)
      }
      out[rows, ] <- res$value
      any_ok <- TRUE
    } else {
      bodies[b] <- list(res$value)
      any_ok <- TRUE
    }

    if (show_bar) {
      cli::cli_progress_update(id = pb, inc = length(rows))
    }
  }

  if (!any_ok) {
    stop(first_failure)
  }

  if (length(failed) > 0L) {
    # Shown whatever `quiet` says, because it is the only signal that some
    # vectors are missing (D-010, D-011). The positions are joined here,
    # because cli shortens a vector of more than 20 values and would drop
    # some of them.
    positions <- cli::ansi_collapse(failed, trunc = Inf)
    detail <- if (isTRUE(simplify)) {
      "The rows of those inputs hold {.code NA}. Use {.code simplify = FALSE} to keep the conditions."
    } else {
      "The element of each request that carried them holds the {.cls rlmstudio_api_error} or {.cls rlmstudio_bad_response} condition."
    }
    cli::cli_warn(c(
      "{length(failed)} input{?s} failed, at {cli::qty(length(failed))}position{?s} {positions}.",
      "i" = detail
    ))
  }

  if (isTRUE(simplify)) out else bodies
}

#' Send one embeddings request and read its reply
#'
#' @param client The httr2 request that `lms_client()` built.
#' @param body List. The request body, whose `input` is one batch.
#' @param has_token Logical. Whether a token was sent, for the abort hint.
#' @param simplify Logical. Whether to build the matrix from the reply.
#' @param host,token The host and token of the call, for the model-list
#'   lookup of `check_reply_model()`.
#' @return A list of `value`, the matrix or the parsed body, and `resp`.
#'
#' @noRd
embed_request <- function(client, body, has_token, simplify, host, token) {
  resp <- client |>
    httr2::req_url_path("v1/embeddings") |>
    rlm_req_body(body) |>
    httr2::req_error(is_error = \(resp) FALSE) |>
    httr2::req_perform()

  if (httr2::resp_status(resp) != 200) {
    rlm_abort_api(resp, "Embeddings Failed", has_token)
  }

  resp_data <- parse_ok_body(resp, "Embeddings Failed")
  # With one embedding model loaded, LM Studio answered an unknown name with
  # status 200 and vectors from that model (D-046), as the chat routes do
  # (D-025).
  check_reply_model(
    resp,
    resp_data,
    body[["model"]],
    host,
    token,
    "Embeddings Failed"
  )

  if (!isTRUE(simplify)) {
    return(list(value = resp_data, resp = resp))
  }

  # The count comes from the body that was actually sent rather than from the
  # batch. R rejects a call that names `input` twice, so today the two are
  # always the same length; reading the body keeps them the same if a later
  # change ever puts the inputs together some other way.
  list(value = embed_matrix(resp_data, length(body[["input"]]), resp), resp = resp)
}

#' Is this value one number?
#'
#' `parse_ok_body()` parses with `simplifyVector = FALSE` (LESSONS, M005), so
#' every number in the body arrives as a length-one numeric and a
#' JSON `null` arrives as `NULL`. A logical passes `is.numeric()` nowhere, and
#' a base64 string fails on the type.
#'
#' @param x Any value read out of a parsed response body.
#' @return `TRUE` when `x` is one number that is not `NA`.
#'
#' @noRd
is_one_number <- function(x) {
  is.numeric(x) && length(x) == 1L && !is.na(x)
}

#' Did this value parse from a JSON object?
#'
#' A JSON object parses to a named list, a JSON array to an unnamed one, and
#' anything else to an atomic value. The distinction matters because `[[` on
#' an atomic value is an error rather than a missing field.
#'
#' @param x Any value read out of a parsed response body.
#' @return `TRUE` when `x` parsed from a JSON object.
#'
#' @noRd
is_json_object <- function(x) {
  is.list(x) && !is.null(names(x))
}

#' Read one named field out of a parsed JSON object
#'
#' `$` on a list partial-matches, so `body$data` reads a `database` field when
#' no `data` field exists, and `$` on an atomic value is an error rather than
#' a missing field. This reads the exact name and returns `NULL` for anything
#' the name does not reach.
#'
#' @param x Any value read out of a parsed response body.
#' @param name Character. The exact field name.
#' @return The field, or `NULL`.
#'
#' @noRd
json_field <- function(x, name) {
  if (is_json_object(x) && name %in% names(x)) x[[name]] else NULL
}

#' Check the data block of an embeddings response and build the matrix
#'
#' Reads the `data` block of a parsed `/v1/embeddings` body and returns a
#' double matrix with one row per input. Every condition below aborts with
#' class `rlmstudio_bad_response` rather than building a matrix, because a
#' block whose indexes are missing, repeated, or out of range would otherwise
#' pair a vector with the wrong text and corrupt a whole batch without a
#' message.
#'
#' The rows are placed by the `index` field of each element rather than by
#' arrival order, so a server that answers out of order still pairs each
#' vector with its own text.
#'
#' @param resp_data List. The parsed response body.
#' @param n Integer. How many texts the request asked the server to embed.
#' @param resp The httr2 response, which carries the status the condition
#'   reports.
#' @return A double matrix of `n` rows and one column per embedding dimension.
#'
#' @noRd
embed_matrix <- function(resp_data, n, resp) {
  # Resolve the detail here rather than in the abort helper. cli interpolates
  # the braces of the string it is handed, and not the braces of a value
  # spliced into it, so a detail built in this frame has to be formatted in
  # this frame or it reaches the user with its braces intact.
  fail <- function(detail) {
    rlm_abort_bad_response(
      resp,
      "Embeddings Failed",
      cli::format_inline(detail, .envir = parent.frame())
    )
  }

  if (!is.list(resp_data)) {
    fail("the response body is not a JSON object.")
  }

  data <- json_field(resp_data, "data")
  # Three faults, not one. `json_field()` gives NULL for a name the body does
  # not carry, so that is the only shape that means the block is absent. A
  # block that is there but holds a plain value has to say so, or the user
  # goes looking for a field that is already in front of them. And a JSON
  # array parses to an unnamed list where a JSON object parses to a named one:
  # `length()` on the object form would count its members as though they were
  # array elements, so the names are what tell those two apart.
  if (is.null(data)) {
    fail("the response carries no {.field data} block.")
  }
  if (!is.list(data)) {
    fail("the {.field data} block is a plain value rather than an array.")
  }
  if (!is.null(names(data))) {
    fail("the {.field data} block is a JSON object rather than an array.")
  }
  if (length(data) != n) {
    fail(
      "the response carries {length(data)} embedding{?s} for {n} input{?s}."
    )
  }
  if (!all(vapply(data, is_json_object, logical(1)))) {
    fail("an element of the {.field data} block is not a JSON object.")
  }

  indexes <- vapply(
    data,
    function(el) {
      index <- json_field(el, "index")
      if (is_one_number(index)) as.numeric(index) else NA_real_
    },
    numeric(1)
  )
  if (anyNA(indexes)) {
    fail("an element of the {.field data} block carries no usable index.")
  }
  if (any(indexes != round(indexes))) {
    fail("an index in the {.field data} block is not a whole number.")
  }
  if (anyDuplicated(indexes) > 0) {
    fail("the {.field data} block repeats an index.")
  }
  if (any(indexes < 0) || any(indexes > n - 1)) {
    fail("an index in the {.field data} block falls outside 0 to {n - 1}.")
  }

  vectors <- lapply(data, function(el) {
    embedding <- json_field(el, "embedding")
    if (!is.list(embedding) || !is.null(names(embedding))) {
      return(NULL)
    }
    if (!all(vapply(embedding, is_one_number, logical(1)))) {
      return(NULL)
    }
    as.double(unlist(embedding, use.names = FALSE))
  })

  if (any(vapply(vectors, is.null, logical(1)))) {
    fail("an element of the {.field data} block carries no list of numbers.")
  }
  # An empty JSON array is a list of numbers, of none of them. It needs its
  # own clause, and its own guard: `matrix(ncol = 0)` would otherwise build a
  # matrix of no columns rather than abort.
  if (any(lengths(vectors) == 0L)) {
    fail("an embedding in the {.field data} block is empty.")
  }

  widths <- unique(lengths(vectors))
  if (length(widths) > 1L) {
    fail("the embeddings in the {.field data} block are of unequal length.")
  }

  # `matrix()` returns NULL dimnames and `unlist(use.names = FALSE)` strips
  # the names off every value placed into it, so the matrix is unnamed by
  # construction and needs no `dimnames(out) <- NULL` line to make it so.
  out <- matrix(0, nrow = n, ncol = widths)
  for (i in seq_len(n)) {
    out[indexes[[i]] + 1L, ] <- vectors[[i]]
  }
  out
}

Try the rlmstudio package in your browser

Any scripts or data that you put into this service are public.

rlmstudio documentation built on Dec. 1, 2026, 9:07 a.m.