Skip to content

Commit a0b5e06

Browse files
authored
Merge pull request #41 from jhk0530/39-new-feature-request-addition-of-time-out-parameter-in-vertex-ai
feat: update version to 0.14.1 and add timeout parameter to gemini and gemini.vertex functions
2 parents 4a6b918 + 8750e32 commit a0b5e06

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: gemini.R
22
Title: Interface for 'Google Gemini' API
3-
Version: 0.14.0
3+
Version: 0.14.1
44
Authors@R: c(
55
person("Jinhwan", "Kim", , "hwanistic@gmail.com", role = c("aut", "cre", "cph"), comment = c(ORCID = "0009-0009-3217-2417")),
66
person("Maciej", "Nasinski", role = "ctb"))

R/gemini.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' see https://ai.google.dev/gemini-api/docs/models/generative-models#model-parameters
1414
#' @param maxOutputTokens The maximum number of tokens to generate.
1515
#' Default is 8192 and 100 tokens correspond to roughly 60-80 words.
16+
#' @param timeout Request timeout in seconds. Default is 60.
1617
#' @return Generated text or image
1718
#' @export
1819
#' @examples
@@ -27,7 +28,7 @@
2728
#' @seealso https://ai.google.dev/docs/gemini_api_overview#text_input
2829
#'
2930

30-
gemini <- function(prompt, model = "2.0-flash", temperature = 1, maxOutputTokens = 8192, topK = 40, topP = 0.95, seed = 1234) {
31+
gemini <- function(prompt, model = "2.0-flash", temperature = 1, maxOutputTokens = 8192, topK = 40, topP = 0.95, seed = 1234, timeout = 60) {
3132
# Validate all parameters at once
3233
if (!validate_params(prompt, model, temperature, topP, topK, seed, api_key = TRUE)) {
3334
return(NULL)
@@ -63,10 +64,12 @@ gemini <- function(prompt, model = "2.0-flash", temperature = 1, maxOutputTokens
6364
generationConfig = generation_config
6465
)
6566

67+
# Set timeout using req_timeout
6668
req <- request(url) |>
6769
req_url_query(key = api_key) |>
6870
req_headers("Content-Type" = "application/json") |>
69-
req_body_json(request_body)
71+
req_body_json(request_body) |>
72+
req_timeout(as.integer(timeout))
7073
resp <- req_perform(req)
7174

7275
# Add logic to check status code
@@ -98,6 +101,7 @@ gemini <- function(prompt, model = "2.0-flash", temperature = 1, maxOutputTokens
98101
#' see https://ai.google.dev/gemini-api/docs/models/generative-models#model-parameters
99102
#' @param seed The seed to use. Default is 1234 value should be integer
100103
#' see https://ai.google.dev/gemini-api/docs/models/generative-models#model-parameters
104+
#' @param timeout Request timeout in seconds. Default is 60.
101105
#'
102106
#' @examples
103107
#' \dontrun{
@@ -113,7 +117,7 @@ gemini <- function(prompt, model = "2.0-flash", temperature = 1, maxOutputTokens
113117
#' @export
114118

115119
gemini.vertex <- function(prompt = NULL, tokens = NULL, temperature = 1, maxOutputTokens = 8192,
116-
topK = 40, topP = 0.95, seed = 1234) {
120+
topK = 40, topP = 0.95, seed = 1234, timeout = 60) {
117121
# Validate all parameters at once
118122
if (!validate_params(prompt, NULL, temperature, topP, topK, seed, api_key = FALSE, tokens = tokens)) {
119123
return(NULL)
@@ -143,13 +147,14 @@ gemini.vertex <- function(prompt = NULL, tokens = NULL, temperature = 1, maxOutp
143147
generationConfig = generation_config
144148
)
145149

146-
# Separate API request and check status code
150+
# Add req_timeout to set timeout
147151
req <- request(tokens$url) |>
148152
req_headers(
149153
"Authorization" = paste0("Bearer ", tokens$key),
150154
"Content-Type" = "application/json"
151155
) |>
152-
req_body_json(request_body)
156+
req_body_json(request_body) |>
157+
req_timeout(as.integer(timeout))
153158

154159
resp <- req_perform(req)
155160

man/gemini.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/gemini.vertex.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)