Learn R Programming

gemini.R (version 0.15.0)

gemini_structured: Generate structured response from Gemini

Description

Returns a structured (JSON) response from the Gemini API.

Usage

gemini_structured(
  prompt,
  schema,
  model = "2.5-flash",
  temperature = 1,
  maxOutputTokens = 8192,
  topK = 40,
  topP = 0.95,
  seed = 1234,
  timeout = 60
)

Value

A structured list (parsed JSON).

Arguments

prompt

The prompt (question) to send to the model.

schema

JSON schema (as a list) for the expected response.

model

Model to use. Default is '2.5-flash'.

temperature

Sampling temperature. Default is 1.

maxOutputTokens

Maximum number of output tokens. Default is 8192.

topK

Top-k value. Default is 40.

topP

Top-p value. Default is 0.95.

seed

Random seed. Default is 1234.

timeout

Request timeout in seconds. Default is 60.

Examples

Run this code
if (FALSE) {
schema <- list(
  type = "ARRAY",
  items = list(
    type = "OBJECT",
    properties = list(
      recipeName = list(type = "STRING"),
      ingredients = list(
        type = "ARRAY",
        items = list(type = "STRING")
      )
    ),
    propertyOrdering = c("recipeName", "ingredients")
  )
)
gemini_structured(
  "List a few popular cookie recipes, and include the amounts of ingredients.",
  schema
)
}

Run the code above in your browser using DataLab