Learn R Programming

chatAI4R (version 1.3.1)

gemini4R: Gemini API Request (v1beta, 2025-07)

Description

A thin, dependency-light R wrapper for Google Gemini API (`models.generateContent` / `models.streamGenerateContent`).

Usage

gemini4R(
  mode,
  contents,
  model = "gemini-2.0-flash",
  store_history = FALSE,
  api_key = Sys.getenv("GoogleGemini_API_KEY"),
  max_tokens = 2048,
  ...
)

Value

For non-stream modes, a parsed list. For stream modes, a list with `full_text` and `chunks`.

Arguments

mode

One of `"text"`, `"stream_text"`, `"chat"`, `"stream_chat"`.

contents

Character vector (single-turn) or list of message objects (chat modes). See Examples.

model

Gemini model ID. Default `"gemini-2.0-flash"`.

store_history

Logical. If TRUE, chat history is persisted to the `chat_history` env-var (JSON).

api_key

Your Google Gemini API key (default: `Sys.getenv("GEMINI_API_KEY")`).

max_tokens

Maximum output tokens. NULL for server default.

...

Additional `httr::POST` options (timeouts etc.).

Author

Satoshi Kume (revised 2025-07-01)

Examples

Run this code
if (FALSE) {
gemini4R("text",
         contents = "Explain how AI works.",
         max_tokens = 256)
}

Run the code above in your browser using DataLab