Starting April 29, 2025, Gemini 1.5 Pro and Gemini 1.5 Flash models are not available in projects that have no prior usage of these models, including new projects. For details, see
Model versions and lifecycle.
Optimize prompts for text generation with Vertex AI
Stay organized with collections
Save and categorize content based on your preferences.
This sample demonstrates how to use the Vertex AI Prompt Optimizer to optimize prompts for a text generation model.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Optimize prompts for text generation with Vertex AI\n\nThis sample demonstrates how to use the Vertex AI Prompt Optimizer to optimize prompts for a text generation model.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Optimize prompts](/vertex-ai/generative-ai/docs/learn/prompts/prompt-optimizer)\n\nCode sample\n-----------\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Python API\nreference documentation](/python/docs/reference/aiplatform/latest).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import aiplatform\n\n # Initialize Vertex AI platform\n aiplatform.https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.html(project=PROJECT_ID, location=\"us-central1\")\n\n # TODO(Developer): Check and update lines below\n # cloud_bucket = \"gs://cloud-samples-data\"\n # config_path = f\"{cloud_bucket}/instructions/sample_configuration.json\"\n # output_path = \"custom_job/output/\"\n\n custom_job = aiplatform.https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.types.CustomJob.html(\n display_name=\"Prompt Optimizer example\",\n worker_pool_specs=[\n {\n \"replica_count\": 1,\n \"container_spec\": {\n \"image_uri\": \"us-docker.pkg.dev/vertex-ai-restricted/builtin-algorithm/apd:preview_v1_0\",\n \"args\": [f\"--config={cloud_bucket}/{config_path}\"],\n },\n \"machine_spec\": {\n \"machine_type\": \"n1-standard-4\",\n },\n }\n ],\n staging_bucket=cloud_bucket,\n base_output_dir=f\"{cloud_bucket}/{output_path}\",\n )\n\n custom_job.submit()\n print(f\"Job resource name: {custom_job.resource_name}\")\n # Example response:\n # 'projects/123412341234/locations/us-central1/customJobs/12341234123412341234'\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=generativeaionvertexai)."]]