零樣本最佳化工具可自動修正及改良使用者編寫的提示。如果提示詞語意不明、缺少背景資訊或包含不相關的資訊,模型可能無法產生您想要的回覆。這項工具會分析並重寫現有提示,使其更清晰、有效,且更符合模型能力,最終生成更高品質的回覆。
零樣本最佳化工具特別適用於:
因應模型更新:升級至新版模型後,現有提示可能無法發揮最佳效果。
強化提示理解:如果提示的措辭複雜或可能遭誤解,這項工具可以重新措辭,盡可能提高清晰度和精確度,降低產生不理想結果的機率。
使用最佳化工具的方法有兩種:
生成指示:不必從頭撰寫複雜的系統指示,只要用一般語言描述目標或工作,接著,最佳化工具會生成一套完整且結構良好的系統指令,協助您達成目標。
提示詞修正:您有可用的提示詞,但模型的輸出內容不一致、稍微離題或缺乏所需細節。最佳化工具可協助改善提示,取得更優質的輸出內容。
最佳化工具支援 Gemini 支援的所有語言,可透過 Agent Platform SDK 使用。
事前準備
為確保 Compute Engine 預設服務帳戶具備必要權限,可最佳化提示,請要求管理員將專案的下列 IAM 角色授予該帳戶,以及具備 Agent Platform 使用者 (roles/aiplatform.user) 角色的使用者帳戶:
- Agent Platform 使用者 (
roles/aiplatform.user) - Vertex AI 服務代理 (
roles/aiplatform.serviceAgent)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
管理員或許也能透過自訂角色或其他預先定義的角色,將必要權限授予 Compute Engine 預設服務帳戶和您的使用者帳戶。
將提示最佳化
# Import libraries
import vertexai
import logging
# Google Colab authentication
from google.colab import auth
PROJECT_NAME = "PROJECT"
auth.authenticate_user(project_id=PROJECT_NAME)
# Initialize the Agent Platform client
client = vertexai.Client(project=PROJECT_NAME, location='us-central1')
# Input original prompt to optimize
prompt = """You are a professional chef. Your goal is teaching how to cook healthy cooking recipes to your apprentice.
Given a question from your apprentice and some context, provide the correct answer to the question.
Use the context to return a single and correct answer with some explanation.
"""
# Optimize prompt
output = client.prompt_optimizer.optimize_prompt(prompt=prompt)
# View optimized prompt
print(output.model_dump_json(indent=2))
這個 output 物件的類型為 OptimizeResponse,可提供最佳化程序的相關資訊。其中最重要的部分是 suggested_prompt,內含最佳化提示,可讓模型產生更優質的結果。其他欄位 (尤其是 applicable_guidelines) 有助於瞭解提示詞改善的原因和方式,進而撰寫更優質的提示詞。輸出內容範例如下:
{
"optimization_mode": "zero_shot",
"applicable_guidelines": [
{
"applicable_guideline": "Structure",
"suggested_improvement": "Add role definition.",
"text_before_change": "...",
"text_after_change": "Role: You are an AI assistant...\n\nTask Context:\n..."
},
{
"applicable_guideline": "RedundancyInstructions",
"suggested_improvement": "Remove redundant explanation.",
"text_before_change": "...",
"text_after_change": ""
}
],
"original_prompt": "...",
"suggested_prompt": "Role: You are an AI assistant...\n\nTask Context:\n..."
}
針對小型模型進行最佳化
Gemma 3n E4B 等小型模型,與大型模型在指令遵循能力方面有所不同。大型模型適用的提示,不一定適合這些小型模型。零樣本最佳化工具提供 gemini_nano 模式,可解決這個問題。gemini_nano 模式會根據小型模型的特性調整提示,生成更符合小型模型能力的提示。
以下範例說明如何針對較小的模型最佳化提示:
client = vertexai.Client(project=PROJECT_NAME, location='us-central1')
prompt = 'Generate system instructions for analyzing medical articles'
# optimize_prompt returns OptimizeResponse type, which has 2 fields: raw_text_response and parsed_response
# raw_text_response is guaranteed and always will be given to the user.
# parsed_response is not guaranteed. If parsing is possible, the parsed response is stored in this field.
# parsed_response contains the following 4 fields:
# optimization_type, applicable_guidelines, original_prompt, suggested_prompt
response = client.prompt_optimizer.optimize_prompt(
prompt=prompt,
config=vertexai.types.OptimizeConfig(
optimization_target=vertexai.types.OptimizeTarget.OPTIMIZATION_TARGET_GEMINI_NANO
)
)
# print(response.raw_text_response)
print(response.parsed_response.suggested_prompt)