Use this file to discover all available pages before exploring further.
This quickstart guide will get you up and running with Gemini on Vertex AI. You’ll learn to generate text and build a multimodal application that processes both text and images.
Before starting, make sure you’ve completed the Environment Setup guide.
Let’s start with the simplest possible example - generating text from a prompt.
1
Import and Initialize
Create a new Python file or notebook and set up the client:
from google import genaifrom google.genai import types# Initialize the clientPROJECT_ID = "your-project-id" # Replace with your project IDLOCATION = "us-central1"client = genai.Client( vertexai=True, project=PROJECT_ID, location=LOCATION)
If you’re running on Google Colab, add authentication first:
from google.colab import authauth.authenticate_user()
2
Generate Your First Response
Send a prompt to Gemini and get a response:
response = client.models.generate_content( model="gemini-2.0-flash-exp", contents="Explain how AI works in simple terms.")print(response.text)
That’s it! You should see a clear, friendly explanation of AI concepts.
response = client.models.generate_content( model="gemini-2.0-flash-exp", contents="What are the key differences between Python and JavaScript?")print(response.text)
prompt = """Write a short story about a robot learning to paint.Make it heartwarming and under 200 words."""response = client.models.generate_content( model="gemini-2.0-flash-exp", contents=prompt)print(response.text)
Here’s a complete example that analyzes a product photo:
from google import genaifrom google.genai import types# Initialize clientclient = genai.Client( vertexai=True, project="your-project-id", location="us-central1")# Analyze a product imageresponse = client.models.generate_content( model="gemini-2.0-flash-exp", contents=[ types.Part.from_uri( file_uri="gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png", mime_type="image/png" ), """ Analyze this image and provide: 1. A detailed description 2. The mood or atmosphere 3. Potential use cases for marketing """ ])print(response.text)
Build conversational applications with chat history:
# Start a chat sessionchat = client.chats.create( model="gemini-2.0-flash-exp")# First messageresponse = chat.send_message( "I'm building a web app. Should I use React or Vue?")print("Gemini:", response.text)# Follow-up questionresponse = chat.send_message( "What about performance differences?")print("Gemini:", response.text)# The model remembers the contextresponse = chat.send_message( "Can you show me a simple component example?")print("Gemini:", response.text)
system_instruction = """You are a helpful coding assistant specializing in Python.Always provide working code examples with comments.Use best practices and explain your reasoning."""response = client.models.generate_content( model="gemini-2.0-flash-exp", contents="How do I read a CSV file?", config=types.GenerateContentConfig( system_instruction=system_instruction ))print(response.text)
Here’s a production-ready example combining multiple features:
from google import genaifrom google.genai import typesimport osclass GeminiAnalyzer: def __init__(self, project_id: str, location: str = "us-central1"): self.client = genai.Client( vertexai=True, project=project_id, location=location ) self.model = "gemini-2.0-flash-exp" def analyze_image(self, image_uri: str, question: str) -> str: """Analyze an image with a custom question.""" response = self.client.models.generate_content( model=self.model, contents=[ types.Part.from_uri( file_uri=image_uri, mime_type="image/jpeg" ), question ], config=types.GenerateContentConfig( temperature=0.4, max_output_tokens=2048 ) ) return response.text def chat(self, messages: list[str]) -> list[str]: """Have a multi-turn conversation.""" chat = self.client.chats.create(model=self.model) responses = [] for message in messages: response = chat.send_message(message) responses.append(response.text) return responses# Usageanalyzer = GeminiAnalyzer(project_id="your-project-id")# Analyze an imageresult = analyzer.analyze_image( image_uri="gs://cloud-samples-data/generative-ai/image/meal.png", question="What ingredients would I need to recreate this dish?")print(result)# Have a conversationresponses = analyzer.chat([ "What's the best way to learn Python?", "How long would that take?", "What resources do you recommend?"])for i, response in enumerate(responses, 1): print(f"Response {i}: {response}\n")