Optimize prompts for text generation with Vertex AI

This sample demonstrates how to use the Vertex AI Prompt Optimizer to optimize prompts for a text generation model.

Code sample

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Python API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

fromgoogle.cloudimport aiplatform
# Initialize Vertex AI platform
aiplatform.init (project=PROJECT_ID, location="us-central1")
# TODO(Developer): Check and update lines below
# cloud_bucket = "gs://cloud-samples-data"
# config_path = f"{cloud_bucket}/instructions/sample_configuration.json"
# output_path = "custom_job/output/"
custom_job = aiplatform.CustomJob (
 display_name="Prompt Optimizer example",
 worker_pool_specs=[
 {
 "replica_count": 1,
 "container_spec": {
 "image_uri": "us-docker.pkg.dev/vertex-ai-restricted/builtin-algorithm/apd:preview_v1_0",
 "args": [f"--config={cloud_bucket}/{config_path}"],
 },
 "machine_spec": {
 "machine_type": "n1-standard-4",
 },
 }
 ],
 staging_bucket=cloud_bucket,
 base_output_dir=f"{cloud_bucket}/{output_path}",
)
custom_job.submit()
print(f"Job resource name: {custom_job.resource_name}")
# Example response:
# 'projects/123412341234/locations/us-central1/customJobs/12341234123412341234'

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

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.