使用 Vertex AI 最佳化文字產生提示

這個範例示範如何使用 Vertex AI 提示最佳化工具,為文字生成模型最佳化提示。

深入探索

如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:

程式碼範例

Python

在試用這個範例之前,請先按照Python使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Python API 參考說明文件

如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

from google.cloud import 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'

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器