MacMusic  |  PcMusic  |  440 Software  |  440 Forums  |  440TV  |  Zicos
chat
Recherche

3 of the best LLM integration tools for R

jeudi 13 mars 2025, 09:30 , par InfoWorld
When we first looked at this space in late 2023, many generative AI R packages focused largely on OpenAI and ChatGPT-like functionality or coding add-ins. Today, the landscape includes packages that natively support more LLM suppliers—including models run locally on your own computer. The range of genAI tasks you can do in R has also broadened.

Here’s an updated look at one of the more intriguing categories in the landscape of generative AI packages for R: adding large language model capabilities to your own scripts and apps. My next article will cover tools for getting help with your R programming and running LLMs locally.

ellmer

ellmer wasn’t one of the earliest entries in this space, but as the main Tidyverse package for using LLMs in an R workflow, it’s among the most important. ellmer has the resources of Posit (formerly RStudio) behind it, and its co-authors—Posit Chief Scientist Hadley Wickham, (creator of ggplot2 and dplyr) and CTO Joe Cheng, original author of the Shiny R web framework—are well known for their work on other popular R packages.

Kyle Walker, author of several R and Python packages including tidycensus, posted on Bluesky: “{ellmer} is the best #LLM interface in any language, in my opinion.” (Walker recently contributed code to ellmer for processing PDF files.)

Getting started with ellmer

ellmer supports a range of features, use cases, and platforms, and it’s also well-documented. You can install the ellmer package from CRAN the usual way or try the development version with pak::pak('tidyverse/ellmer').

To use ellmer, you start by creating a chat object with functions such as chat_openai() or chat_claude(). Then you’ll use that object to interact with the LLM.

Check the chat function help file to see how to store your API key (unless you’re using a local model). For example, chat_openai() expects an environment variable, OPENAI_API_KEY, which you can store in your.Renviron file. A call like chat_anthropic() will look for ANTHROPIC_API_KEY.

ellmer supports around a dozen LLM platforms including OpenAI and Azure OpenAI, Anthropic, Google Gemini, AWS Bedrock, Snowflake Cortex, Perplexity, and Ollama for local models. Here’s an example of creating a basic OpenAI chat object:

library(ellmer)
my_chat

And here’s the syntax for using Claude:

my_chat_claude

System prompts aren’t required, but I like to include them if I’ve got a topic-specific task. Not all chat functions require setting the model, either, but it’s a good idea to specify one so you’re not unpleasantly surprised by either an old, outdated model or a cutting-edge pricey one.

Several R experts said in early 2025 that Anthropic’s Claude Sonnet 3.5 seemed to be best specifically for writing R code, and early reports say Sonnet 3.7 is even better in general at code. However, with the new batch of models released by OpenAI, Google, and others, it’s worth keeping an eye on how that unfolds. This is a good reason for coding in a way that it’s relatively easy to swap models and providers. Separating the chat object from other code should do the trick.

I’ve found OpenAI’s new o3-mini model to be quite good at writing R code. It’s less expensive than Sonnet 3.7, but as a “reasoning” model that always thinks step by step, it may not respond as quickly as conventional models do.

Sonnet is somewhat pricier than GPT-4o, but unless you’re feeding it massive amounts of code, the bill should still be pretty small (my queries typically run two cents each).

If you want to set other parameters for the model, including temperature (meaning the amount of randomness acceptable in the response), use api_arg. That takes a named list like this one:

my_chat

A higher temperature is often viewed as more creative, whereas a lower one is considered more precise. Also note that temperature isn’t an explicit argument in ellmer’s chat functions, so you need to remember to set it in the api_args named list. (Updating this setting is a good idea since temperature can be an important parameter when working with LLMs.)

Check the model provider’s API documentation for a list of available argument names and valid value ranges for temperature and other settings.

Interactivity

ellmer offers three levels of interactivity for using its chat objects: a chatbot interface, either in the R console or browser only; streaming and saving the results as text; or only storing query results without streaming.

live_console(my_chat) opens my_chat in a chat interface in the R console. live_browser(my_chat) creates a basic chatbot interface in a browser. In both cases, responses are streamed.

ellmer’s “interactive method call” uses a syntax like my_chat$chat() to both display streamed responses and save the result as text:

my_results

If you use my_chat again to ask another question, you’ll find the chat retained your previous question-and-answer history, so you can refer to your previous questions and the responses. For example, the following will refer back to the initial question:

my_results2
chat(openai(.model = 'gpt-4o-mini',.temperature = 0,.stream = FALSE))
print(my_conversation2)

It’s possible to extract some metadata from the results with

result_metadata

which returns a tibble (a special data frame) with columns for model, timestamp, prompt_tokens, completion_tokens, total_tokens (which oddly was the same as completion_tokens when I tested in some cases), and api_specific lists with more token details. You can also extract the user message that generated the reply with get_user_message(my_conversation2).

llm_message() also supports sending images to models that support such uploads with the.imagefile argument. And, you can ask questions about PDF files with the.pdf argument (make sure you also have the pdftools R package installed). For example, I downloaded the tidyllm PDF reference manual from CRAN to a file named tidyllm.pdf in a files subdirectory, and then asked a question using tidyllm with R:

my_conversation3
chat(openai(.model = 'gpt-4o',.temperature =.1,.stream = FALSE))

print(my_conversation3)
Message History: system: You are a helpful assistant ————————————————————– user: What are 10 interesting tasks I can perform with the tidyllm package? -> Attached Media Files: tidyllm.pdf ————————————————————– assistant: The tidyllm package provides a tidy interface for integrating large language model (LLM) APIs into R workflows. Here are 10 interesting tasks you can perform with the tidyllm package:

Chat with Language Models: Use the chat() function to send messages to various language models like OpenAI, Claude, or Mistral, and receive responses. This can be useful for generating text, answering questions, or creating conversational agents.

Generate Text Embeddings: Use the embed() function to generate embeddings for text data. This can be useful for tasks like semantic search, clustering, or as input features for machine learning models.

Batch Processing: Use functions like send_batch(), fetch_batch(), and check_batch() to send multiple requests in a batch to an LLM API, retrieve results, and check the status of batch processing. This is useful for processing large datasets efficiently.

Multimodal Interactions: Create LLMMessage objects that include text and media (such as images or PDFs) for multimodal processing. This can be useful for tasks that require understanding both text and visual content.

Interactive Message History: Maintain a conversation history using LLMMessage objects, allowing for context-aware interactions with language models. This is useful for building chatbots or virtual assistants that need to remember previous interactions.

Custom JSON Schemas: Define custom JSON schemas using tidyllm_schema() to enforce structured outputs from language models. This is useful for ensuring that the responses from the models adhere to a specific format.

Transcribe Audio Files: Use the groq_transcribe() function to transcribe audio files using the Groq transcription API. This can be useful for converting spoken content into text.

File Management with Gemini API: Use functions like gemini_upload_file(), gemini_list_files(), and gemini_delete_file() to manage files in the Gemini API. This is useful for tasks that involve uploading, listing, or deleting files as part of the workflow.

Rate Limit Information: Use rate_limit_info() to retrieve the current rate limit information for APIs. This is useful for managing API usage and avoiding hitting rate limits.

Stream Responses: Enable streaming of responses from language models using the.stream parameter in functions like chat() or ollama_chat(). This is useful for real-time applications where you want to display the response as it is being generated.

These tasks demonstrate the versatility of the tidyllm package in integrating LLMs into various data workflows and applications.

result_metadata3
answer_as_boolean() |>
send_prompt(llm_provider_groq(parameters = list(model = 'llama3-70b-8192') ))

which in this case returns FALSE. (Note that I had first stored my Groq API key in an R environment variable, as would be the case for any cloud LLM provider.) For a more detailed example, check out the Sentiment analysis in R with a LLM and ‘tidyprompt’ vignette on GitHub.

There are also more complex pipelines using functions such as llm_feedback() to check if an LLM response meets certain conditions and user_verify() to make it possible for a human to check an LLM response.

You can create your own tidyprompt prompt wraps with the prompt_wrap() function.

The tidyprompt package supports OpenAI, Google Gemini, Ollama, Groq, Grok, XAI, and OpenRouter (not Anthropic directly, but Claude models are available on OpenRouter). It was created by Luka Koning and Tjark Van de Merwe.

The bottom line

The generative AI ecosystem for R is not as robust as Python’s, and that’s unlikely to change. However, in the past year, there’s been a lot of progress in creating tools for key tasks programmers might want to do with LLMs in R. If R is your language of choice and you’re interested in working with large language models either locally or via APIs, it’s worth giving some of these options a try.
https://www.infoworld.com/article/3828268/top-llm-integration-tools-for-r-programming.html

Voir aussi

News copyright owned by their original publishers | Copyright © 2004 - 2025 Zicos / 440Network
Date Actuelle
ven. 14 mars - 21:05 CET