Learn R Programming

EnTraineR (version 1.0.0)

gemini_generate: Generate text with Google Gemini (Generative Language API) - robust w/ retries

Description

Minimal wrapper around the Generative Language API ':generateContent' endpoint for text prompts, with retries, exponential backoff, clearer errors, and optional output compilation (HTML/DOCX) with auto-open.

Usage

gemini_generate(
  prompt,
  model = "gemini-2.5-flash",
  api_key = Sys.getenv("GEMINI_API_KEY"),
  user_agent = NULL,
  base_url = "https://generativelanguage.googleapis.com/v1beta",
  temperature = NULL,
  top_p = NULL,
  top_k = NULL,
  max_output_tokens = NULL,
  stop_sequences = NULL,
  system_instruction = NULL,
  safety_settings = NULL,
  seed = NULL,
  timeout = 120,
  verbose = FALSE,
  max_tries = 5,
  backoff_base = 0.8,
  backoff_cap = 8,
  force_markdown = TRUE,
  compile_to = c("none", "html", "docx")
)

Value

If compile_to = "none": character scalar (raw text as returned by the API). If compile_to = "html": list(markdown = <string>, html_path = <path>), and opens the HTML. If compile_to = "docx": list(markdown = <string>, docx_path = <path>), and opens the DOCX.

Arguments

prompt

Character scalar. The user prompt (plain text).

model

Character scalar. Gemini model id (e.g., "gemini-2.5-flash", "gemini-2.5-pro"). You may also pass "models/..." and it will be normalized.

api_key

Character scalar. API key. Defaults to env var 'GEMINI_API_KEY'.

user_agent

Character scalar. If NULL, a dynamic value is used.

base_url

Character scalar. API base URL.

temperature

Optional numeric in [0, 2].

top_p

Optional numeric in (0, 1].

top_k

Optional integer >= 1.

max_output_tokens

Optional integer > 0.

stop_sequences

Optional character vector.

system_instruction

Optional character scalar.

safety_settings

Optional list passed as-is to the API.

seed

Optional integer seed.

timeout

Numeric seconds for request timeout (default 120).

verbose

Logical; if TRUE, prints URL/retries.

max_tries

Integer. Max attempts (default 5).

backoff_base

Numeric. Initial backoff seconds (default 0.8).

backoff_cap

Numeric. Max backoff seconds (default 8).

force_markdown

Logical. If TRUE, instructs the model to answer in Markdown.

compile_to

Character scalar. One of c("none","html","docx").