Vertex AI는 프롬프트 템플릿과 프롬프트 데이터를 관리하는 데 도움이 되는 도구를 제공합니다. 프롬프트 템플릿은 버전을 지정할 수 있으며 Vertex AI의 생성형 모델과 함께 사용할 수 있습니다. 각 프롬프트는 서로 조합하여 Vertex AI Studio 또는 Vertex AI SDK에서 버전을 지정할 수 있습니다.
프롬프트가 생성형 모델과 함께 작동할 수 있도록 Vertex AI SDK에는 vertexai.preview.prompts 모듈이 포함되어 있습니다. vertexai.preview.prompts 모듈은 Gemini로 텍스트를 생성하기 위한 프롬프트를 정의, 저장, 관리하는 기능을 지원합니다.
Prompt
프롬프트 클래스는 프롬프트 데이터, 변수, 생성 구성, 기타 관련 정보를 캡슐화하는 Gemini 메서드로 텍스트를 생성하는 데 사용할 수 있는 프롬프트를 나타냅니다.
Prompt 객체를 만들려면 vertexai.preview.prompts.Prompt() 생성자를 사용합니다.
이 객체 내에서 프롬프트 데이터, 변수, 기타 구성을 정의할 수 있습니다.
로컬 프롬프트 만들기 및 콘텐츠 생성
Vertex AI SDK for Python
Vertex AI SDK for Python
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 이러한 ID는 Google Cloud 콘솔 시작 페이지에서 찾을 수 있습니다.
Google Cloud 콘솔에서 액세스할 수 있는 온라인 리소스에 프롬프트를 저장하려면 vertexai.preview.prompts.create_version() 메서드를 사용하세요. 이 메서드는 Prompt 객체를 입력으로 사용하고 온라인 상점에 새 버전의 프롬프트를 만듭니다.
온라인 리소스와 연결된 새 Prompt 객체가 반환됩니다.
Prompt 객체에 적용된 모든 업데이트는 create_version()이 호출될 때까지는 로컬입니다. 다음 코드 샘플은 프롬프트를 저장하는 방법을 보여줍니다.
Vertex AI SDK for Python
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")
Google Cloud 콘솔에서 생성된 프롬프트 검색
저장된 프롬프트를 업데이트하려면 먼저 get() 메서드를 사용하여 프롬프트를 로드하고 필요에 따라 속성을 수정한 다음 create_version() 메서드를 사용하여 업데이트된 프롬프트를 저장합니다. 그러면 업데이트된 정보가 포함된 새 버전의 프롬프트가 생성됩니다.
Vertex AI SDK for Python
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)
프롬프트 버전 복원
프롬프트 리소스에는 이전에 저장된 프롬프트 버전을 저장하는 버전 기록도 포함되어 있습니다. restore_version() 메서드를 사용하여 이전 버전을 최신 버전의 프롬프트로 복원할 수 있습니다. 이렇게 하면 get() 호출과 함께 사용하여 새로 복원된 버전을 가져오는 데 사용할 수 있는 PromptVersionMetadata가 반환됩니다.
Vertex AI SDK for Python
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-04(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\")"]]