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プロジェクト作成

  1. 以下のコマンドを実行し、Mastraのプロジェクトを作成する。ここではmy-mastra-appという名前でプロジェクトを作成します。
npx create-mastra
  1. 作業ディレクトリを移動します。
cd my-mastra-app
  1. パッケージをインストールします。
npm i @ai-sdk/azure
npm i @mastra/langfuse
  1. 以下のファイルを変更し、LLMモデルにAzure OpenAI Serviceを使うように変更します。
src\mastra\agents\weather-agent.ts
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),
 <省略>
  },
});
  1. LangfuseのAPIキーを発行する。こちらの記事を参考にAPIキー(パブリックキー、シークレットキー)を発行します。

  2. Langfuseでトレースするため、先ほど発行したAPIキー(パブリックキー、シークレットキー)を以下のように設定します。
    以上でMastraへの問い合わせをLangfuseでトレースできるようになります。

src\mastra\index.ts
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追加

  1. LangfuseのUIからプロジェクトを選択します。

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

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

  4. Set Upを選択します。

  5. Add LLM Connectionを選択します。

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

  7. Saveを選択します。

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

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

  10. Set up evaluatorを選択します。

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

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

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

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

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

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

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

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

まとめ & 次のステップ

  • LLM-as-a-Judgeの動作確認を行いました。
  • 今後、LLM-as-a-Judgeが用意する評価の内容を正しく理解していきたいです。
セリオ株式会社 テックブログ

Discussion