foundry_similarity: Compute Cosine Similarity Between Embeddings

View source: R/embed.R

foundry_similarityR Documentation

Compute Cosine Similarity Between Embeddings

Description

Compute pairwise cosine similarity between all embeddings in a tibble. Useful for finding semantically similar texts.

Usage

foundry_similarity(data, text_col = "text", top_k = NULL, as_matrix = FALSE)

Arguments

data

A tibble from foundry_embed() containing an embedding list-column.

text_col

Character. Name of the column containing text labels. Default: "text".

top_k

Integer. Optional maximum number of most-similar pairs to return.

as_matrix

Logical. If TRUE, return the full cosine-similarity matrix instead of a long pairwise tibble.

Value

A tibble with columns:

text_1

Character. First text.

text_2

Character. Second text.

similarity

Numeric. Cosine similarity between -1 and 1.

Examples

# Toy vectors demonstrate local computation without calling Azure.
embeddings <- tibble::tibble(
  text = c("Vector A", "Vector B", "Vector C"),
  embedding = list(c(1, 0), c(1, 1), c(0, 1))
)
foundry_similarity(embeddings)
foundry_similarity(embeddings, top_k = 1)
foundry_similarity(embeddings, as_matrix = TRUE)

foundryR documentation built on Sept. 25, 2026, 1:10 a.m.