Vertex AI には、プロンプト テンプレートとプロンプト データを管理するためのツールが用意されています。プロンプト テンプレートはバージョニングして、Vertex AI の生成モデルと連携して使用できます。各プロンプトは、Vertex AI Studio または Vertex AI SDK でアセンブルしてバージョニングできます。
Vertex AI SDK には vertexai.preview.prompts モジュールが含まれているため、プロンプトで生成モデルを使用できます。vertexai.preview.prompts モジュールは、Gemini でテキストを生成するプロンプトを定義、保存、管理する機能をサポートしています。
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.....
project: 。これらの ID は、 Google Cloud コンソールのスタートページで確認できます。
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 オブジェクトを返します。次のコードサンプルは、保存したプロンプトを読み込む方法を示しています。
Vertex AI SDK for Python
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)
プロンプトを一覧表示する
現在のGoogle Cloud プロジェクトに保存されているすべてのプロンプトの表示名とプロンプト ID を表示するには、list_prompts() メソッドを使用します。
Vertex AI SDK for Python
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)
プロンプトを削除する
プロンプト ID に関連付けられているオンライン リソースを削除するには、delete() メソッドを使用します。
[[["わかりやすい","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 UTC。"],[],[],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\")"]]