Text Classification with Generative Models on Vertex AI

Source notebook

Repo path: gemini/prompts/examples/text_classification.ipynb · Open on GitHub · intro

Classifies text with Gemini on Vertex AI using zero-shot, few-shot, and evaluation workflows.

Summary

This notebook teaches how to use prompts with Gemini on Vertex AI for text classification tasks. It initializes the Vertex AI SDK, configures Gemini generation settings, runs zero-shot and few-shot prompts, then demonstrates common classification tasks such as sentiment, topic, spam, intent, language, toxicity, and emotion detection. It ends by evaluating sentiment predictions against ground truth labels with scikit-learn.

Key code patterns

Initialize Vertex AI Gemini

import vertexai
from vertexai.generative_models import GenerationConfig, GenerativeModel
 
vertexai.init(project=PROJECT_ID, location=LOCATION)
generation_model = GenerativeModel("gemini-2.0-flash")
generation_config = GenerationConfig(temperature=0.1, max_output_tokens=256)

Sets the Google Cloud project, region, Gemini model, and generation controls used by every classification call.

Zero-shot classification prompt

prompt = """
Classify the following:\n
text: "I saw a furry animal in the park today with a long tail and big eyes."
label: dogs, cats
"""
response = generation_model.generate_content(
    contents=prompt, generation_config=generation_config
).text

Shows classification without labeled examples by naming candidate labels directly in the prompt.

Few-shot topic classification

prompt = """
Text: Pixel 7 Pro Expert Hands On Review.
The answer is: technology
 
Text: Quit smoking?
The answer is: health
 
Text: You won't guess who just arrived in Bari, Italy for the movie premiere.
The answer is:
"""
response = generation_model.generate_content(contents=prompt, generation_config=generation_config).text

Provides labeled examples so Gemini can infer the target label format for a new headline.

Evaluate predictions

def get_sentiment(row):
    prompt = f"""Classify the sentiment of the following review as "positive", "neutral" and "negative".\n\nreview: {row}\nsentiment:"""
    return generation_model.generate_content(contents=prompt, generation_config=generation_config).text
 
review_data_df["sentiment_prediction"] = review_data_df["review"].apply(get_sentiment)
report = classification_report(review_data_df["sentiment_groundtruth"], review_data_df["sentiment_prediction"])

Applies Gemini to each review row and compares predictions with ground truth labels using classification metrics.

Models & APIs used

  • Models: gemini-2.0-flash
  • APIs / services: Vertex AI
  • SDKs / libraries: google-cloud-aiplatform, vertexai, pandas, scikit-learn

When to use this

Use this pattern when you need fast text classification with Gemini prompts before building a labeled-data or tuned model workflow.

Gotchas & caveats

  • A Google Cloud project is required before initializing Vertex AI.
  • The Vertex AI API must be enabled.
  • The notebook authenticates only when running in Google Colab.
  • The configured location is us-central1.
  • Generation parameter values can change outputs, so the notebook recommends experimenting with them.
  • Evaluation requires ground truth classes.

Best practices

  • Use explicit candidate labels or task instructions in classification prompts.
  • Use few-shot examples when the model should follow a specific class mapping or answer format.
  • Use low temperature and limited max output tokens for concise classification responses.
  • Evaluate classification outputs against ground truth labels when they are available.
  • Keep custom model training separate when prompt-based classification is sufficient for the notebook scope.