ellmer

Call LLM APIs from R

https://github.com/tidyverse/ellmer

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    2 of 31 committers (6.5%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.8%) to scientific vocabulary

Keywords from Contributors

geo tidy-data shiny gdal devtools visualisation rmarkdown setup web-development web-app
Last synced: 10 months ago · JSON representation

Repository

Call LLM APIs from R

Basic Info
Statistics
  • Stars: 517
  • Watchers: 17
  • Forks: 96
  • Open Issues: 60
  • Releases: 7
Created almost 2 years ago · Last pushed 10 months ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
```

# ellmer ellmer website


[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![R-CMD-check](https://github.com/tidyverse/ellmer/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidyverse/ellmer/actions/workflows/R-CMD-check.yaml)


ellmer makes it easy to use large language models (LLM) from R. It supports a wide variety of LLM providers and implements a rich set of features including streaming outputs, tool/function calling, structured data extraction, and more.

ellmer is one of a number of LLM-related packages created by Posit:

* Looking for something similar in python? Check out [chatlas](https://github.com/posit-dev/chatlas)!
* Want to evaluate your LLMs? Try [vitals](https://vitals.tidyverse.org).
* Need RAG? Take a look at [ragnar](https://ragnar.tidyverse.org).
* Want to make a beautiful LLM powered chatbot? Consider [shinychat](https://posit-dev.github.io/shinychat/).
* Working with MCP? Check out [mcptools](https://posit-dev.github.io/mcptools/).

## Installation

You can install ellmer from CRAN with:

```{r}
#| eval: false
install.packages("ellmer")
```

## Providers

ellmer supports a wide variety of model providers:

* Anthropic's Claude: `chat_anthropic()`.
* AWS Bedrock: `chat_aws_bedrock()`.
* Azure OpenAI: `chat_azure_openai()`.
* Cloudflare: `chat_cloudflare()`.
* Databricks: `chat_databricks()`.
* DeepSeek: `chat_deepseek()`.
* GitHub model marketplace: `chat_github()`.
* Google Gemini/Vertex AI: `chat_google_gemini()`, `chat_google_vertex()`.
* Groq: `chat_groq()`.
* Hugging Face: `chat_huggingface()`.
* Mistral: `chat_mistral()`.
* Ollama: `chat_ollama()`.
* OpenAI: `chat_openai()`.
* OpenRouter: `chat_openrouter()`.
* perplexity.ai: `chat_perplexity()`.
* Snowflake Cortex: `chat_snowflake()` and `chat_cortex_analyst()`.
* VLLM: `chat_vllm()`.

### Provider/model choice

If you're using ellmer inside an organisation, you may have internal policies that limit you to models from big cloud providers, e.g. `chat_azure_openai()`, `chat_aws_bedrock()`, `chat_databricks()`, or `chat_snowflake()`.

If you're using ellmer for your own exploration, you'll have a lot more freedom, so we have a few recommendations to help you get started:

- `chat_openai()` or `chat_anthropic()` are good places to start. `chat_openai()` defaults to **GPT-4.1**, but you can use `model = "gpt-4-1-nano"` for a cheaper, faster model, or `model = "o3"` for more complex reasoning. `chat_anthropic()` is also good; it defaults to **Claude 4.0 Sonnet**, which we have found to be particularly good at writing R code.

- `chat_google_gemini()` is a strong model with generous free tier (with the downside that [your data is used](https://ai.google.dev/gemini-api/terms#unpaid-services) to improve the model), making it a great place to start if you don't want to spend any money.

- `chat_ollama()`, which uses [Ollama](https://ollama.com), allows you to run models on your own computer. While the biggest models you can run locally aren't as good as the state of the art hosted models, they don't share your data and are effectively free.

### Authentication

Authentication works a little differently depending on the provider. A few popular ones (including OpenAI and Anthropic) require you to obtain an API key. We recommend you save it in an environment variable rather than using it directly in your code, and if you deploy an app or report that uses ellmer to another system, you'll need to ensure that this environment variable is available there, too.

ellmer also automatically detects many of the OAuth or IAM-based credentials used by the big cloud providers (currently `chat_azure_openai()`, `chat_aws_bedrock()`, `chat_databricks()`, `chat_snowflake()`). That includes credentials for these platforms managed by [Posit Workbench](https://docs.posit.co/ide/server-pro/user/posit-workbench/managed-credentials/managed-credentials.html) and [Posit Connect](https://docs.posit.co/connect/user/oauth-integrations/#adding-oauth-integrations-to-deployed-content).

If you find cases where ellmer cannot detect credentials from one of these cloud providers, feel free to open an issue; we're happy to add more auth mechanisms if needed.

## Using ellmer

You can work with ellmer in several different ways, depending on whether you are working interactively or programmatically. They all start with creating a new chat object:

```{r}
library(ellmer)

chat <- chat_openai("Be terse", model = "gpt-4o-mini")
```

Chat objects are stateful [R6 objects](https://r6.r-lib.org): they retain the context of the conversation, so each new query builds on the previous ones. You call their methods with `$`.

### Interactive chat console

The most interactive and least programmatic way of using ellmer is to chat directly in your R console or browser with `live_console(chat)` or `live_browser()`:

```{r}
#| eval: false

live_console(chat)
#> ╔════════════════════════════════════════════════════════╗
#> ║  Entering chat console. Use """ for multi-line input.  ║
#> ║  Press Ctrl+C to quit.                                 ║
#> ╚════════════════════════════════════════════════════════╝
#> >>> Who were the original creators of R?
#> R was originally created by Ross Ihaka and Robert Gentleman at the University of
#> Auckland, New Zealand.
#>
#> >>> When was that?
#> R was initially released in 1995. Development began a few years prior to that,
#> in the early 1990s.
```

Keep in mind that the chat object retains state, so when you enter the chat console, any previous interactions with that chat object are still part of the conversation, and any interactions you have in the chat console will persist after you exit back to the R prompt. This is true regardless of which chat function you use.

### Interactive method call

The second most interactive way to chat is to call the `chat()` method:

```{r}
chat$chat("What preceding languages most influenced R?")
```

If you initialize the chat object in the global environment, the `chat` method will stream the response to the console. When the entire response is received, it's also (invisibly) returned as a character vector. This is useful when you want to see the response as it arrives, but you don't want to enter the chat console.

If you want to ask a question about an image, you can pass one or more additional input arguments using `content_image_file()` and/or `content_image_url()`:

```{r}
chat$chat(
  content_image_url("https://www.r-project.org/Rlogo.png"),
  "Can you explain this logo?"
)
```

### Streaming vs capturing

In most circumstances, ellmer will stream the output to the console. You can take control of this by setting the `echo` argument either when creating the chat object or when calling `$chat()`. Set `echo = "none"` to return a string instead:

```{r}
my_function <- function() {
  chat <- chat_openai("Be terse", model = "gpt-4o-mini", echo = "none")
  chat$chat("What is 6 times 7?")
}
str(my_function())
```

If needed, you can manually control this behaviour with the `echo` argument. This is useful for programming with ellmer when the result is either not intended for human consumption or when you want to process the response before displaying it.

## Learning more

ellmer comes with a bunch of vignettes to help you learn more:

* Learn key vocabulary and see example use cases in `vignette("ellmer")`.
* Learn how to design your prompt in `vignette("prompt-design")`.
* Learn about tool/function calling in `vignette("tool-calling")`.
* Learn how to extract structured data in `vignette("structured-data")`.
* Learn about streaming and async APIs in `vignette("streaming-async")`.

Owner

  • Name: tidyverse
  • Login: tidyverse
  • Kind: organization

The tidyverse is a collection of R packages that share common principles and are designed to work together seamlessly

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 374
  • Total Committers: 31
  • Avg Commits per committer: 12.065
  • Development Distribution Score (DDS): 0.369
Past Year
  • Commits: 374
  • Committers: 31
  • Avg Commits per committer: 12.065
  • Development Distribution Score (DDS): 0.369
Top Committers
Name Email Commits
Hadley Wickham h****m@g****m 236
Joe Cheng j****e@p****o 37
Garrick Aden-Buie g****k@a****m 33
Aaron Jacobs a****l 28
lindbrook l****k 9
Charlie Gao 5****o 3
Michael Grund 2****d 2
Simon P. Couch s****h@g****m 2
Simon Spavound s****d@l****k 2
Adi Sarid a****i@s****l 1
Anatoliy Sokolov a****v@d****m 1
Brandon Greenwell g****n@g****m 1
Carson Sievert c****1@g****m 1
Davis Vaughan d****s@p****o 1
Gavin Simpson u****s@g****m 1
Hannah Frick h****k 1
Hernando Cortina h****h@a****u 1
Howard Baik 5****k 1
Jacob Sowder 5****r 1
James Wade g****b@j****m 1
Julio Trecenti j****i@g****m 1
Kyle Walker k****a@g****m 1
Maciej Banaś 7****s 1
Mauro Lepore m****e@g****m 1
Pedro Z p****z@g****m 1
Ryan Hafen r****n@g****m 1
Ryan Johnson 1****9 1
Stephanie Hazlitt s****t@g****m 1
Tomasz Kalinowski k****t@g****m 1
billsanto t****b@y****m 1
and 1 more...
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 217
  • Total pull requests: 407
  • Average time to close issues: 15 days
  • Average time to close pull requests: 5 days
  • Total issue authors: 98
  • Total pull request authors: 33
  • Average comments per issue: 1.28
  • Average comments per pull request: 0.8
  • Merged pull requests: 307
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 217
  • Pull requests: 407
  • Average time to close issues: 15 days
  • Average time to close pull requests: 5 days
  • Issue authors: 98
  • Pull request authors: 33
  • Average comments per issue: 1.28
  • Average comments per pull request: 0.8
  • Merged pull requests: 307
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • hadley (61)
  • gadenbuie (16)
  • atheriel (10)
  • simonpcouch (7)
  • moodymudskipper (6)
  • arunrajes (6)
  • vorpalvorpal (3)
  • arnavchauhan7 (3)
  • kbenoit (3)
  • CorradoLanera (3)
  • schmidb (2)
  • mark-andrews (2)
  • bzzzwa (2)
  • Sade154 (2)
  • smach (2)
Pull Request Authors
  • hadley (199)
  • gadenbuie (76)
  • atheriel (61)
  • lindbrook (9)
  • billsanto (6)
  • howardbaik (5)
  • s-spavound (4)
  • netique (3)
  • michaelgrund (3)
  • jcheng5 (2)
  • maurolepore (2)
  • simonpcouch (2)
  • t-kalinowski (2)
  • calderonsamuel (2)
  • kbenoit (2)
Top Labels
Issue Labels
bug (6) documentation (5) upkeep (4) feature (3)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • cran 6,067 last-month
  • Total dependent packages: 0
    (may contain duplicates)
  • Total dependent repositories: 0
    (may contain duplicates)
  • Total versions: 12
  • Total maintainers: 1
proxy.golang.org: github.com/tidyverse/ellmer
  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.4%
Average: 5.6%
Dependent repos count: 5.8%
Last synced: 11 months ago
cran.r-project.org: ellmer

Chat with Large Language Models

  • Versions: 7
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 6,067 Last month
Rankings
Dependent packages count: 27.5%
Dependent repos count: 33.8%
Average: 49.4%
Downloads: 87.0%
Maintainers (1)
Last synced: 10 months ago

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action v4.5.0 composite
  • actions/checkout v4 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pr-commands.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/pr-fetch v2 composite
  • r-lib/actions/pr-push v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml actions
  • actions/checkout v4 composite
  • actions/upload-artifact v4 composite
  • codecov/codecov-action v4 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION cran
  • R6 * imports
  • cli * imports
  • httr2 >= 1.0.3.9000 imports
  • jsonlite * imports
  • rlang * imports
  • testthat >= 3.0.0 suggests