Skip to content

View or edit on GitHub

This page is synchronized from doc/Google-Cloud-Platform.md. Last modified on 2025-12-09 00:30 CET by Trase Admin. Please view or edit the original file there; changes should be reflected here after a midnight build (CET time), or manually triggering it with a GitHub action (link).

Google Cloud Platform (GCP) and Gemini

Using Gemini

There are a number of ways to interact with Gemini:

  • Gemini web app (gemini.google.com/): For general use, like a conversational chatbot.
  • Gemini AI Studio playground (aistudio.google.com/): For exploring the models and prototyping apps without needing to write any code. This web interface is always free to use, but comes with certain usage restrictions.
  • Vertex AI Studio (console.cloud.google.com/vertex-ai/studio): For developing AI applications and accessing more advanced features.
  • API: For integrating Gemini into your own scripts and accessing paid features without rate limitations.

Accessing Gemini via the API

You can do this via either an API key or a Application Default Credentials (ADC). We strongly recommend ADCs. An API key is scoped to a whole project, while ADCs are tied to a user or service account. ADCs support granular IAM roles, and handle credential rotation automatically. API keys are static, broad in scope, and easier to leak or misuse.

To generate an ADC see README.md – Section D: If you are going to be interacting with Google Cloud. You may need the role roles/aiplatform.user. If you are not able to do this then ask on the #data-systems channel on Slack.

The API can then be used as follows from Python:

from google import genai

client = genai.Client(vertexai=True, project="trase-396112", location="global")

prompt = "Write a short essay about what a great person I am"
response = client.models.generate_content(model="gemini-2.5-flash", contents=prompt)
answer = response.text.strip()

or from R:

# install.packages(c("httr2", "gargle"))  # if needed
library(httr2)
library(gargle)

project  <- "trase-396112"
location <- "global"
model    <- "gemini-2.5-flash"

prompt <- "Write a short essay about what a great person I am"

# Load credentials (assumed to have already been created e.g. by `gcloud auth application-default login`)
tok <- gargle::credentials_app_default(
  scopes = "https://www.googleapis.com/auth/cloud-platform"
)
access_token <- tok$credentials$access_token

# Vertex AI Generative endpoint for Gemini
endpoint <- paste0(
  "https://", location, "-aiplatform.googleapis.com/v1/",
  "projects/", project,
  "/locations/", location,
  "/publishers/google/models/", model, ":generateContent"
)

# Build request body
body <- list(
  contents = list(
    list(
      role  = "user",
      parts = list(list(text = prompt))
    )
  )
)

# Call the API
resp <- request(endpoint) |>
  req_headers(Authorization = paste("Bearer", access_token)) |>
  req_body_json(body) |>
  req_method("POST") |>
  req_perform()

# Extract the text response
dat <- resp_body_json(resp)
answer <- tryCatch(
  dat$candidates[[1]]$content$parts[[1]]$text,
  error = function(e) ""
)
cat(trimws(answer))