少量樣本提示最佳化工具

少量樣本提示最佳化工具會分析模型回應未達預期的範例,協助您修正系統指令。您提供提示、模型回覆和回覆意見的具體範例,最佳化工具就會改善系統指令。您可以透過評分量表和評分量表評估,或目標回覆提供意見。

如要進行少樣本最佳化,您必須在 Pandas DataFrame 中提供範例,並透過 Vertex AI SDK 進行。

使用評量表進行最佳化

這個方法適用於您為每個範例提供評分標準 (也稱為評分條件),並記錄模型是否符合這些條件的情況。DataFrame 必須包含下列四個資料欄:

  • prompt:原始使用者提示。
  • model_response:模型實際生成的輸出內容。
  • rubrics:該範例的特定條件清單,以字串清單的字串表示法形式呈現。
  • rubrics_evaluations:布林值清單,表示是否符合各項評量標準,以布林值清單的字串表示。

評量表可套用至所有資料列,也可針對每個範例量身打造。

import pandas as pd
import vertexai
from vertexai import types

PROJECT_NAME = "YOUR_PROJECT_NAME"
LOCATION = "YOUR_LOCATION" # e.g. "us-central1"
client = vertexai.Client(project=PROJECT_NAME, location=LOCATION)

system_instructions = "You are an AI assistant skilled in analyzing articles. Your task is to extract key arguments, evaluate evidence quality, identify potential biases, and summarize core findings into a concise, actionable, and objective report. Make sure the response is less than 50 words"
df = pd.DataFrame(
    [
        {
            "prompt": "prompt1",
            "model_response": "response1",
            "rubrics": "['Response is in English', 'Under 50 words']",
            "rubrics_evaluations": "[True, True]",
        },
        {
            "prompt": "prompt2",
            "model_response": "response2",
            "rubrics": "['Response is in English', 'Under 50 words']",
            "rubrics_evaluations": "[True, False]",
        },
    ]
)

config = vertexai.types.OptimizeConfig(
    optimization_target=vertexai.types.OptimizeTarget.OPTIMIZATION_TARGET_FEW_SHOT_RUBRICS,
    examples_dataframe=df,
)

response = client.prompts.optimize(prompt=system_instructions, config=config)

print(response.parsed_response.suggested_prompt)

使用目標回覆進行最佳化

如果您沒有特定評量表,但每個提示都有理想或「黃金標準」的回覆,則可以使用以目標回覆為準的調整功能。最佳化工具會比較模型輸出內容與理想輸出內容,並調整指令來彌補差距。

DataFrame 必須包含下列三欄:

  • prompt:原始使用者指令。
  • model_response:目前需要改進的模型輸出內容。
  • target_response:您希望模型產生的理想回應。
import pandas as pd
import vertexai
from vertexai import types

PROJECT_NAME = "PROJECT"
LOCATION = "YOUR_LOCATION" # e.g. "us-central1"
client = vertexai.Client(project=YOUR_PROJECT_NAME, location=LOCATION)

system_instructions = "You are an AI assistant skilled in analyzing articles. Your task is to extract key arguments, evaluate evidence quality, identify potential biases, and summarize core findings into a concise, actionable, and objective report. Make sure the response is less than 50 words"
df = pd.DataFrame(
    {
        "prompt": ["prompt1", "prompt2"],
        "model_response": ["response1", "response2"],
        "target_response": ["target1", "target2"],
    }
)
config = vertexai.types.OptimizeConfig(
    optimization_target=vertexai.types.OptimizeTarget.OPTIMIZATION_TARGET_FEW_SHOT_TARGET_RESPONSE,
    examples_dataframe=df,
)
response = client.prompts.optimize(
    prompt=system_instructions,
    config=config,
)

print(response.parsed_response.suggested_prompt)