LangfuseでLLM-as-a-Judgeを試してみた
背景
先日、社内向けにTeamsのAIチャットボットアプリを公開しました。こちらでは、質問に対して社内ナレッジ検索とWeb検索した結果をまとめて通知する機能があります。しかし、回答の精度改善が課題となっており、引き続き改善のための取り組みを予定しています。
回答の精度改善をするうえで、回答結果の正確性などを定量的に評価する必要があります。そこで今回はAIチャットボットの回答の精度改善をする際の評価機構としてLLM as a judgeの導入を考えています。また、現在LangfuseにMastraで作成したAIチャットボットへの問い合わせをトレースしているので、その情報を活かして評価する手段を動作検証した内容をここに記します。
環境
| 項目 | バージョン |
|---|---|
| OS | Windows 11 |
| ランタイム | Node.js v22.14.0 |
| 主要ライブラリ | "@ai-sdk/azure": "^2.0.70", "@mastra/core": "^0.24.1", "@mastra/evals": "^0.14.4", "@mastra/langfuse": "^0.2.3", "@mastra/libsql": "^0.16.2", "@mastra/loggers": "^0.10.19", "@mastra/memory": "^0.15.11", "zod": "^3.25.76" |
| LLM | Azure OpenAI(gpt-4.1-mini) |
手順
Mastraプロジェクト作成
- 以下のコマンドを実行し、Mastraのプロジェクトを作成する。ここでは
my-mastra-appという名前でプロジェクトを作成します。
npx create-mastra
- 作業ディレクトリを移動します。
cd my-mastra-app
- パッケージをインストールします。
npm i @ai-sdk/azure
npm i @mastra/langfuse
- 以下のファイルを変更し、LLMモデルにAzure OpenAI Serviceを使うように変更します。
import { Agent } from '@mastra/core/agent';
import { Memory } from '@mastra/memory';
import { LibSQLStore } from '@mastra/libsql';
import { weatherTool } from '../tools/weather-tool';
import { scorers } from '../scorers/weather-scorer';
import { createAzure } from "@ai-sdk/azure";
import { config } from '../config';
// Azure 接続情報 設定
const azure = createAzure({
apiKey: config.apiKey,
resourceName: config.resourceName,
})
export const weatherAgent = new Agent({
name: 'Weather Agent',
<省略>
model: azure(config.deploymentName),
<省略>
},
});
-
LangfuseのAPIキーを発行する。こちらの記事を参考にAPIキー(パブリックキー、シークレットキー)を発行します。
-
Langfuseでトレースするため、先ほど発行したAPIキー(パブリックキー、シークレットキー)を以下のように設定します。
以上でMastraへの問い合わせをLangfuseでトレースできるようになります。
import { Mastra } from '@mastra/core/mastra';
import { PinoLogger } from '@mastra/loggers';
import { LibSQLStore } from '@mastra/libsql';
import { weatherWorkflow } from './workflows/weather-workflow';
import { weatherAgent } from './agents/weather-agent';
import { toolCallAppropriatenessScorer, completenessScorer, translationScorer } from './scorers/weather-scorer';
import { LangfuseExporter } from "@mastra/langfuse";
import { config } from './config';
export const mastra = new Mastra({
<省略>
observability: {
configs: {
langfuse: {
serviceName: "my-service",
exporters: [
new LangfuseExporter({
publicKey: config.langfusePublicKey,
secretKey: config.langfuseSecretKey,
baseUrl: config.langfuseBaseUrl,
options: {
environment: "development",
},
}),
],
},
},
},
});
LLM as a JudgeのEvaluation追加
-
LangfuseのUIからプロジェクトを選択します。

-
LLM-as-a-Judgeを選択します。

-
こちらを選択してLLMのモデルを設定します。

-
Set Upを選択します。

-
Add LLM Connectionを選択します。

-
接続情報を入力してCreate Connectionを選択します。

-
Saveを選択します。

-
以上でデフォルトモデルが設定されます。

-
再びLLM-as-a-Judgeを選択します。

-
Set up evaluatorを選択します。

-
今回は動作確認のため、Faithfulnessを選択します。

-
{{answer}}のObject Variableを Output に指定し、Executeを選択して実行します。

-
Statusがactiveとなり、実行されます。実行結果はViewを選択して参照できます。

-
初期状態は空です。これからMastraに問い合わせた内容は、こちらで設定した評価が実行されます。

-
動作確認のためMastraに問い合わせを行います

-
リロードするとStatusがpendingのデータがあり、評価を実行中であることがわかります。

-
評価が終了するとStatusがcompletedとなります。Execution Traceを選択し、評価結果を参照できます。

-
評価結果が赤枠内となります。「回答には、一般的な天気、気温、湿度、風速などの複数の気象情報と質問を組み合わせた複数の複雑な文が含まれています。この複雑さにより、各文を代名詞を使わずに、より単純で完全に理解可能なステートメントに分割することが正当化されます」という評価結果が表示されています。また、scoreは1(出典に対する回答の忠実性がある)となっていました。scoreが0の場合は回答に事実誤認があると判定されています。

まとめ & 次のステップ
- LLM-as-a-Judgeの動作確認を行いました。
- 今後、LLM-as-a-Judgeが用意する評価の内容を正しく理解していきたいです。
Discussion