少数ショット プロンプト オプティマイザー

少数ショット プロンプト オプティマイザーは、モデルのレスポンスが期待どおりにならなかった例を分析することで、システム指示を調整するのに役立ちます。プロンプト、モデルのレスポンス、レスポンスに対するフィードバックの具体的な例を指定すると、オプティマイザーがシステム指示を改善します。フィードバックは、ルーブリックとルーブリック ベースの評価、または目標回答のいずれかの方法で提供できます。

フューショット最適化では、Pandas DataFrame で例を指定する必要があります。これは Vertex AI SDK で使用できます。

ルーブリック ベースの評価を使用して最適化する

この方法は、各例のルーブリック(採点基準とも呼ばれます)と、モデルがその基準を満たしたかどうかの記録があるシナリオで使用します。DataFrame には次の 4 つの列が含まれている必要があります。

  • 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 には次の 3 つの列が含まれている必要があります。

  • 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)