少样本提示优化器可帮助您通过分析模型回答未达到预期的示例来优化系统指令。您提供提示、模型回答以及对这些回答的反馈的具体示例,优化器会改进系统指令。您可以通过以下两种方式提供反馈:评分标准和基于评分标准的评估或目标回答。
小样本优化需要您在 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)