| 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 |
Build a fuzzy-join joint data.frame via LLM
build_joint(x, y, key1, key2, ...)build_joint(x, y, key1, key2, ...)
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() |
a 2-column data.frame mapping values from key1 to key2.
build_joint( x = data.frame(x = c("01","02","04")), y = data.frame(y = c("January","Feb","May")), key1 = "x", key2 = "y" )build_joint( x = data.frame(x = c("01","02","04")), y = data.frame(y = c("January","Feb","May")), key1 = "x", key2 = "y" )
This function sends a message to the LLM model and retrieves the result.
chat_llm( .message, .model = NULL, .temperature = 0, .max_tokens = 30000, .timeout = 300, .verbose = getOption("llmjoin.verbose", FALSE) )chat_llm( .message, .model = NULL, .temperature = 0, .max_tokens = 30000, .timeout = 300, .verbose = getOption("llmjoin.verbose", FALSE) )
.message |
the message to send: character, or any input coercible with
|
.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
|
.timeout |
Max seconds to communicate with LLM. |
.verbose |
logical, print progress messages. Default |
A character string with the LLM's response text.
chat_llm("tell a joke.")chat_llm("tell a joke.")
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.
get_llm(show_key = FALSE)get_llm(show_key = FALSE)
show_key |
logical, print and return the API key in full instead of a
masked form. Default |
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().
# 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)# 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 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.
joint_prompt(x, y)joint_prompt(x, y)
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 |
A character string containing the matching prompt.
joint_prompt( data.frame(x = c("01","02","04")), data.frame(y = c("January","Feb","May")) )joint_prompt( data.frame(x = c("01","02","04")), data.frame(y = c("January","Feb","May")) )
Fuzzy join with LLM
llm_join(x, y, key1, key2, ...)llm_join(x, y, key1, key2, ...)
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() |
the fuzzy-joined data.frame
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")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")
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.
parse_joint(llm_response, key1, key2, x_keys = NULL, y_keys = NULL)parse_joint(llm_response, key1, key2, x_keys = NULL, y_keys = NULL)
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. |
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.
a 2-column data.frame mapping values from key1 to key2.
parse_joint("01,January\n02,Feb\n04,May", key1 = "id", key2 = "month")parse_joint("01,January\n02,Feb\n04,May", key1 = "id", key2 = "month")
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.
set_llm(provider = "openai", url = NULL, key = NULL, model = NULL)set_llm(provider = "openai", url = NULL, key = NULL, model = NULL)
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
( |
model |
character, model name. If NULL, auto-set from provider default. |
NULL invisibly. Called for side effect of writing the config file.
# 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"))# 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
tbl2md(tbl, nm = NULL)tbl2md(tbl, nm = NULL)
tbl |
a data.frame object or a vector. |
nm |
character, only used if 'tbl' is a vector. |
markdown style table string lines
tbl2md(iris)tbl2md(iris)