テキスト差分評価
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.
TextualDifferenceMetric
を使用して、シーケンス差分や編集操作を解析し、2つのテキスト文字列の類似度を評価します。メトリクスは query
と response
を受け取り、スコアと、confidence、ratio、変更数、長さ差を含む info
オブジェクトを返します。
インストール
npm install @mastra/evals
差分なしの例
この例では、テキストは完全に同一です。メトリクスは完全一致を判定し、満点のスコアと変更なしを示します。
import { TextualDifferenceMetric } from "@mastra/evals/nlp";
const metric = new TextualDifferenceMetric();
const query = "The quick brown fox jumps over the lazy dog.";
const response = "The quick brown fox jumps over the lazy dog.";
const result = await metric.measure(query, response);
console.log(result);
差分なしの出力
メトリクスは高いスコアを返し、テキストが同一であることを示します。詳細情報は、変更がゼロで長さの差もないことを確認しています。
{
score: 1,
info: {
confidence: 1,
ratio: 1,
changes: 0,
lengthDiff: 0
}
}
軽微な差分の例
この例では、テキストに小さな差異があります。メトリクスはこれらの軽微な差分を検出し、類似度は中程度のスコアとして返されます。
import { TextualDifferenceMetric } from "@mastra/evals/nlp";
const metric = new TextualDifferenceMetric();
const query = "Hello world! How are you?";
const response = "Hello there! How is it going?";
const result = await metric.measure(query, response);
console.log(result);
軽微な差分の出力
メトリクスは、テキスト間の小さな差異を反映した中程度のスコアを返します。詳細情報には、検出された変更数と長さの差が含まれます。
{
score: 0.5925925925925926,
info: {
confidence: 0.8620689655172413,
ratio: 0.5925925925925926,
changes: 5,
lengthDiff: 0.13793103448275862
}
}
大きな差異の例
この例では、テキストに大きな差があります。メトリックは大幅な変更を検出し、低い類似度スコアを返します。
import { TextualDifferenceMetric } from "@mastra/evals/nlp";
const metric = new TextualDifferenceMetric();
const query = "Python is a high-level programming language.";
const response = "JavaScript is used for web development";
const result = await metric.measure(query, response);
console.log(result);
大きな差異の出力
テキスト間の差異が大きいため、メトリックは低いスコアを返します。詳細情報には多数の変更点と顕著な長さの違いが示されます。
{
score: 0.3170731707317073,
info: {
confidence: 0.8636363636363636,
ratio: 0.3170731707317073,
changes: 8,
lengthDiff: 0.13636363636363635
}
}
メトリクスの設定
TextualDifferenceMetric
のインスタンスはデフォルト設定で作成できます。追加の設定は不要です。
const metric = new TextualDifferenceMetric();
設定可能なオプションの一覧は TextualDifferenceMetric を参照してください。
結果の理解
TextualDifferenceMetric
は次の形式の結果を返します:
{
score: number,
info: {
confidence: number,
ratio: number,
changes: number,
lengthDiff: number
}
}
テキスト差分スコア
0 から 1 の範囲のテキスト差分スコア:
- 1.0: テキストは同一 — 差分なし。
- 0.7–0.9: 軽微な差分 — わずかな修正が必要。
- 0.4–0.6: 中程度の差分 — 目立つ修正が必要。
- 0.1–0.3: 大きな差分 — 大幅な修正が必要。
- 0.0: 完全に異なるテキスト。
テキスト差分情報
スコアの根拠となる説明。以下の詳細を含みます:
- テキスト長の比較に基づく信頼度。
- シーケンスマッチングから算出される類似度比。
- テキストを一致させるために必要な編集操作の回数。
- テキスト長の正規化された差分。