Learn R Programming

chatAI4R (version 1.3.1)

geminiGrounding4R: Gemini API Google Search Grounding Request (v1/v1beta, 2025-07)

Description

A thin R wrapper for Google Gemini API with Google Search grounding enabled. Grounding improves factuality by incorporating web search results.

Usage

geminiGrounding4R(
  mode,
  contents,
  model = "gemini-2.5-flash",
  api_version = "v1beta",
  store_history = FALSE,
  dynamic_threshold = 0.7,
  api_key = Sys.getenv("GoogleGemini_API_KEY"),
  max_tokens = 2048,
  debug = FALSE,
  enable_grounding = TRUE,
  ...
)

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.5-flash"`.

api_version

API version for the Generative Language API (`"v1"` or `"v1beta"`). Default `"v1beta"`.

store_history

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

dynamic_threshold

Numeric [0,1] for dynamic retrieval threshold (default: 0.7). Only used for Gemini 1.5 models with `googleSearchRetrieval`; ignored for newer models that use `google_search`.

api_key

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

max_tokens

Maximum output tokens. Default 2048.

debug

Logical. If TRUE, prints request details for debugging.

enable_grounding

Logical. If TRUE, enables Google Search grounding (default).

...

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

Author

Satoshi Kume (revised 2025-07-01)

Examples

Run this code
if (FALSE) {
  # Synchronous text generation with grounding:
  result <- geminiGrounding4R(
    mode = "text",
    contents = "What is the current Google stock price?",
    store_history = FALSE,
    debug = TRUE,  # Enable debug to see request details
    api_key = Sys.getenv("GoogleGemini_API_KEY")
  )
  print(result)

  # Basic text generation without grounding (for troubleshooting):
  basic_result <- geminiGrounding4R(
    mode = "text",
    contents = "Hello, how are you?",
    enable_grounding = FALSE,
    debug = TRUE,
    api_key = Sys.getenv("GoogleGemini_API_KEY")
  )
  print(basic_result)

  # Chat mode with history storage:
  chat_history <- list(
    list(role = "user", text = "Hello"),
    list(role = "model", text = "Hi there! How can I help you?")
  )
  chat_result <- geminiGrounding4R(
    mode = "chat",
    contents = chat_history,
    store_history = TRUE,
    dynamic_threshold = 0.7,
    api_key = Sys.getenv("GoogleGemini_API_KEY")
  )
  print(chat_result)

  # Streaming text generation:
  stream_result <- geminiGrounding4R(
    mode = "stream_text",
    contents = "Tell me a story about a magic backpack.",
    store_history = FALSE,
    dynamic_threshold = 0.7,
    api_key = Sys.getenv("GoogleGemini_API_KEY")
  )
  print(stream_result$full_text)
}

Run the code above in your browser using DataLab