メインコンテンツまでスキップ

検索根拠判定

RetrievalGroundednessジャッジは、アプリケーションの応答が、提供されたコンテキスト (RAG システムから、またはツール呼び出しによって生成されたもの) によって事実に基づいてサポートされているかどうかを評価し、そのコンテキストに裏付けられていない幻覚やステートメントを検出するのに役立ちます。

この組み込み LLM ジャッジは、応答が取得された情報に基づいていることを確認する必要がある RAG アプリケーションを評価するために設計されています。

デフォルトでは、このジャッジは GenAI 品質評価を実行するために設計された Databricks ホスト LLM を使用します。ジャッジ定義内のmodel引数を使用して、ジャッジモデルを変更できます。モデルは<provider>:/<model-name>形式で指定する必要があります。ここで、 <provider>は LiteLLM 互換のモデル プロバイダーです。モデル プロバイダーとしてdatabricksを使用する場合、モデル名はサービス エンドポイント名と同じになります。

例を実行するための前提条件

  1. MLflow と必要なパッケージをインストールする

    Bash
    pip install --upgrade "mlflow[databricks]>=3.4.0"
  2. MLflow エクスペリメントを作成するには、環境のセットアップに関するクイックスタートに従ってください。

mlflow.evaluate() での使用

RetrievalGroundednessジャッジは、MLflow の評価フレームワークで直接使用できます。

要件:

  • トレース要件 :
    • MLflow トレースには、 span_type が 1 に設定されたスパンが少なくとも 1 つ含まれている必要があります。 RETRIEVER
    • inputs また、 outputs トレースのルートスパン上にある必要があります
  1. OpenAI クライアントを初期化して、Databricks でホストされている LLM または OpenAI でホストされている LLM に接続します。

MLflow を使用して、Databricks でホストされている LLM に接続する OpenAI クライアントを取得します。利用可能な基盤モデルからモデルを選択します。

Python
import mlflow
from databricks.sdk import WorkspaceClient

# Enable MLflow's autologging to instrument your application with Tracing
mlflow.openai.autolog()

# Set up MLflow tracking to Databricks
mlflow.set_tracking_uri("databricks")
mlflow.set_experiment("/Shared/docs-demo")

# Create an OpenAI client that is connected to Databricks-hosted LLMs
w = WorkspaceClient()
client = w.serving_endpoints.get_open_ai_client()

# Select an LLM
model_name = "databricks-claude-sonnet-4"
  1. ジャッジを使用する:

    Python
    from mlflow.genai.scorers import RetrievalGroundedness
    from mlflow.entities import Document
    from typing import List


    # Define a retriever function with proper span type
    @mlflow.trace(span_type="RETRIEVER")
    def retrieve_docs(query: str) -> List[Document]:
    # Simulated retrieval based on query
    if "mlflow" in query.lower():
    return [
    Document(
    id="doc_1",
    page_content="MLflow is an open-source platform for managing the ML lifecycle.",
    metadata={"source": "mlflow_docs.txt"}
    ),
    Document(
    id="doc_2",
    page_content="MLflow provides tools for experiment tracking, model packaging, and deployment.",
    metadata={"source": "mlflow_features.txt"}
    )
    ]
    else:
    return [
    Document(
    id="doc_3",
    page_content="Machine learning involves training models on data.",
    metadata={"source": "ml_basics.txt"}
    )
    ]

    # Define your RAG app
    @mlflow.trace
    def rag_app(query: str):
    # Retrieve relevant documents
    docs = retrieve_docs(query)
    context = "\n".join([doc.page_content for doc in docs])

    # Generate response using LLM
    messages = [
    {"role": "system", "content": f"Answer based on this context: {context}"},
    {"role": "user", "content": query}
    ]

    response = client.chat.completions.create(
    # This example uses Databricks hosted Claude. If you provide your own OpenAI credentials, replace with a valid OpenAI model e.g., gpt-4o, etc.
    model=model_name,
    messages=messages
    )

    return {"response": response.choices[0].message.content}

    # Create evaluation dataset
    eval_dataset = [
    {
    "inputs": {"query": "What is MLflow used for?"}
    },
    {
    "inputs": {"query": "What are the main features of MLflow?"}
    }
    ]

    # Run evaluation with RetrievalGroundedness scorer
    eval_results = mlflow.genai.evaluate(
    data=eval_dataset,
    predict_fn=rag_app,
    scorers=[
    RetrievalGroundedness(
    model="databricks:/databricks-gpt-oss-120b", # Optional. Defaults to custom Databricks model.
    )
    ]
    )

カスタマイズ

異なるジャッジモデルを提供することでジャッジをカスタマイズできます。

Python
from mlflow.genai.scorers import RetrievalGroundedness

# Use a different judge model
groundedness_judge = RetrievalGroundedness(
model="databricks:/databricks-gpt-5-mini" # Or any LiteLLM-compatible model
)

# Use in evaluation
eval_results = mlflow.genai.evaluate(
data=eval_dataset,
predict_fn=rag_app,
scorers=[groundedness_judge]
)

結果の解釈

ジャッジは、次の Feedback オブジェクトを返します。

  • value :応答が根拠がある場合は「はい」、幻覚が含まれている場合は「いいえ」
  • rationale :以下を特定する詳細な説明:
    • コンテキストでサポートされているステートメント
    • サポートが不足しているステートメント(幻覚)
    • 主張を支持または否定する文脈からの特定の引用

次のステップ