零樣本最佳化工具

使用零樣本最佳化工具,自動修正及改善提示。如果提示的語言含糊不清、缺少背景資訊或包含不相關的資訊,可能無法產生您想要的結果。這項工具會分析並改寫提示,讓提示更清楚、更有效,且更符合模型功能,進而產生品質更高的回覆。

零樣本最佳化工具適用於下列情境:

  • 配合模型更新:升級至較新模型版本後,現有提示可能無法發揮最佳效果。
  • 提升提示理解程度:如果提示的措辭複雜或可能遭到誤解,最佳化工具可以重新措辭,讓提示更清楚明確。

本頁說明如何使用零樣本提示最佳化工具,並包含下列章節:

零樣本最佳化工具的使用時機

最佳化工具的使用方法有兩種。下表比較這兩種方法。

選項 說明 用途
生成指令 根據您以簡單語言描述的目標,生成完整且結構良好的系統指令集。 需要從頭建立複雜的新提示,並確保提示結構良好。
修正提示 分析及改善現有提示,生成更一致、詳細或切題的回覆。 提示可正常運作,但模型輸出內容未達品質標準。

將提示最佳化

最佳化工具可透過 Vertex AI SDK 使用,並支援 Gemini 支援的所有語言。以下範例說明如何呼叫最佳化工具:

# 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 Vertex AI 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)

瞭解輸出內容

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..."
}