Package 'llmjoin'

Title: LLM-Powered Fuzzy Join
Description: Resolves ambiguous links between data.frames using large language models (LLMs). Supports matching across spelling variations, translations, and differing levels of precision.
Authors: Yifan LIU [aut, cre]
Maintainer: Yifan LIU <[email protected]>
License: MIT + file LICENSE
Version: 0.3.2
Built: 2026-11-07 11:51:16 UTC
Source: https://github.com/evanliu3594/llmjoin

Help Index


Build a fuzzy-join joint data.frame via LLM

Description

Build a fuzzy-join joint data.frame via LLM

Usage

build_joint(x, y, key1, key2, ...)

Arguments

x

a data.frame to be joined on the lhs.

y

a data.frame to be joined on the rhs.

key1

string, name of the key column of data.frame x waiting for pairing. Must name an existing column in x.

key2

string, name of the key column of data.frame y waiting for pairing. Must name an existing column in y.

...

extra params passed to chat_llm()

Value

a 2-column data.frame mapping values from key1 to key2.

Examples

build_joint(
    x = data.frame(x = c("01","02","04")),
    y = data.frame(y = c("January","Feb","May")),
    key1 = "x", key2 = "y"
  )

Send message to LLM server

Description

This function sends a message to the LLM model and retrieves the result.

Usage

chat_llm(
  .message,
  .model = NULL,
  .temperature = 0,
  .max_tokens = 30000,
  .timeout = 300,
  .verbose = getOption("llmjoin.verbose", FALSE)
)

Arguments

.message

the message to send: character, or any input coercible with as.character. A length > 1 input is pasted into a single string separated by newlines, and NA elements are treated as blank lines. Missing, NULL, zero-length, or all-blank input (after trimming whitespace) raises an error.

.model

character, LLM model to use. By default NULL (uses config value).

.temperature

OpenAI style randomness control (0~1), by default 0. For OpenAI gpt-5+/o-series models the parameter is not sent (the official API rejects it); a non-zero value triggers a warning.

.max_tokens

Max tokens to spend. Sent as max_completion_tokens for OpenAI gpt-5+/o-series models, as max_tokens for every other model and provider.

.timeout

Max seconds to communicate with LLM.

.verbose

logical, print progress messages. Default getOption("llmjoin.verbose", FALSE).

Value

A character string with the LLM's response text.

Examples

chat_llm("tell a joke.")

Show the current LLM service configuration

Description

Reads the configuration written by set_llm() and reports it, so you do not have to open the YAML file yourself. The API key is masked by default; pass show_key = TRUE to print it in full. Requests made by chat_llm() always use the stored key as-is, whatever this function displays.

Usage

get_llm(show_key = FALSE)

Arguments

show_key

logical, print and return the API key in full instead of a masked form. Default FALSE.

Value

A named list invisibly, with elements provider, url, model, key (masked unless show_key = TRUE) and config_path. The same fields are reported through message().

Examples

# the key is read from the environment so a deliberate run cannot overwrite
# your real config with a placeholder credential
set_llm(provider = "openai", key = Sys.getenv("LLMJOIN_API_KEY"))
get_llm()
get_llm(show_key = TRUE)

Generate connector prompt

Description

Generate a prompt to guide the LLM in generating a joint for data frame joining, leveraging the two key columns from the tables to be connected. The prompt pins what parse_joint() can check: plain CSV lines (no markdown tables, tabs or fences), exactly one line per value of column 1, and every value copied character for character including its leading zeros – a model that normalises "01" to "1" otherwise has to be re-matched by recovery. As of 2025/04/10, DeepSeek R1 and gpt-4.1-mini showed the best result; other LLMs might fabricate non-existent data in the result.

Usage

joint_prompt(x, y)

Arguments

x

1-column data.frame or vector of characters, left hand side of the join

y

1-column data.frame or vector of characters, right hand side of the join

Value

A character string containing the matching prompt.

Examples

joint_prompt(
  data.frame(x = c("01","02","04")),
  data.frame(y = c("January","Feb","May"))
)

Fuzzy join with LLM

Description

Fuzzy join with LLM

Usage

llm_join(x, y, key1, key2, ...)

Arguments

x

a data.frame to be joined on the lhs.

y

a data.frame to be joined on the rhs.

key1

string, name of the key column of data.frame x waiting for pairing.

key2

string, name of the key column of data.frame y waiting for pairing.

...

extra params passed to chat_llm()

Value

the fuzzy-joined data.frame

Examples

x <- data.frame(id = c("01", "02", "04"), value = c(10, 20, 40))
  y <- data.frame(month = c("January", "Feb", "May"), amount = c(100, 200, 400))

  llm_join(x, y, key1 = "id", key2 = "month")

Parse LLM response into a fuzzy-join joint data.frame

Description

Strips markdown fences, extracts the longest consecutive block of comma-separated lines, drops lines that do not hold exactly two fields (naming them in a warning), ensures a header row matching 'key1,key2' is present, and parses the CSV into a 2-column data.frame. Exact duplicate rows are dropped with a message, and a 'key1' value carrying several mappings is reported because it multiplies the joined rows.

Usage

parse_joint(llm_response, key1, key2, x_keys = NULL, y_keys = NULL)

Arguments

llm_response

character, raw response from the LLM.

key1

string, name of the lhs key column.

key2

string, name of the rhs key column.

x_keys

character vector of unique key values from the left-hand-side data.frame. If provided, values in the parsed key1 column not found in this set are considered LLM fabrications and dropped with a warning. A value differing from exactly one original key only by case, padding whitespace or numeric form ("1" for "01") is recovered to that original and announced by message, since dropping it would lose a correct match; a value whose normalised form fits several originals is dropped as ambiguous. Abbreviation changes ("Feb" answered as "February") are not recoverable – the prompt asks the model to copy values exactly. The literal string "NA" is accepted when the key set contains actual NA values.

y_keys

character vector of unique key values from the right-hand-side data.frame. Handled exactly like 'x_keys', except that an empty cell (the prompt's way of saying "no reasonable match") is kept as 'NA' and never counts as a fabrication.

Details

When 'x_keys'/'y_keys' are provided, original keys that never appear in the parsed result raise an informational warning naming the column, the count and up to 5 sample values; rows are never dropped by this feedback.

Value

a 2-column data.frame mapping values from key1 to key2.

Examples

parse_joint("01,January\n02,Feb\n04,May", key1 = "id", key2 = "month")

Set up your LLM service

Description

Set up your LLM service with native support for OpenAI, Claude (Anthropic), Gemini (via OpenAI-compatible endpoint), and DeepSeek. For custom endpoints like Ollama, proxies, Kimi, and others, use provider = "openai" along with your custom URL to connect through the compatible API interface. All information is stored strictly locally in your system configuration and is never uploaded or shared.

Usage

set_llm(provider = "openai", url = NULL, key = NULL, model = NULL)

Arguments

provider

character, LLM provider. One of "openai", "claude", "gemini", "deepseek". Default "openai".

url

url to your LLM provider endpoint. If NULL, auto-set based on provider.

key

api-key of your service. Prefer passing it from the environment (Sys.getenv("LLMJOIN_API_KEY")) over a string literal if you want to keep it out of your .Rhistory; the package never prints the stored key back (see get_llm(), which masks it by default).

model

character, model name. If NULL, auto-set from provider default.

Value

NULL invisibly. Called for side effect of writing the config file.

Examples

# the key is read from the environment so a deliberate run cannot overwrite
# your real config with a placeholder credential
set_llm(provider = "openai", key = Sys.getenv("LLMJOIN_API_KEY"))

Convert a data frame to a markdown table

Description

Convert a data frame to a markdown table

Usage

tbl2md(tbl, nm = NULL)

Arguments

tbl

a data.frame object or a vector.

nm

character, only used if 'tbl' is a vector.

Value

markdown style table string lines

Examples

tbl2md(iris)