Skip to content

Commit 47cbc5a

Browse files
authored
Merge pull request #47 from jhk0530/37-new-feature-request-access-to-other-model-cards---mistral-or-llama-models-in-gemini-vertex-a
feat: Add gemini_garden function
2 parents 68a3f97 + 860f6fe commit 47cbc5a

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export(gemini_audio.vertex)
88
export(gemini_chat)
99
export(gemini_docs)
1010
export(gemini_docs.vertex)
11+
export(gemini_garden)
1112
export(gemini_image)
1213
export(gemini_image.vertex)
1314
export(gemini_narrative)
@@ -46,6 +47,7 @@ importFrom(httr2,request)
4647
importFrom(httr2,resp_body_json)
4748
importFrom(httr2,resp_body_string)
4849
importFrom(httr2,resp_header)
50+
importFrom(jsonlite,base64_enc)
4951
importFrom(jsonlite,fromJSON)
5052
importFrom(jsonlite,toJSON)
5153
importFrom(knitr,kable)

R/gemini_garden.R

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#' @title Interact with Vertex AI Model Garden
2+
#' @description
3+
#' This function sends a PDF file to the Vertex AI Model Garden (Mistral model) for processing, such as OCR. The PDF is encoded as base64 and sent to the rawPredict endpoint. The function is designed for future extension to support other document types and tasks.
4+
#'
5+
#' @param token Token object (e.g., from \code{token.vertex()}) containing the access token, region, and model_id.
6+
#' @param project_id Google Cloud project ID.
7+
#' @param pdf_path Path to the PDF file to be processed.
8+
#'
9+
#' @return A parsed list containing the results from the Vertex AI API (e.g., OCR results).
10+
#'
11+
#' @details
12+
#' The PDF file is read and encoded as base64, then sent to the Vertex AI rawPredict endpoint for processing using a Mistral model. This function is structured for future extension to support other document types and model tasks available in Vertex AI Model Garden.
13+
#'
14+
#' For more information about available models, endpoints, and supported tasks, see \href{https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/explore-models}{Vertex AI Model Garden documentation}.
15+
#'
16+
#' @examples
17+
#' \dontrun{
18+
#' # Issue a token using token.vertex() first
19+
#' my_token <- token.vertex(
20+
#' jsonkey = "your-service-account.json",
21+
#' region = "us-central1",
22+
#' model_id = "mistral-ocr-2505"
23+
#' )
24+
#' result <- gemini_garden(
25+
#' token = my_token,
26+
#' project_id = "your-project-id",
27+
#' pdf_path = "sample.pdf"
28+
#' )
29+
#' print(result)
30+
#' }
31+
#'
32+
#' @importFrom jsonlite base64_enc
33+
#' @seealso \url{https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/explore-models}
34+
#' @importFrom httr2 request req_headers req_body_json req_perform resp_body_string
35+
#' @importFrom jsonlite toJSON fromJSON
36+
#'
37+
#' @export
38+
gemini_garden <- function(token, project_id, pdf_path) {
39+
# Extract access token from token object
40+
access_token <- token$key
41+
42+
# Extract region from /locations/{region}/ in the URL
43+
region <- sub(".*/locations/([^/]+).*", "\\1", token$url)
44+
# Extract model_id using regular expression
45+
model_id <- sub(".*/models/([^:/]+).*", "\\1", token$url)
46+
# Remove "gemini-" prefix from model_id if present
47+
model_id <- sub("^gemini-", "", model_id)
48+
49+
# Construct endpoint URL for rawPredict (for mistral models)
50+
url <- sprintf(
51+
"https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/mistralai/models/%s:rawPredict",
52+
region, project_id, region, model_id
53+
)
54+
55+
# Read and encode PDF file as base64
56+
pdf_bin <- readBin(pdf_path, "raw", file.info(pdf_path)$size)
57+
base64_pdf <- base64_enc(pdf_bin)
58+
59+
# Build request payload
60+
payload <- list(
61+
model = model_id,
62+
document = list(
63+
type = "document_url",
64+
document_url = paste0("data:application/pdf;base64,", base64_pdf)
65+
),
66+
include_image_base64 = TRUE
67+
)
68+
69+
# Make the API request using httr2
70+
req <- httr2::request(url) |>
71+
httr2::req_headers(
72+
Authorization = paste("Bearer", access_token),
73+
`Content-Type` = "application/json"
74+
) |>
75+
httr2::req_body_json(payload)
76+
77+
resp <- httr2::req_perform(req)
78+
79+
# Output the response as a parsed list
80+
response_text <- httr2::resp_body_string(resp)
81+
return(jsonlite::fromJSON(response_text, flatten = TRUE))
82+
}

man/gemini_garden.Rd

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/gemini_narrative.Rd

Lines changed: 1 addition & 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)