プロンプト整合性評価
We just released a new evals API called Scorers, with a more ergonomic API and more metadata stored for error analysis, and more flexibility to evaluate data structures. It’s fairly simple to migrate, but we will continue to support the existing Evals API.
PromptAlignmentMetric
を使用して、応答が与えられた指示セットにどの程度従っているかを評価します。メトリクスは query
と response
を受け取り、スコアと、理由および指示ごとの整合性の詳細を含む info
オブジェクトを返します。
インストール
npm install @mastra/evals
完全整合の例
この例では、レスポンスが入力の該当する指示すべてに従っています。スコアは完全な遵守を反映しており、見落としや無視された指示はありません。
import { openai } from "@ai-sdk/openai";
import { PromptAlignmentMetric } from "@mastra/evals/llm";
const metric = new PromptAlignmentMetric(openai("gpt-4o-mini"), {
instructions: [
"Use complete sentences",
"Include temperature in Celsius",
"Mention wind conditions", "State precipitation chance"
]
});
const query = "What is the weather like?";
const response = "The temperature is 22 degrees Celsius with moderate winds from the northwest. There is a 30% chance of rain.";
const result = await metric.measure(query, response);
console.log(result);
完全整合の出力
このレスポンスは、該当するすべての指示を完全に満たしているため高得点となります。info
フィールドは、各指示が漏れなく守られていることを示しています。
{
score: 1,
info: {
reason: 'The score is 1 because the output fully aligns with all applicable instructions, providing a comprehensive weather report that includes temperature, wind conditions, and chance of precipitation, all presented in complete sentences.',
scoreDetails: {
totalInstructions: 4,
applicableInstructions: 4,
followedInstructions: 4,
naInstructions: 0
}
}
}
混合アラインメントの例
この例では、レスポンスはいくつかの指示には従っているものの、他の指示は満たしていません。スコアは部分的な順守を反映しており、順守された指示と見落とされた指示が混在しています。
import { openai } from "@ai-sdk/openai";
import { PromptAlignmentMetric } from "@mastra/evals/llm";
const metric = new PromptAlignmentMetric(openai("gpt-4o-mini"), {
instructions: [
"Use bullet points",
"Include prices in USD",
"Show stock status", "Add product descriptions"
]
});
const query = "List the available products";
const response = "• Coffee - $4.99 (In Stock)\n• Tea - $3.99\n• Water - $1.99 (Out of Stock)";
const result = await metric.measure(query, response);
console.log(result);
混合アラインメントの出力
このレスポンスは、いくつかの指示には従っている一方で、他の指示を満たしていないため、混合評価となります。info
フィールドには、順守された指示と見落とされた指示の内訳に加え、スコアの根拠が含まれています。
{
score: 0.75,
info: {
reason: 'スコアが0.75なのは、箇条書きの使用、USDでの価格表示、在庫状況の表示により、ほとんどの指示を満たしているためです。ただし、商品説明を提供するという指示には完全には沿っておらず、全体のスコアに影響しています。',
scoreDetails: {
totalInstructions: 4,
applicableInstructions: 4,
followedInstructions: 3,
naInstructions: 0
}
}
}
非該当のアラインメント例
この例では、クエリと無関係なため、レスポンスはどの指示にも従っていません。スコアは、この文脈では指示が適用できなかったことを反映しています。
import { openai } from "@ai-sdk/openai";
import { PromptAlignmentMetric } from "@mastra/evals/llm";
const metric = new PromptAlignmentMetric(openai("gpt-4o-mini"), {
instructions: [
"Show account balance",
"List recent transactions",
"Display payment history"
]
});
const query = "What is the weather like?";
const response = "It is sunny and warm outside.";
const result = await metric.measure(query, response);
console.log(result);
非該当のアラインメント出力
レスポンスは、いずれの指示も適用できなかったことを示すスコアを受け取ります。info
フィールドには、レスポンスとクエリが指示と無関係であるため、整合性を測定できないことが記載されています。
{
score: 0,
info: {
reason: 'The score is 0 because the output does not follow any applicable instructions related to the context of a weather query, as the instructions provided are irrelevant to the input.',
scoreDetails: {
totalInstructions: 3,
applicableInstructions: 0,
followedInstructions: 0,
naInstructions: 3
}
}
}
メトリクスの設定
instructions
配列で期待する動作や要件を定義し、PromptAlignmentMetric
インスタンスを作成できます。scale
などの任意のパラメータも設定できます。
const metric = new PromptAlignmentMetric(openai("gpt-4o-mini"), {
instructions: [""],
scale: 1
});
設定可能なオプションの一覧については、PromptAlignmentMetric を参照してください。
結果の理解
PromptAlignment
は次の形式の結果を返します:
{
score: number,
info: {
reason: string,
scoreDetails: {
followed: string[],
missed: string[],
notApplicable: string[]
}
}
}
プロンプト整合スコア
0〜1 の範囲のプロンプト整合スコア:
- 1.0: 完全に整合 — 該当する指示がすべて守られている。
- 0.5–0.8: 部分的な整合 — 一部の指示が守られていない。
- 0.1–0.4: 低い整合 — ほとんどの指示が守られていない。
- 0.0: 非整合 — 指示が該当しない、または守られていない。
- -1: 該当なし — クエリに無関係な指示。
プロンプト整合に関する情報
スコアの説明。以下の詳細を含む:
- 各指示の遵守状況。
- クエリへの適用可能性の度合い。
- 守られた指示/見落とされた指示/非該当の指示の分類。
- 整合スコアの根拠。