| Title: | R interface to Google Gemini API |
|---|---|
| Description: | In development |
| Authors: | Sebastian Kranz |
| Maintainer: | Sebastian Kranz <[email protected]> |
| License: | GPL>=2.0 |
| Version: | 0.1.0 |
| Built: | 2026-05-09 06:59:47 UTC |
| Source: | https://github.com/skranz/rgemini |
This function creates a template for an array response consisting of JSON objects. Each element (object) in the array is expected to have the provided fields.
arr_resp(...)arr_resp(...)
... |
Named elements that define the fields for each JSON object in the array. |
A list with class 'arr_resp' that serves as a template for an array response.
# Create an array response template with two fields. arr_template <- arr_resp(city = "Paris", country = "France") str(arr_template)# Create an array response template with two fields. arr_template <- arr_resp(city = "Paris", country = "France") str(arr_template)
Caches a Gemini context to the API.
gemini_cache_context( context, verbose = TRUE, api_key = getOption("gemini_api_key") )gemini_cache_context( context, verbose = TRUE, api_key = getOption("gemini_api_key") )
context |
A 'gemini_context' object. |
verbose |
Logical; if 'TRUE', prints additional logging information. |
api_key |
The API key for authentication (default is taken from 'getOption("gemini_api_key")'). |
The updated 'gemini_context' object with caching details.
Extract content from a more detailed run_gemini response
gemini_content(res_df)gemini_content(res_df)
Initializes a Gemini context object with caching options.
gemini_context( prompt = NULL, model = "gemini-1.5-flash-001", media = NULL, ttl_sec = 10 * 60, role = "user", do_cache = TRUE, api_key = getOption("gemini_api_key") )gemini_context( prompt = NULL, model = "gemini-1.5-flash-001", media = NULL, ttl_sec = 10 * 60, role = "user", do_cache = TRUE, api_key = getOption("gemini_api_key") )
prompt |
The input prompt for the model. |
model |
The model to use (default: '"gemini-1.5-flash-001"'). |
media |
Optional media input. |
ttl_sec |
Time-to-live for cache in seconds (default: '60*60'). |
role |
The role of the request (default: '"user"'). |
do_cache |
Logical; if 'TRUE', attempts to cache the context. |
api_key |
The API key for authentication (default is taken from 'getOption("gemini_api_key")'). |
A 'gemini_context' object.
Computes the remaining seconds until a cached context expires.
gemini_context_cache_expire_sec(context)gemini_context_cache_expire_sec(context)
context |
A 'gemini_context' object. |
The remaining seconds until expiration, or 'NA' if not cached.
Deletes all cached contexts from the Gemini API.
gemini_delete_all_context_caches(api_key = getOption("gemini_api_key"))gemini_delete_all_context_caches(api_key = getOption("gemini_api_key"))
api_key |
The API key for authentication (default is taken from 'getOption("gemini_api_key")'). |
The number of caches deleted.
Deletes a cached context from the Gemini API.
gemini_delete_context_cache( context = NULL, cache_name = context$cache_name, api_key = getOption("gemini_api_key"), verbose = FALSE )gemini_delete_context_cache( context = NULL, cache_name = context$cache_name, api_key = getOption("gemini_api_key"), verbose = FALSE )
context |
An optional context object containing the cache name. |
cache_name |
The name of the cache to delete (default is extracted from 'context'). |
api_key |
The API key for authentication (default is taken from 'getOption("gemini_api_key")'). |
verbose |
Logical; if TRUE, prints messages about the deletion process. |
Returns 'TRUE' if deletion is successful, 'FALSE' otherwise. If 'context' is provided, returns the updated context.
Retrieves a list of all cached contexts from the Gemini API.
gemini_list_context_caches(api_key = getOption("gemini_api_key"))gemini_list_context_caches(api_key = getOption("gemini_api_key"))
api_key |
The API key for authentication (default is taken from 'getOption("gemini_api_key")'). |
A data frame with cached context details, or 'NULL' if no caches are found.
Returned object or a list of such objects can then be passed to gemini_run. Media should be available for around an hour.
gemini_media_upload( file_path, mime_type = NULL, display_name = NULL, api_key = getOption("gemini_api_key"), verbose = FALSE )gemini_media_upload( file_path, mime_type = NULL, display_name = NULL, api_key = getOption("gemini_api_key"), verbose = FALSE )
Updates the TTL of a cached Gemini context.
gemini_update_context_cache( context, ttl_sec = context$ttl_sec, only_if_worked = TRUE, only_if_expired = TRUE, expired_sec_margin = 10, verbose = TRUE, api_key = getOption("gemini_api_key") )gemini_update_context_cache( context, ttl_sec = context$ttl_sec, only_if_worked = TRUE, only_if_expired = TRUE, expired_sec_margin = 10, verbose = TRUE, api_key = getOption("gemini_api_key") )
context |
A 'gemini_context' object. |
ttl_sec |
New TTL value (default: existing TTL). |
only_if_worked |
Logical; if 'TRUE', only updates if caching previously worked. |
only_if_expired |
Logical; if 'TRUE', only updates if cache is expired. |
expired_sec_margin |
Margin in seconds before cache is considered expired (default: '10'). |
api_key |
The API key for authentication (default is taken from 'getOption("gemini_api_key")'). |
The updated 'gemini_context' object.
This function inspects the provided value and returns a string representing the JSON type. It distinguishes between objects, arrays, and primitive types.
infer_response_type(x)infer_response_type(x)
x |
The value to inspect. |
A character string: one of "null", "object", "array", "string", "integer", "number", or "boolean".
This function creates a template for a single JSON object response. The response is expected to be a JSON object with the provided fields.
obj_resp(...)obj_resp(...)
... |
Named elements that define the fields for the JSON object. |
A list with class 'obj_resp' that serves as a template for an object response.
# Create an object response template with three fields. obj_template <- obj_resp(city = "Paris", country = "France", population = 5.2) str(obj_template)# Create an object response template with three fields. obj_template <- obj_resp(city = "Paris", country = "France", population = 5.2) str(obj_template)
This function recursively generates a JSON Schema based on a response template.
The template must be created using either obj_resp (for a single object response)
or arr_resp (for an array response). The function infers the JSON Schema
by inspecting the data types of the provided fields without including the example values.
response_schema(example)response_schema(example)
example |
A response template created by |
A list representing the inferred JSON Schema for the provided response template.
Sends a text prompt (and optionally one or more media objects) to the Gemini API to generate content.
run_gemini( prompt, model = "gemini-2.0-flash", media = NULL, json_mode = !is.null(response_schema), response_schema = NULL, temperature = 0.1, context = NULL, detailed_results = FALSE, make_content = TRUE, add_prompt = FALSE, verbose = FALSE, api_key = getOption("gemini_api_key") )run_gemini( prompt, model = "gemini-2.0-flash", media = NULL, json_mode = !is.null(response_schema), response_schema = NULL, temperature = 0.1, context = NULL, detailed_results = FALSE, make_content = TRUE, add_prompt = FALSE, verbose = FALSE, api_key = getOption("gemini_api_key") )
prompt |
A character string containing the text prompt to be sent to the Gemini API. |
model |
A character string specifying the Gemini model to use. Defaults to '"gemini-2.0-flash"'. |
media |
Either a single media object or a list of media objects to be included in the prompt. Call |
json_mode |
Logical. If |
response_schema |
Optional structured output schema for the response. Defaults to |
temperature |
A numeric value controlling the randomness of the output. Defaults to 0.1. |
context |
A context object generated by |
detailed_results |
Logical. If |
make_content |
Only relevant if |
add_prompt |
Only relevant if |
verbose |
Logical. If |
api_key |
A character string containing your Gemini API key. Defaults to the value obtained from |
The function builds a JSON payload that includes the provided text prompt as well as any media objects supplied via the media parameter.
Each media object is appended to the parts of the request under the file_data key. The payload is then sent via a POST request
to the Gemini API endpoint for content generation.
A list containing the Gemini API response. If as_data_frame is TRUE, the response is converted to a data frame.
## Not run: # Example using only a text prompt: result <- run_gemini(prompt = "Tell a joke", verbose = TRUE) ## End(Not run)## Not run: # Example using only a text prompt: result <- run_gemini(prompt = "Tell a joke", verbose = TRUE) ## End(Not run)
Set your Gemini API
set_gemini_api_key(key = NULL, file = NULL)set_gemini_api_key(key = NULL, file = NULL)