Text Extraction with Generative Models on Vertex AI

Source notebook

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

Uses Gemini on Vertex AI to extract structured facts from text with constrained and few-shot prompts.

Summary

This notebook teaches text extraction with generative models on Vertex AI using Gemini. It initializes the Vertex AI SDK, loads gemini-2.0-flash, and demonstrates prompts that extract product specs, troubleshooting steps, cited answers, recipe ingredients, and comma-separated entities. The workflow emphasizes generation parameters, JSON formatting, constrained responses, and few-shot examples for structured output.

Key code patterns

Initialize Vertex AI

PROJECT_ID = "your-project-id"
LOCATION = "us-central1"
 
import vertexai
vertexai.init(project=PROJECT_ID, location=LOCATION)

Sets the Google Cloud project and region before calling Gemini through Vertex AI.

Load Gemini model

from vertexai.generative_models import GenerationConfig, GenerativeModel
 
generation_model = GenerativeModel("gemini-2.0-flash")

Creates the generative model client used by every extraction example.

Configured generation

generation_config = GenerationConfig(
    temperature=0.2,
    max_output_tokens=1024,
    top_k=40,
    top_p=0.8,
)
response = generation_model.generate_content(
    contents=prompt,
    generation_config=generation_config,
).text

Controls output determinism and length for extraction tasks.

Constrained answering

prompt = """
Answer the question using the text below.
Respond with only the text provided.
Question: What should I do to fix my disconnected WiFi?
 
Text:
Color: Slowly pulsing yellow
What it means: There is a network error.
What to do:
Check that the Ethernet cable is connected...
"""

Grounds the answer in supplied text to reduce unsupported troubleshooting advice.

Few-shot extraction format

prompt = """
Message: Rachel Green (Jennifer Aniston)...
Extract the characters and the actors who played them:
Rachel Green - Jennifer Aniston, ...
 
Message: CapitalG was founded...
Extract the companies funded by CapitalG:
"""

Uses examples to teach the model the desired extraction format.

Models & APIs used

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

When to use this

Use this pattern when extracting structured fields or concise grounded answers from unstructured text with Gemini on Vertex AI.

Gotchas & caveats

  • Requires an existing Google Cloud project with the Vertex AI API enabled.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • The notebook uses billable Vertex AI components.
  • The notebook sets LOCATION to us-central1.
  • Generation parameters should be experimented with for the task.

Best practices

  • Initialize Vertex AI with an explicit project and location.
  • Use low temperature for extraction-oriented prompts.
  • Ask for JSON format when downstream systems need structured output.
  • Constrain answers to provided text for troubleshooting responses.
  • Use few-shot prompting to guide output organization.