|
| 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 | +} |
0 commit comments