importvertexaifromvertexai.previewimportpromptsfromvertexai.preview.promptsimportPrompt# from vertexai.generative_models import GenerationConfig, SafetySetting # Optional# Initialize vertexaivertexai.init(project=PROJECT_ID,location="us-central1")# Create local Promptlocal_prompt=Prompt(prompt_name="movie-critic",prompt_data="Compare the movies {movie1} and {movie2}.",variables=[{"movie1":"The Lion King","movie2":"Frozen"},{"movie1":"Inception","movie2":"Interstellar"},],model_name="gemini-2.0-flash-001",system_instruction="You are a movie critic. Answer in a short sentence.",# generation_config=GenerationConfig, # Optional,# safety_settings=SafetySetting, # Optional,)# Generate content using the assembled prompt for each variable set.foriinrange(len(local_prompt.variables)):response=local_prompt.generate_content(contents=local_prompt.assemble_contents(**local_prompt.variables[i]))print(response)# Save a versionprompt1=prompts.create_version(prompt=local_prompt)print(prompt1)# Example response# Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2# Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2# Created prompt resource with id 12345678910.....
fromvertexai.previewimportprompts# Save Prompt to online resource.# Returns a new Prompt object associated with the online resource.prompt1=prompts.create_version(prompt=prompt)
載入已儲存的提示
如要載入已儲存至線上資源的提示,請使用 vertexai.preview.prompts.get() 方法。這個方法會將提示 ID 做為輸入內容,並傳回對應的 Prompt 物件。這個程式碼範例說明如何載入已儲存的提示:
Python 適用的 Vertex AI SDK
fromvertexai.previewimportprompts# Get promptprompt=prompts.get(prompt_id="123456789")
fromvertexai.previewimportpromptsfromvertexai.preview.promptsimportPrompt# Get promptprompt=prompts.get(prompt_id="123456789")# Generate content using the assembled prompt (a prompt without variables)prompt.generate_content(contents=prompt.assemble_contents())# Update prompt (changes are local until create_version is called)prompt.prompt_data="new prompt"# Save Prompt to online resource. Since the prompt is associated with a prompt resource, it creates a new version under the same prompt_id. Returns a new Prompt object associated with the online resourceprompt1=prompts.create_version(prompt=prompt)
fromvertexai.previewimportpromptsprompts_metadata=prompts.list()# Get a prompt from the listprompt1=prompts.get(prompt_id=prompts_metadata[0].prompt_id)
fromvertexai.previewimportpromptsprompt_versions_metadata=prompts.list_versions(prompt_id="123456789")# Get a specific prompt version from the versions metadata listprompt1=prompts.get(prompt_id=prompt_versions_metadata[3].prompt_id,version_id=prompt_versions_metadata[3].version_id)
fromvertexai.previewimportprompts# Restore to prompt version id 1 (original)prompt_version_metadata=prompts.restore_version(prompt_id="123456789",version_id="1")# Fetch the newly restored latest version of the promptprompt1=prompts.get(prompt_id=prompt_version_metadata.prompt_id)
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-02 (世界標準時間)。"],[],[],null,["# Prompt management\n\n| **Preview**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nVertex AI offers tooling to help manage prompt templates and\nprompt data. Prompt templates can be versioned and used in tandem with\ngenerative models on Vertex AI. Each prompt can be assembled and\nversioned in Vertex AI Studio or the Vertex AI SDK.\n\nVertex AI SDK includes the `vertexai.preview.prompts` module so that\nprompts can work with generative models. The `vertexai.preview.prompts` module\nsupports the ability to define, save, and manage prompts for generating text\nwith Gemini.\n\n### `Prompt`\n\nThe [Prompt class](/vertex-ai/generative-ai/docs/reference/python/latest/vertexai.preview.prompts#prompt) represents a prompt that can be used to generate text with a\nGemini method, which encapsulates the prompt data, variables,\ngeneration configuration, and other relevant information.\n\nTo create a `Prompt` object, use the `vertexai.preview.prompts.Prompt()` constructor.\nYou can define the prompt data, variables, and other configurations within\nthis object.\n\n#### Create a local prompt and generate content\n\n### Vertex AI SDK for Python\n\n### Python\n\n import vertexai\n from vertexai.preview import prompts\n from vertexai.preview.prompts import Prompt\n\n # from vertexai.generative_models import GenerationConfig, SafetySetting # Optional\n\n # Initialize vertexai\n vertexai.init(project=PROJECT_ID, location=\"us-central1\")\n\n # Create local Prompt\n local_prompt = Prompt(\n prompt_name=\"movie-critic\",\n prompt_data=\"Compare the movies {movie1} and {movie2}.\",\n variables=[\n {\"movie1\": \"The Lion King\", \"movie2\": \"Frozen\"},\n {\"movie1\": \"Inception\", \"movie2\": \"Interstellar\"},\n ],\n model_name=\"gemini-2.0-flash-001\",\n system_instruction=\"You are a movie critic. Answer in a short sentence.\",\n # generation_config=GenerationConfig, # Optional,\n # safety_settings=SafetySetting, # Optional,\n )\n\n # Generate content using the assembled prompt for each variable set.\n for i in range(len(local_prompt.variables)):\n response = local_prompt.generate_content(\n contents=local_prompt.assemble_contents(**local_prompt.variables[i])\n )\n print(response)\n\n # Save a version\n prompt1 = prompts.create_version(prompt=local_prompt)\n\n print(prompt1)\n\n # Example response\n # Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2\n # Assembled prompt replacing: 1 instances of variable movie1, 1 instances of variable movie2\n # Created prompt resource with id 12345678910.....\n\n- `project`: . You can find these IDs in the Google Cloud console [welcome](https://console.cloud.google.com/welcome) page.\n- `location`: See [Vertex AI\n locations](/vertex-ai/docs/general/locations).\n- `prompt_name`: The display name of the prompt created by the user, if stored in an online resource.\n- `prompt_data`: A `PartsType` prompt, which can be a template with variables or a prompt with no variables.\n- `variables`: A list of dictionaries containing the variable names and values.\n- `generation_config`: A `GenerationConfig` object containing parameters for generation.\n- `model_name`: Model Garden model resource name. Alternatively, a tuned model endpoint resource name can be provided. If no model is provided, the default latest model is used.\n- `safety_settings`: A `SafetySetting` object containing safety settings for generation.\n- `system_instruction`: A `PartsType` object representing the system instruction.\n\nAfter the creation of a `Prompt` object, the prompt data and properties\nrepresenting various configurations can be used to generate content.\n\nPrompts also support function calling. See\n[Introduction to function calling](/vertex-ai/generative-ai/docs/multimodal/function-calling)\nto learn more.\n\n### Save a prompt\n\nTo save a prompt to an online resource, which can be accessed in the\nGoogle Cloud console, use the\n`vertexai.preview.prompts.create_version()` method. This method takes a `Prompt`\nobject as input and creates a new version of the prompt in the online store.\nA new `Prompt` object is returned which is associated with the online resource.\nAny updates made to a `Prompt` object are local until `create_version()`\nis called. The following code sample shows how to save a prompt: \n\n### Vertex AI SDK for Python\n\n```python\nfrom vertexai.preview import prompts\n\n# Save Prompt to online resource.\n# Returns a new Prompt object associated with the online resource.\nprompt1 = prompts.create_version(prompt=prompt)\n```\n\n### Load a saved prompt\n\nTo load a prompt that has been saved to the online resource, use the\n`vertexai.preview.prompts.get()` method. This method takes the prompt ID as\ninput and returns the corresponding `Prompt` object. This code\nsample shows how to load a saved prompt: \n\n### Vertex AI SDK for Python\n\n from vertexai.preview import prompts\n\n # Get prompt\n prompt = prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html(prompt_id=\"123456789\")\n\n### Retrieve prompt created in the Google Cloud console\n\nTo update a saved prompt, first load the prompt using the\n[`get()` method](/vertex-ai/generative-ai/docs/reference/python/latest/vertexai.preview.prompts#vertexai_preview_prompts_get),\nmodify its properties as needed, and then save the updated prompt using the\n`create_version()` method. This creates a new version of the prompt with the\nupdated information. \n\n### Vertex AI SDK for Python\n\n from vertexai.preview import prompts\n from vertexai.preview.prompts import Prompt\n\n # Get prompt\n prompt = prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html(prompt_id=\"123456789\")\n\n # Generate content using the assembled prompt (a prompt without variables)\n prompt.generate_content(\n contents=prompt.assemble_contents()\n )\n\n # Update prompt (changes are local until create_version is called)\n prompt.prompt_data = \"new prompt\"\n\n # Save Prompt to online resource. Since the prompt is associated with a prompt resource, it creates a new version under the same prompt_id. Returns a new Prompt object associated with the online resource\n prompt1 = prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html(prompt=prompt)\n\n### List prompts\n\nTo see the display names and prompt IDs of all prompts saved in the current\nGoogle Cloud project, use the [`list_prompts()`method](/vertex-ai/generative-ai/docs/reference/python/latest/vertexai.preview.prompts#vertexai_preview_prompts_list). \n\n### Vertex AI SDK for Python\n\n from vertexai.preview import prompts\n\n prompts_metadata = prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html()\n\n # Get a prompt from the list\n prompt1 = prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html(prompt_id=prompts_metadata[0].prompt_id)\n\n### List prompt versions\n\nTo see the display names and version IDs of all prompt versions saved within\nthe prompt, use the [`list_versions()` method](/vertex-ai/generative-ai/docs/reference/python/latest/vertexai.preview.prompts#vertexai_preview_prompts_list_versions) . \n\n### Vertex AI SDK for Python\n\n from vertexai.preview import prompts\n\n prompt_versions_metadata = prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html(prompt_id=\"123456789\")\n\n # Get a specific prompt version from the versions metadata list\n prompt1 = prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html(\n prompt_id=prompt_versions_metadata[3].prompt_id,\n version_id=prompt_versions_metadata[3].version_id\n )\n\n### Restore a prompt version\n\nA prompt resource also contains version history that stores previous saved\nversions of the prompt. You can use the\n[`restore_version()` method](/vertex-ai/generative-ai/docs/reference/python/latest/vertexai.preview.prompts#vertexai_preview_prompts_restore_version)\nto restore an older version as the latest version of the prompt. This returns\nPromptVersionMetadata that can be used with a `get()` call to fetch the newly\nrestored version. \n\n### Vertex AI SDK for Python\n\n from vertexai.preview import prompts\n\n # Restore to prompt version id 1 (original)\n prompt_version_metadata = prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html(prompt_id=\"123456789\", version_id=\"1\")\n\n # Fetch the newly restored latest version of the prompt\n prompt1 = prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html(prompt_id=prompt_version_metadata.prompt_id)\n\n### Delete a prompt\n\nTo delete the online resource associated with a prompt ID, use the\n[`delete()` method](/vertex-ai/generative-ai/docs/reference/python/latest/vertexai.preview.prompts#vertexai_preview_prompts_delete). \n\n### Vertex AI SDK for Python\n\n from vertexai.preview import prompts\n\n prompts.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.prompts.html(prompt_id=\"123456789\")"]]