Create a reusable chat object for interactive conversations with Google Gemini.
The returned object provides $chat() for text responses, $stream() for
streaming, and $chat_structured() for structured data extraction.
Use gemini_chat() for interactive, multi-turn conversations with streaming.
Use gemini_generate() for single-turn, structured output (e.g., pipeline tasks).
Arguments
- sys_prompt
Character. System prompt to set model behavior. Default NULL.
- ml
Character. Model ID. Default from
ART_GEMINI_MODELenv var.- temp
Numeric. Temperature (0-2). Default 1.
- tools
List. Tools to register. Create with
ellmer::tool(). Default NULL.- echo
Character. Output mode: "none", "output", or "all". Default "none".
Value
ellmer Chat object with methods:
$chat(...): Send message(s), receive text response$chat_structured(prompt, type): Send message, receive structured data$stream(...): Stream response chunks (for real-time display)$register_tool(tool): Add a tool after creation
See also
gemini_generate()for low-level single-turn requestsgemini_continue()for manual conversation continuation
Examples
if (FALSE) { # \dontrun{
# Basic chat
chat <- gemini_chat()
chat$chat("Explain Impressionism in 2 sentences.")
# With system prompt
chat <- gemini_chat(sys_prompt = "You are an art historian.")
chat$chat("What is Baroque?")
# Multi-turn conversation
chat <- gemini_chat(sys_prompt = "You are an art expert.")
chat$chat("What is Baroque art?")
chat$chat("How does it differ from Renaissance?")
# Streaming for real-time UI
stream <- chat$stream("Tell me a story about an artist.")
coro::loop(for (chunk in stream) cat(chunk))
# Image input
chat$chat(ellmer::content_image_file("artwork.png"), "Describe this.")
# Structured output
schema <- ellmer::type_object(
style = ellmer::type_string("Art style"),
period = ellmer::type_string("Time period")
)
chat$chat_structured("Describe Impressionism", type = schema)
} # }
