# Load the gemini.R package (an R wrapper for Google's Gemini API)
library(gemini.R)
# Set your API key (you get this from https://ai.google.dev/gemini-api/docs)
google_key <- ""
setAPI(google_key)
# Define 4 different prompts to send to Gemini
# These are deliberately varied: a serious question, a playful one,
# a summarisation task, and a deliberately weird one
prompts <- c("What is the meaning of life",
"What is the meaning of life? (Wrong answers only)",
"Summarise the plot of Harry Potter in a paragraph.",
"Explain the concept of Elon Musk.")
# Create a sequence of temperature values from 0.1 to 2.0, in steps of 0.1
# This gives us 20 different temperature settings to test
temperatures <- seq(.1, 2, .1)
# Initialise a counter for filling in the data frame row by row
i <- 0
# Pre-allocate an empty data frame with one row for every prompt × temperature combination
# That's 4 prompts × 20 temperatures = 80 rows
gemini_out <- data.frame(temperature = rep(NA, length(temperatures) * length(prompts)),
prompt = rep(NA, length(temperatures) * length(prompts)),
text = rep(NA, length(temperatures) * length(prompts)))
# Outer loop: iterate over each prompt
for(p in prompts){
print(p) # Print the current prompt to the console so we can track progress
# Inner loop: for each prompt, iterate over every temperature value
for(temp in temperatures){
# Pause for 2.2 seconds between API calls to avoid hitting rate limits
Sys.sleep(2.2)
# Increment the row counter
i <- i+1
# Call the Gemini API with the current prompt and temperature,
# and store the generated text, prompt, and temperature in the data frame
gemini_out$text[i] <- gemini(prompt = p, temperature = temp)
gemini_out$prompt[i] <- p
gemini_out$temperature[i] <- temp
}
}
# Save the full results data frame to an .Rdata file for later analysis
save(gemini_out, file = "gemini_out.Rdata")
Social science applications