hf_nearest_neighbors: Find Nearest Neighbors by Semantic Similarity

View source: R/tidytext.R

hf_nearest_neighborsR Documentation

Find Nearest Neighbors by Semantic Similarity

Description

Find the k most similar texts to a query text based on embedding similarity.

Usage

hf_nearest_neighbors(
  data,
  query,
  k = 5,
  text_col = "text",
  model = hf_default_model("embed"),
  token = NULL
)

Arguments

data

A data frame with an 'embedding' column (from hf_embed_text).

query

Character string. The query text to compare against.

k

Integer. Number of nearest neighbors to return. Default: 5.

text_col

Character string. Name of text column. Default: "text".

model

Character string. Model to use for query embedding. Should match the model used for data embeddings.

token

Character string or NULL. API token for authentication.

Value

A tibble with the k nearest neighbors, sorted by similarity (descending).

Examples

## Not run: 
docs_embedded |>
  hf_nearest_neighbors("machine learning", k = 5)

## End(Not run)

huggingfaceR documentation built on Aug. 30, 2026, 1:06 a.m.