Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: lang
Title: Translates R Help Documentation using Large Language Models
Version: 0.1.1
Version: 0.1.1.9000
Authors@R: c(
person("Edgar", "Ruiz", , "edgar@posit.co", role = c("aut", "cre")),
person("Posit Software, PBC", role = c("cph", "fnd"),
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# lang 0.1.1.9000

## Bug fixes

* When the input is 10 words or fewer, the context summary is now omitted from the translation prompt. Local LLMs can get confused by a context summary that is much longer than the field being translated, causing them to paraphrase the context instead of translating the input.

# lang 0.1.1

## New features
Expand Down
8 changes: 8 additions & 0 deletions R/lang-help.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
#' will be processed, so the help returned will be the original package's
#' documentation.
#'
#' To improve translation consistency, `lang` generates a short summary of the
#' full help page and passes it as context with each individual translation
#' request. This helps the LLM maintain consistent terminology across sections.
#' You can control the length of this summary with the `context_size` argument,
#' or set it to `0` to disable context-aware translation entirely. To avoid the
#' LLM getting confused when the context is much longer than the field being
#' translated, context is automatically omitted for fields of 10 words or fewer.
#'
#' @param topic A character string specifying the help topic to translate.
#' @param package The R package to look for the topic, if not provided the
#' function will attempt to find the topic based on the loaded packages.
Expand Down
10 changes: 9 additions & 1 deletion R/rd-translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ rd_translate <- function(rd_content, lang, context_size) {
}

rd_field_translate <- function(x, lang, rs, context_summary = NULL) {
context_block <- if (!is.null(context_summary)) {
word_count <- length(unlist(strsplit(paste(x, collapse = " "), " ")))
use_context <- !is.null(context_summary) && word_count > 10L
context_block <- if (use_context) {
paste0(
"For context, here is a short summary of the full help page in the target language:\n\n",
context_summary,
Expand Down Expand Up @@ -288,6 +290,12 @@ lang_rs_hash <- function() {
}

lang_rs_get <- function() {
if (is.null(.lang_env$session[["backend"]])) {
cli_abort(
"No LLM backend configured. Call {.fn lang_use} first.",
call = NULL
)
}
rs <- .lang_env$rs
if (!is.null(rs) && rs$is_alive()) {
if (rs$get_state() == "starting") {
Expand Down
36 changes: 26 additions & 10 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,24 @@ Note that R enforces the printed names of each section, so titles such as
"Description", "Usage", and "Arguments" will always remain untranslated.

During translation, `lang` will display its progress by showing which section
of the documentation is currently translating. Because each section of a help
page is translated independently, the LLM can lose track of the broader topic
and produce inconsistent or out-of-context translations. To address this,
`lang` first summarizes the full help page in English, translates that summary
into the target language, and then uses it as context when translating each
individual section. You can control the length of this summary with the
`context_size` argument in `lang_use()` or `lang_help()` — set it to `0` to
disable it, or increase it to give the LLM more context. During the R session,
if you request the same R function's help more than one time then `lang` will
use its cached results, which will run immediately.
of the documentation is currently translating. During the R session, if you
request the same R function's help more than one time then `lang` will use its
cached results, which will run immediately.

### Context summary

Because each section of a help page is translated independently, the LLM can
lose track of the broader topic and produce inconsistent or out-of-context
translations. To address this, `lang` first summarizes the full help page in
English, translates that summary into the target language, and then uses it as
context when translating each individual section. You can control the length of
this summary with the `context_size` argument in `lang_use()` or `lang_help()`
— set it to `0` to disable it, or increase it to give the LLM more context.

To avoid the LLM getting confused by a context summary that is longer than the
content being translated, context is automatically omitted for fields of 10
words or fewer.



### LLM connections
Expand Down Expand Up @@ -228,6 +236,14 @@ use the full language name, such as 'spanish', or 'french', etc. You can use
`Sys.setenv(LANGUAGE = "[my language]")`, or, for a more permanent solution,
add the entry to your .Renviron file (`usethis::edit_r_environ()`).

### Translation errors

If you experience unexpected translation errors and you are using a local LLM
without a `seed` set, try restarting your R session and running the translation
again. Non-deterministic LLM output can occasionally produce output that causes
errors. If the problem persists, please open an issue at
<https://github.com/mlverse/lang/issues>.

### Interaction with `mall`

`lang` uses the `mall` package to produce the translations. To avoid conflicts
Expand Down
Loading
Loading