Google Vertex AI & Gemini API: A Surprisingly Simple Setup

A beginners guide to understanding the Google Gemini API code generated in Google Vertex AI

Allan Alfonso
Google Cloud - Community
4 min readAug 3, 2024

--

How do I use the Google Gemini API?

This is a common question I get asked. The quick answer I give is to use Vertex AI in the Google Cloud Console since it is an easy buttton. Vertex AI provides developer tools to start using the Gemini API quickly. You can experiment with prompts and settings visually and once you’re satisified with the response, Vertex AI will generate code for your application. Once you see how the code works, you can’t unsee it and then developing new AI applications becomes easy.

Let’s look at an example.

Vertex AI will Generate Google Gemini API Code

Python Code Generated from Google Vertex AI Studio

In this example, I’m analyzing the Berkshire Hathaway 2023 Shareholder Letter.

The shareholder letter is a PDF that is stored in Cloud Storage. I configure the settings such as model type, region, and output token limit. Then I provide a prompt “What are Warren Buffet’s main concerns in 2023?”.

Let’s dive into the Python code.

pip install --upgrade google-cloud-aiplatform
gcloud auth application-default login
  • Install the Python Google Cloud AI libraries packages using the PIP package manager.
  • gcloud auth application-defaul login authenticates you to Google Cloud so you can make API calls.
import base64
import vertexai
from vertexai.generative_models import GenerativeModel, Part, FinishReason
import vertexai.preview.generative_models as generative_models
  • Import the Google AI Python libraries.
def generate():
vertexai.init(project="<project-id>", location="us-central1")
model = GenerativeModel(
"gemini-1.5-pro-001",
)
responses = model.generate_content(
[document1, """What are Warren Buffet\'s main concerns in 2023?"""],
generation_config=generation_config,
safety_settings=safety_settings,
stream=True,
)

for response in responses:
print(response.text, end="")

Let’s break down the generate() function created by Google.

vertexai.init(project="<project-id>", location="us-central1")
  • Initialize Vertex AI with your project ID and region.
model = GenerativeModel(
"gemini-1.5-pro-001",
)
responses = model.generate_content(
[document1, """What are Warren Buffet\'s main concerns in 2023?"""],
generation_config=generation_config,
safety_settings=safety_settings,
stream=True,
)
  • model.generate_content sends your prompt and settings to the model. An object called responses is returned.
  • What are Warren Buffet’s main concerns in 2023?” is the prompt I’m sending to the model.
  • Send the prompt, PDF, and safety settings to the model.
  • stream=True tells the model to stream the response. You can also tell the model not to stream the response by setting stream=False.
  • Loop through all the responses and print the text of each response.
document1 = Part.from_uri(
mime_type="application/pdf",
uri="gs://berkshire-hathaway-shareholder-letters/warren-buffet-shareholder-letter-2023.pdf")
  • document1 is the file we are sending to the model.
  • Specify the mime_type. In this example, it’s a PDF.
  • Provide the Cloud Storage URI so the model can reference the PDF. In this case, the PDF is the Berkshire Hathaway 2023 Shareholder Letter that I downloaded from the Berkshire Hathaway website.
Configurable Settings in Vertex AI
generation_config = {
"max_output_tokens": 8192,
"temperature": 1,
"top_p": 0.95,
}
  • generation_config contains your settings such as output tokens, temperature, and top_p. These settings map to what is configured in the GUI.
safety_settings = {
generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
}
  • Configure the safety_settings to block content that is likely harmful.
generate()
  • Call the function generate() to generate a response from the model.
Run a Python Script using the code from Google Gemini.

I can convert the code into a Python script (example.py) and run it.

Start Building Applications using the Google Gemini API

Build using Vertex AI

The Google Gemini API is easy to use.

Many people don’t know that Google Cloud generates code in Vertex AI Studio. By selecting the “<>Get Code” option at the top right (above where you select the model), Google will generate code options in Python, Node.JS, Java, and CURL based on the settings and prompts that you configure in the GUI. This allows you to experiment with different prompts and settings in the GUI and once you’re satisfied with the response, you can generate code for your application.

Now that you know this feature exists, try it for yourself and see how easy it is to start building applications using Vertex AI and Google Gemini.

--

--