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

