Text Summarization with Generative Models on Vertex AI
Source notebook
Repo path:
gemini/prompts/examples/text_summarization.ipynb· Open on GitHub · intro
Demonstrates Gemini text summarization prompts on Vertex AI and evaluates summaries with ROUGE.
Summary
This notebook teaches how to use a generative model on Vertex AI to summarize text in several formats, including short summaries, TL;DRs, bullet points, dialogue summaries with to-dos, hashtags, and title options. It initializes the Vertex AI SDK, loads gemini-2.0-flash, varies GenerationConfig parameters, and calls generate_content with task-specific prompts. It also compares a model-generated summary with a human reference summary using ROUGE scores.
Key code patterns
Initialize Vertex AI
import vertexai
vertexai.init(project=PROJECT_ID, location=LOCATION)Sets the Google Cloud project and region before using Vertex AI generative models.
Load Gemini model
from vertexai.generative_models import GenerationConfig, GenerativeModel
generation_model = GenerativeModel("gemini-2.0-flash")Creates a reusable Gemini model client for all prompt examples.
Generate summary
generation_config = GenerationConfig(temperature=0.1, max_output_tokens=256)
response = generation_model.generate_content(
contents=prompt,
generation_config=generation_config,
).textShows the core pattern for sending a summarization prompt with controlled generation parameters.
Tune generation settings
generation_config = GenerationConfig(
temperature=0.2,
max_output_tokens=256,
top_k=1,
top_p=0.8,
)Demonstrates changing temperature, top_k, top_p, and output length for different summarization tasks.
Evaluate with ROUGE
from rouge import Rouge
ROUGE = Rouge()
ROUGE.get_scores(candidate, reference)Compares a model-generated summary against a human-created reference summary.
Models & APIs used
- Models: gemini-2.0-flash
- APIs / services: Vertex AI
- SDKs / libraries:
google-cloud-aiplatform,vertexai,rouge
When to use this
Use this pattern when building prompt-based text summarization workflows with Gemini on Vertex AI and basic ROUGE evaluation.
Gotchas & caveats
- Vertex AI is a billable Google Cloud component.
- A Google Cloud project is required and the Vertex AI API must be enabled.
- Colab users must authenticate with google.colab.auth.authenticate_user().
- The notebook uses LOCATION = “us-central1” for Vertex AI initialization.
- GenerationConfig values can change outputs, so the notebook recommends experimenting with parameters.
Best practices
- Initialize Vertex AI with an explicit project and location before model calls.
- Use low temperature for concise summary generation when consistency is desired.
- Adjust temperature, max_output_tokens, top_k, and top_p for different output styles.
- Write prompts that specify the desired format, such as bullet points, TL;DR, to-dos, or title options.
- Evaluate generated summaries against human-created summaries with ROUGE metrics.
Related
- Concepts: Prompt Engineering · Evaluation · Applied Use Cases
- Entities: Vertex AI · Vertex AI SDK · Gemini
- Area: Gemini Notebooks
- Best practices: Prompt Engineering - Best Practices · Evaluation - Best Practices · Applied Use Cases - Best Practices