# ellmer 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 install.packages("ellmer") ``` ## Providers ellmer supports a wide variety of model providers. Official providers are actively maintained, with priority support for bug fixes and new features. Community providers are contributed and maintained by the community; contributions to improve them are especially welcome. ### Official providers - Anthropic’s Claude: [`chat_anthropic()`](https://ellmer.tidyverse.org/reference/chat_anthropic.md). - AWS Bedrock: [`chat_aws_bedrock()`](https://ellmer.tidyverse.org/reference/chat_aws_bedrock.md). - Azure OpenAI: [`chat_azure_openai()`](https://ellmer.tidyverse.org/reference/chat_azure_openai.md). - Databricks: [`chat_databricks()`](https://ellmer.tidyverse.org/reference/chat_databricks.md). - DeepSeek: [`chat_deepseek()`](https://ellmer.tidyverse.org/reference/chat_deepseek.md). - GitHub model marketplace: [`chat_github()`](https://ellmer.tidyverse.org/reference/chat_github.md). - Google Gemini/Vertex AI: [`chat_google_gemini()`](https://ellmer.tidyverse.org/reference/chat_google_gemini.md), [`chat_google_vertex()`](https://ellmer.tidyverse.org/reference/chat_google_gemini.md). - Ollama: [`chat_ollama()`](https://ellmer.tidyverse.org/reference/chat_ollama.md). - OpenAI: [`chat_openai()`](https://ellmer.tidyverse.org/reference/chat_openai.md). - Posit AI: [`chat_posit()`](https://ellmer.tidyverse.org/reference/chat_posit.md). - Snowflake Cortex: [`chat_snowflake()`](https://ellmer.tidyverse.org/reference/chat_snowflake.md) and `chat_cortex_analyst()`. ### Community providers - Cloudflare: [`chat_cloudflare()`](https://ellmer.tidyverse.org/reference/chat_cloudflare.md). - Groq: [`chat_groq()`](https://ellmer.tidyverse.org/reference/chat_groq.md). - Hugging Face: [`chat_huggingface()`](https://ellmer.tidyverse.org/reference/chat_huggingface.md). - LM Studio: [`chat_lmstudio()`](https://ellmer.tidyverse.org/reference/chat_lmstudio.md). - Mistral: [`chat_mistral()`](https://ellmer.tidyverse.org/reference/chat_mistral.md). - OpenRouter: [`chat_openrouter()`](https://ellmer.tidyverse.org/reference/chat_openrouter.md). - perplexity.ai: [`chat_perplexity()`](https://ellmer.tidyverse.org/reference/chat_perplexity.md). - Portkey: [`chat_portkey()`](https://ellmer.tidyverse.org/reference/chat_portkey.md). - VLLM: [`chat_vllm()`](https://ellmer.tidyverse.org/reference/chat_vllm.md). ### 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()`](https://ellmer.tidyverse.org/reference/chat_azure_openai.md), [`chat_aws_bedrock()`](https://ellmer.tidyverse.org/reference/chat_aws_bedrock.md), [`chat_databricks()`](https://ellmer.tidyverse.org/reference/chat_databricks.md), or [`chat_snowflake()`](https://ellmer.tidyverse.org/reference/chat_snowflake.md). 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()`](https://ellmer.tidyverse.org/reference/chat_openai.md) or [`chat_anthropic()`](https://ellmer.tidyverse.org/reference/chat_anthropic.md) are good places to start. [`chat_openai()`](https://ellmer.tidyverse.org/reference/chat_openai.md) defaults to **GPT-5.4**, but you can use `model = "gpt-5.4-nano"` for a cheaper, faster model. [`chat_anthropic()`](https://ellmer.tidyverse.org/reference/chat_anthropic.md) defaults to **Claude Sonnet 4.6**, which we have found to be particularly good at writing R code. - [`chat_google_gemini()`](https://ellmer.tidyverse.org/reference/chat_google_gemini.md) is a strong model with a 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()`](https://ellmer.tidyverse.org/reference/chat_ollama.md), 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()`](https://ellmer.tidyverse.org/reference/chat_azure_openai.md), [`chat_aws_bedrock()`](https://ellmer.tidyverse.org/reference/chat_aws_bedrock.md), [`chat_databricks()`](https://ellmer.tidyverse.org/reference/chat_databricks.md), [`chat_snowflake()`](https://ellmer.tidyverse.org/reference/chat_snowflake.md)). 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()`](https://ellmer.tidyverse.org/reference/live_console.md): ``` r 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()`](https://ellmer.tidyverse.org/reference/chat-any.md) method: ``` r chat$chat("What preceding languages most influenced R?") #> R was primarily influenced by: #> #> 1. **S** - The predecessor to R, which introduced many foundational concepts. #> 2. **Scheme** - A dialect of Lisp that influenced R's functional programming #> aspects. #> 3. **Fortran** - Influenced R's efficiency and mathematical capabilities. #> 4. **C** - Impacted R's performance and low-level programming features. #> #> These languages contributed to R's design and functionality in statistics and #> data analysis. ``` 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()`](https://ellmer.tidyverse.org/reference/content_image_url.md) and/or [`content_image_url()`](https://ellmer.tidyverse.org/reference/content_image_url.md): ``` r chat$chat( content_image_url("https://www.r-project.org/Rlogo.png"), "Can you explain this logo?" ) #> The logo features a stylized letter "R" within a circular shape. The design #> reflects the programming language R, which is widely used for statistical #> analysis and data visualization. The circular element suggests continuity and #> completeness, while the bold "R" emphasizes its identity. Overall, the logo #> conveys modernity and practicality, aligning with R's functionality in data #> science. ``` ### 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()) #> 'ellmer_output' chr "6 times 7 is 42." ``` 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")`](https://ellmer.tidyverse.org/articles/ellmer.md). - Learn how to design your prompt in [`vignette("prompt-design")`](https://ellmer.tidyverse.org/articles/prompt-design.md). - Learn about tool/function calling in [`vignette("tool-calling")`](https://ellmer.tidyverse.org/articles/tool-calling.md). - Learn how to extract structured data in [`vignette("structured-data")`](https://ellmer.tidyverse.org/articles/structured-data.md). - Learn about streaming and async APIs in [`vignette("streaming-async")`](https://ellmer.tidyverse.org/articles/streaming-async.md). # Package index ## Chatbots ellmer provides a simple interface to a wide range of LLM providers. Use the `chat_` functions to initialize a `Chat` object for a specific provider and model. Once created, use the methods of the `Chat` object to send messages, receive responses, manage tools and extract structured data. - [`chat()`](https://ellmer.tidyverse.org/reference/chat-any.md) : Chat with any provider - [`Chat`](https://ellmer.tidyverse.org/reference/Chat.md) : The Chat object - [`token_usage()`](https://ellmer.tidyverse.org/reference/token_usage.md) : Report on token usage in the current session ### Official providers Actively maintained, with priority support for bug fixes and new features. - [`chat_anthropic()`](https://ellmer.tidyverse.org/reference/chat_anthropic.md) [`chat_claude()`](https://ellmer.tidyverse.org/reference/chat_anthropic.md) [`models_claude()`](https://ellmer.tidyverse.org/reference/chat_anthropic.md) [`models_anthropic()`](https://ellmer.tidyverse.org/reference/chat_anthropic.md) : Chat with an Anthropic Claude model - [`chat_aws_bedrock()`](https://ellmer.tidyverse.org/reference/chat_aws_bedrock.md) [`models_aws_bedrock()`](https://ellmer.tidyverse.org/reference/chat_aws_bedrock.md) : Chat with an AWS bedrock model - [`chat_azure_openai()`](https://ellmer.tidyverse.org/reference/chat_azure_openai.md) : Chat with a model hosted on Azure OpenAI - [`chat_databricks()`](https://ellmer.tidyverse.org/reference/chat_databricks.md) : Chat with a model hosted on Databricks - [`chat_deepseek()`](https://ellmer.tidyverse.org/reference/chat_deepseek.md) [`models_deepseek()`](https://ellmer.tidyverse.org/reference/chat_deepseek.md) : Chat with a model hosted on DeepSeek - [`chat_github()`](https://ellmer.tidyverse.org/reference/chat_github.md) [`models_github()`](https://ellmer.tidyverse.org/reference/chat_github.md) : Chat with a model hosted on the GitHub model marketplace - [`chat_google_gemini()`](https://ellmer.tidyverse.org/reference/chat_google_gemini.md) [`chat_google_vertex()`](https://ellmer.tidyverse.org/reference/chat_google_gemini.md) [`models_google_gemini()`](https://ellmer.tidyverse.org/reference/chat_google_gemini.md) [`models_google_vertex()`](https://ellmer.tidyverse.org/reference/chat_google_gemini.md) : Chat with a Google Gemini or Vertex AI model - [`chat_ollama()`](https://ellmer.tidyverse.org/reference/chat_ollama.md) [`models_ollama()`](https://ellmer.tidyverse.org/reference/chat_ollama.md) : Chat with a local Ollama model - [`chat_openai()`](https://ellmer.tidyverse.org/reference/chat_openai.md) [`models_openai()`](https://ellmer.tidyverse.org/reference/chat_openai.md) : Chat with an OpenAI model - [`chat_posit()`](https://ellmer.tidyverse.org/reference/chat_posit.md) [`models_posit()`](https://ellmer.tidyverse.org/reference/chat_posit.md) : Chat with a model hosted by Posit AI - [`chat_snowflake()`](https://ellmer.tidyverse.org/reference/chat_snowflake.md) : Chat with a model hosted on Snowflake ### Community providers Community contributed and maintained. Contributions to improve these providers are especially welcome. - [`chat_cloudflare()`](https://ellmer.tidyverse.org/reference/chat_cloudflare.md) : Chat with a model hosted on CloudFlare - [`chat_groq()`](https://ellmer.tidyverse.org/reference/chat_groq.md) [`models_groq()`](https://ellmer.tidyverse.org/reference/chat_groq.md) : Chat with a model hosted on Groq - [`chat_huggingface()`](https://ellmer.tidyverse.org/reference/chat_huggingface.md) : Chat with a model hosted on Hugging Face Serverless Inference API - [`chat_lmstudio()`](https://ellmer.tidyverse.org/reference/chat_lmstudio.md) [`models_lmstudio()`](https://ellmer.tidyverse.org/reference/chat_lmstudio.md) : Chat with a local LM Studio model - [`chat_mistral()`](https://ellmer.tidyverse.org/reference/chat_mistral.md) [`models_mistral()`](https://ellmer.tidyverse.org/reference/chat_mistral.md) : Chat with a model hosted on Mistral's La Platforme - [`chat_openai_compatible()`](https://ellmer.tidyverse.org/reference/chat_openai_compatible.md) : Chat with an OpenAI-compatible model - [`chat_openrouter()`](https://ellmer.tidyverse.org/reference/chat_openrouter.md) : Chat with one of the many models hosted on OpenRouter - [`chat_perplexity()`](https://ellmer.tidyverse.org/reference/chat_perplexity.md) : Chat with a model hosted on perplexity.ai - [`chat_portkey()`](https://ellmer.tidyverse.org/reference/chat_portkey.md) [`models_portkey()`](https://ellmer.tidyverse.org/reference/chat_portkey.md) : Chat with a model hosted on PortkeyAI - [`chat_vllm()`](https://ellmer.tidyverse.org/reference/chat_vllm.md) [`models_vllm()`](https://ellmer.tidyverse.org/reference/chat_vllm.md) : Chat with a model hosted by vLLM ### Provider-specific helpers - [`google_upload()`](https://ellmer.tidyverse.org/reference/google_upload.md) **\[experimental\]** : Upload a file to gemini - [`claude_file_upload()`](https://ellmer.tidyverse.org/reference/claude_file_upload.md) [`claude_file_list()`](https://ellmer.tidyverse.org/reference/claude_file_upload.md) [`claude_file_get()`](https://ellmer.tidyverse.org/reference/claude_file_upload.md) [`claude_file_download()`](https://ellmer.tidyverse.org/reference/claude_file_upload.md) [`claude_file_delete()`](https://ellmer.tidyverse.org/reference/claude_file_upload.md) **\[experimental\]** : Upload, downloand, and manage files for Claude - [`claude_tool_web_search()`](https://ellmer.tidyverse.org/reference/claude_tool_web_search.md) : Claude web search tool - [`claude_tool_web_fetch()`](https://ellmer.tidyverse.org/reference/claude_tool_web_fetch.md) : Claude web fetch tool - [`google_tool_web_search()`](https://ellmer.tidyverse.org/reference/google_tool_web_search.md) : Google web search (grounding) tool - [`google_tool_web_fetch()`](https://ellmer.tidyverse.org/reference/google_tool_web_fetch.md) : Google URL fetch tool - [`openai_tool_web_search()`](https://ellmer.tidyverse.org/reference/openai_tool_web_search.md) : OpenAI web search tool ## Chat helpers - [`create_tool_def()`](https://ellmer.tidyverse.org/reference/create_tool_def.md) : Create metadata for a tool - [`content_image_url()`](https://ellmer.tidyverse.org/reference/content_image_url.md) [`content_image_file()`](https://ellmer.tidyverse.org/reference/content_image_url.md) [`content_image_plot()`](https://ellmer.tidyverse.org/reference/content_image_url.md) : Encode images for chat input - [`content_pdf_file()`](https://ellmer.tidyverse.org/reference/content_pdf_file.md) [`content_pdf_url()`](https://ellmer.tidyverse.org/reference/content_pdf_file.md) : Encode PDFs content for chat input - [`live_console()`](https://ellmer.tidyverse.org/reference/live_console.md) [`live_browser()`](https://ellmer.tidyverse.org/reference/live_console.md) : Open a live chat application - [`interpolate()`](https://ellmer.tidyverse.org/reference/interpolate.md) [`interpolate_file()`](https://ellmer.tidyverse.org/reference/interpolate.md) [`interpolate_package()`](https://ellmer.tidyverse.org/reference/interpolate.md) : Helpers for interpolating data into prompts - [`stream_controller()`](https://ellmer.tidyverse.org/reference/stream_controller.md) : Create a stream controller ## Parallel and batch chat - [`batch_chat()`](https://ellmer.tidyverse.org/reference/batch_chat.md) [`batch_chat_text()`](https://ellmer.tidyverse.org/reference/batch_chat.md) [`batch_chat_structured()`](https://ellmer.tidyverse.org/reference/batch_chat.md) [`batch_chat_completed()`](https://ellmer.tidyverse.org/reference/batch_chat.md) : Submit multiple chats in one batch - [`parallel_chat()`](https://ellmer.tidyverse.org/reference/parallel_chat.md) [`parallel_chat_text()`](https://ellmer.tidyverse.org/reference/parallel_chat.md) [`parallel_chat_structured()`](https://ellmer.tidyverse.org/reference/parallel_chat.md) : Submit multiple chats in parallel ## Tools and structured data - [`tool()`](https://ellmer.tidyverse.org/reference/tool.md) : Define a tool - [`tool_annotations()`](https://ellmer.tidyverse.org/reference/tool_annotations.md) : Tool annotations - [`tool_reject()`](https://ellmer.tidyverse.org/reference/tool_reject.md) : Reject a tool call - [`type_boolean()`](https://ellmer.tidyverse.org/reference/type_boolean.md) [`type_integer()`](https://ellmer.tidyverse.org/reference/type_boolean.md) [`type_number()`](https://ellmer.tidyverse.org/reference/type_boolean.md) [`type_string()`](https://ellmer.tidyverse.org/reference/type_boolean.md) [`type_enum()`](https://ellmer.tidyverse.org/reference/type_boolean.md) [`type_array()`](https://ellmer.tidyverse.org/reference/type_boolean.md) [`type_object()`](https://ellmer.tidyverse.org/reference/type_boolean.md) [`type_from_schema()`](https://ellmer.tidyverse.org/reference/type_boolean.md) [`type_ignore()`](https://ellmer.tidyverse.org/reference/type_boolean.md) : Type specifications ## Objects These classes abstract away behaviour differences in chat providers so that for typical ellmer use you don’t need to worry about them. You’ll need to learn more about the objects if you’re doing something that’s only supported by one provider, or if you’re implementing a new provider. - [`Provider()`](https://ellmer.tidyverse.org/reference/Provider.md) : A chatbot provider - [`Turn()`](https://ellmer.tidyverse.org/reference/Turn.md) [`UserTurn()`](https://ellmer.tidyverse.org/reference/Turn.md) [`SystemTurn()`](https://ellmer.tidyverse.org/reference/Turn.md) [`AssistantTurn()`](https://ellmer.tidyverse.org/reference/Turn.md) [`AssistantPartialTurn()`](https://ellmer.tidyverse.org/reference/Turn.md) : A user, assistant, or system turn - [`Content()`](https://ellmer.tidyverse.org/reference/Content.md) [`ContentText()`](https://ellmer.tidyverse.org/reference/Content.md) [`ContentImage()`](https://ellmer.tidyverse.org/reference/Content.md) [`ContentImageRemote()`](https://ellmer.tidyverse.org/reference/Content.md) [`ContentImageInline()`](https://ellmer.tidyverse.org/reference/Content.md) [`ContentToolRequest()`](https://ellmer.tidyverse.org/reference/Content.md) [`ContentToolResult()`](https://ellmer.tidyverse.org/reference/Content.md) [`ContentThinking()`](https://ellmer.tidyverse.org/reference/Content.md) [`ContentPDF()`](https://ellmer.tidyverse.org/reference/Content.md) : Content types received from and sent to a chatbot - [`TypeBasic()`](https://ellmer.tidyverse.org/reference/Type.md) [`TypeEnum()`](https://ellmer.tidyverse.org/reference/Type.md) [`TypeArray()`](https://ellmer.tidyverse.org/reference/Type.md) [`TypeJsonSchema()`](https://ellmer.tidyverse.org/reference/Type.md) [`TypeIgnore()`](https://ellmer.tidyverse.org/reference/Type.md) [`TypeObject()`](https://ellmer.tidyverse.org/reference/Type.md) : Type definitions for function calling and structured data extraction. ## Utilities - [`contents_text()`](https://ellmer.tidyverse.org/reference/contents_text.md) [`contents_html()`](https://ellmer.tidyverse.org/reference/contents_text.md) [`contents_markdown()`](https://ellmer.tidyverse.org/reference/contents_text.md) **\[experimental\]** : Format contents into a textual representation - [`contents_record()`](https://ellmer.tidyverse.org/reference/contents_record.md) [`contents_replay()`](https://ellmer.tidyverse.org/reference/contents_record.md) : Record and replay content - [`df_schema()`](https://ellmer.tidyverse.org/reference/df_schema.md) : Describe the schema of a data frame, suitable for sending to an LLM - [`params()`](https://ellmer.tidyverse.org/reference/params.md) : Standard model parameters # Articles ### All vignettes - [Getting started with ellmer](https://ellmer.tidyverse.org/articles/ellmer.md): - [Programming with ellmer](https://ellmer.tidyverse.org/articles/programming.md): - [Prompt design](https://ellmer.tidyverse.org/articles/prompt-design.md): - [Streaming and async APIs](https://ellmer.tidyverse.org/articles/streaming-async.md): - [Structured data](https://ellmer.tidyverse.org/articles/structured-data.md): - [Tool/function calling](https://ellmer.tidyverse.org/articles/tool-calling.md):