提示管理

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() 建構函式。您可以在這個物件中定義提示資料、變數和其他設定。

建立本地提示並生成內容

Python 適用的 Vertex AI SDK

Python

import vertexai
from vertexai.preview import prompts
from vertexai.preview.prompts import Prompt

# from vertexai.generative_models import GenerationConfig, SafetySetting # Optional

# Initialize vertexai
vertexai.init(project=PROJECT_ID, location="us-central1")

# Create local Prompt
local_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.
for i in range(len(local_prompt.variables)):
    response = local_prompt.generate_content(
        contents=local_prompt.assemble_contents(**local_prompt.variables[i])
    )
    print(response)

# Save a version
prompt1 = 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:。您可以在 Google Cloud 控制台 歡迎頁面中找到這些 ID。
  • location:請參閱 Vertex AI 地理位置
  • prompt_name:使用者建立的提示顯示名稱 (如儲存在線上資源中)。
  • prompt_dataPartsType 提示,可以是含有變數的範本,也可以是不含變數的提示。
  • variables:包含變數名稱和值的字典清單。
  • generation_config:包含生成參數的 GenerationConfig 物件。
  • model_name:Model Garden 模型資源名稱。或者,您也可以提供調整後模型端點資源名稱。如未提供模型,系統會使用預設的最新模型。
  • safety_settings:包含生成安全設定的 SafetySetting 物件。
  • system_instruction:代表系統指令的 PartsType 物件。

建立 Prompt 物件後,即可使用代表各種設定的提示資料和屬性生成內容。

提示也支援函式呼叫。詳情請參閱「函式呼叫簡介」。

儲存提示

如要將提示儲存至線上資源,並在Google Cloud 控制台中存取,請使用 vertexai.preview.prompts.create_version() 方法。這個方法會將 Prompt 物件做為輸入內容,並在線上商店中建立新版提示。 系統會傳回與線上資源相關聯的新 Prompt 物件。對 Prompt 物件所做的任何更新都是本機更新,直到呼叫 create_version() 為止。以下程式碼範例說明如何儲存提示:

Python 適用的 Vertex AI SDK

from vertexai.preview import prompts

# 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

from vertexai.preview import prompts

# Get prompt
prompt = prompts.get(prompt_id="123456789")

擷取在 Google Cloud 控制台中建立的提示

如要更新已儲存的提示,請先使用 get() 方法載入提示,視需要修改提示的屬性,然後使用 create_version() 方法儲存更新後的提示。系統會根據更新後的資訊建立新版提示。

Python 適用的 Vertex AI SDK

from vertexai.preview import prompts
from vertexai.preview.prompts import Prompt

# Get prompt
prompt = 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 resource
prompt1 = prompts.create_version(prompt=prompt)

列出提示

如要查看目前Google Cloud 專案中儲存的所有提示的顯示名稱和提示 ID,請使用 list_prompts() 方法

Python 適用的 Vertex AI SDK

from vertexai.preview import prompts

prompts_metadata = prompts.list()

# Get a prompt from the list
prompt1 = prompts.get(prompt_id=prompts_metadata[0].prompt_id)

列出提示版本

如要查看提示中儲存的所有提示版本顯示名稱和版本 ID,請使用 list_versions() 方法

Python 適用的 Vertex AI SDK

from vertexai.preview import prompts

prompt_versions_metadata = prompts.list_versions(prompt_id="123456789")

# Get a specific prompt version from the versions metadata list
prompt1 = prompts.get(
    prompt_id=prompt_versions_metadata[3].prompt_id,
    version_id=prompt_versions_metadata[3].version_id
)

還原提示版本

提示資源也包含版本記錄,可儲存先前儲存的提示版本。您可以使用 restore_version() 方法,將舊版提示還原為最新版本。這會傳回 PromptVersionMetadata,可用於 get() 呼叫,擷取新還原的版本。

Python 適用的 Vertex AI SDK

from vertexai.preview import prompts

# 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 prompt
prompt1 = prompts.get(prompt_id=prompt_version_metadata.prompt_id)

刪除提示

如要刪除與提示 ID 相關聯的線上資源,請使用 delete() 方法

Python 適用的 Vertex AI SDK

from vertexai.preview import prompts

prompts.delete(prompt_id="123456789")