liveBook · Manning

Google Vertex AI

As explained in Starting Data Analytics with Generative AI and Python

Google Cloud's managed platform for developing, deploying, and using machine-learning and generative AI models.

Google Vertex AI answers a practical question: how can Python applications access Google’s generative models? It provides an API path to Gemini, Codey, and Imagen through Google Cloud. section 1.3.3

A basic Vertex AI workflow involves:

  • Installing the Google AI package with pip. section 1.3.3
  • Initializing Vertex AI with a Google Cloud project ID and location. section 1.3.3
  • Selecting a model such as gemini-1.0-pro and configuring output, randomness, and safety thresholds. section 1.3.3

Calling Gemini from Python

A Python function initializes Vertex AI, loads gemini-1.0-pro, accepts a prompt, and generates text with limits for tokens, temperature, top-p, top-k, and harmful-content blocking. section 1.3.3

Listing 1.4 Example code to call Vertex AI on Google Cloud
import vertexai
from vertexai.generative_models import GenerativeModel, ChatSession, 
HarmCategory, HarmBlockThreshold
# Replace PROJECT_ID with the ID of your Google Cloud project.
my_id = "PROJECT_ID"
# Replace with your Google Cloud location
my_location = "us-central1"
def generate_text(project_id: str, location: str, prompt: str) -> str:
    # Initialize Vertex AI
    vertexai.init(project=project_id, location=location)
    # Load the model
    model = GenerativeModel("gemini-1.0-pro")
    # Generation config
    config = {"max_output_tokens": 2048, "temperature": 0.4, "top_p": 1,
    "top_k": 32}
    # Safety config
    safety_config = {
        HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT:
        HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
        HarmCategory.HARM_CATEGORY_HARASSMENT:
        HarmBlockThreshold.BLOCK_LOW_AND_ABOVE
    }
    # Generate content
    responses = model.generate_content(
        [prompt],
        generation_config=config,
        stream=True,
        safety_settings=safety_config,
    )
    text_responses = []
    for response in responses:
        text_responses.append(response.text)
    return "".join(text_responses)
prompt = "What are all the colors in a rainbow?"
print(generate_text(my_id, my_location, prompt))

Inspect the complete Vertex AI call pattern, including imports, project configuration, model selection, generation settings, and safety controls. section 1.3.3

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Demonstrates package installation and a Python completion call, but does not detail deployment or broader platform operations.
Examples
Uses pip installation and Python code to call gemini-1.0-pro with generation and harmful-content blocking settings.
What it assumes
Assumes Google Cloud access, a project ID, a location, and package installation; teaches the basic initialization and completion flow.
What you can do after
Apply it via Python to generate responses from a selected Google model with configurable output and safety behavior.
How it connects
Connects Gemini, Codey, Imagen, Google Cloud, Cloud Shell, multimodal requests, and model safety parameters.

Also explained in

Compare how 2 books explain Google Vertex AI →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.