Translates text to a specific language

R/llm-translate.R

llm_translate

Description

Use a Large Language Model (LLM) to translate a text to a specific language

Usage

 
llm_translate( 
  .data, 
  col, 
  language, 
  pred_name = ".translation", 
  additional_prompt = "" 
) 
 
llm_vec_translate(x, language, additional_prompt = "", preview = FALSE) 

Arguments

Arguments Description
.data A data.frame or tbl object that contains the text to be analyzed
col The name of the field to analyze, supports tidy-eval
language Target language to translate the text to
pred_name A character vector with the name of the new column where the prediction will be placed
additional_prompt Inserts this text into the prompt sent to the LLM
x A vector that contains the text to be analyzed
preview It returns the R call that would have been used to run the prediction. It only returns the first record in x. Defaults to FALSE Applies to vector function only.

Value

llm_translate returns a data.frame or tbl object. llm_vec_translate returns a vector that is the same length as x.

Examples

 
library(mall) 
 
data("reviews") 
 
llm_use("ollama", "llama3.2", seed = 100, .silent = TRUE) 
 
# Pass the desired language to translate to 
llm_translate(reviews, review, "spanish") 
#> # A tibble: 3 × 2
#>   review                                        .translation                    
#>   <chr>                                         <chr>                           
#> 1 This has been the best TV I've ever used. Gr… Esta ha sido la mejor televisió…
#> 2 I regret buying this laptop. It is too slow … Me arrepiento de comprar este p…
#> 3 Not sure how to feel about my new washing ma… No estoy seguro de cómo me sien…